aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt72
1 files changed, 49 insertions, 23 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
index ab7e165a..57d5a88d 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
@@ -14,6 +14,10 @@ import com.pitchedapps.frost.utils.launchLogin
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.subjects.SingleSubject
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
+import kotlin.coroutines.resume
+import kotlin.coroutines.suspendCoroutine
/**
* Created by Allan Wang on 2017-05-30.
@@ -27,37 +31,59 @@ object FbCookie {
inline val webCookie: String?
get() = CookieManager.getInstance().getCookie(FB_URL_BASE)
- private fun setWebCookie(cookie: String?, callback: (() -> Unit)?) {
- with(CookieManager.getInstance()) {
- removeAllCookies { _ ->
- if (cookie == null) {
- callback?.invoke()
- return@removeAllCookies
- }
- L.d { "Setting 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() }) {}
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe {
- callback?.invoke()
- L.d { "Cookies set" }
- L._d { cookie }
- flush()
- }
+ private fun CookieManager.setWebCookie(cookie: String?, callback: (() -> Unit)?) {
+ removeAllCookies { _ ->
+ if (cookie == null) {
+ callback?.invoke()
+ return@removeAllCookies
}
+ L.d { "Setting 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() }) {}
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe {
+ callback?.invoke()
+ L.d { "Cookies set" }
+ L._d { cookie }
+ flush()
+ }
+ }
+ }
+
+ private suspend fun CookieManager.suspendSetWebCookie(cookie: String?): Boolean {
+ cookie ?: return true
+ removeAllCookies()
+ val result = cookie.split(":").all {
+ setSingleWebCookie(it)
+ }
+ flush()
+ return result
+ }
+
+ private suspend fun CookieManager.removeAllCookies(): Boolean = suspendCoroutine { cont ->
+ removeAllCookies {
+ cont.resume(it)
}
}
+ private suspend fun CookieManager.setSingleWebCookie(cookie: String): Boolean = suspendCoroutine { cont ->
+ setCookie(FB_URL_BASE, cookie) {
+ cont.resume(it)
+ }
+ }
+
+
operator fun invoke() {
L.d { "FbCookie Invoke User" }
- with(CookieManager.getInstance()) {
- setAcceptCookie(true)
- }
+ val manager = CookieManager.getInstance()
+ manager.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)
+ GlobalScope.launch {
+ manager.suspendSetWebCookie(dbCookie)
+ }
}
}
@@ -91,7 +117,7 @@ object FbCookie {
}
L.d { "Switching User" }
Prefs.userId = cookie.id
- setWebCookie(cookie.cookie, callback)
+ CookieManager.getInstance().setWebCookie(cookie.cookie, callback)
}
/**