aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
blob: 875f1c49d69146a0163bd8c01b9738a9fbbc5857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.pitchedapps.frost.facebook

import android.webkit.CookieManager
import com.pitchedapps.frost.dbflow.CookieModel
import com.pitchedapps.frost.dbflow.loadFbCookie
import com.pitchedapps.frost.dbflow.removeCookie
import com.pitchedapps.frost.dbflow.saveFbCookie
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.subjects.SingleSubject

/**
 * Created by Allan Wang on 2017-05-30.
 */
object FbCookie {

    val webCookie: String?
        get() = CookieManager.getInstance().getCookie(FB_URL_BASE)

    fun setWebCookie(cookie: String?, callback: (() -> Unit)?) {
        with(CookieManager.getInstance()) {
            removeAllCookies({
                if (cookie == null) {
                    callback?.invoke()
                    return@removeAllCookies
                }
                L.d("Setting cookie", cookie)
                val cookies = cookie.split(";").map { Pair(it, SingleSubject.create<Boolean>()) }
                cookies.forEach { (cookie, callback) -> setCookie(FB_URL_BASE, cookie, { callback.onSuccess(it) }) }
                Observable.zip<Boolean, Unit>(cookies.map { (_, callback) -> callback.toObservable() }, {}).subscribeOn(AndroidSchedulers.mainThread()).subscribe({
                    callback?.invoke()
                    L.d("Cookies set", webCookie)
                    flush()
                })
            })
        }
    }

    operator fun invoke() {
        L.d("FbCookie Invoke User", Prefs.userId.toString())
        with(CookieManager.getInstance()) {
            setAcceptCookie(true)
        }
        val dbCookie = loadFbCookie(Prefs.userId)?.cookie
        if (dbCookie != null && webCookie == null) {
            L.d("DbCookie found & WebCookie is null; setting webcookie")
            setWebCookie(dbCookie, null)
        }
    }

    fun save(id: Long) {
        L.d("New cookie found", id.toString())
        Prefs.userId = id
        CookieManager.getInstance().flush()
        val cookie = CookieModel(Prefs.userId, "", webCookie)
        saveFbCookie(cookie)
    }

    fun reset(callback: () -> Unit) {
        Prefs.userId = -1L
        with(CookieManager.getInstance()) {
            removeAllCookies({
                flush()
                callback()
            })
        }
    }

    fun switchUser(id: Long, callback: () -> Unit) = switchUser(loadFbCookie(id), callback)

    fun switchUser(name: String, callback: () -> Unit) = switchUser(loadFbCookie(name), callback)

    fun switchUser(cookie: CookieModel?, callback: () -> Unit) {
        if (cookie == null) {
            L.d("Switching User; null cookie")
            callback()
            return
        }
        L.d("Switching User", cookie.toString())
        Prefs.userId = cookie.id
        setWebCookie(cookie.cookie, callback)
    }

    fun logout(id: Long, callback: () -> Unit) {
        L.d("Logging out user $id")
        removeCookie(id)
        reset(callback)
    }

    /**
     * Notifications may come from different accounts, and we need to switch the cookies to load them
     * When coming back to the main app, switch back to our original account before continuing
     */
    fun switchBackUser(callback: () -> Unit) {
        if (Prefs.prevId != -1L && Prefs.prevId != Prefs.userId) {
            switchUser(Prefs.prevId) {
                L.d("Switch back user", "${Prefs.userId} to ${Prefs.prevId}")
                callback()
            }
        } else callback()
        if (Prefs.prevId != -1L) Prefs.prevId = -1L
    }
}