diff options
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt | 22 |
1 files changed, 15 insertions, 7 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 57d5a88d..d54c82bc 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,7 @@ import com.pitchedapps.frost.utils.launchLogin import io.reactivex.Observable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.subjects.SingleSubject +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlin.coroutines.resume @@ -24,12 +25,14 @@ import kotlin.coroutines.suspendCoroutine */ object FbCookie { + const val COOKIE_DOMAIN = FACEBOOK_COM + /** * Retrieves the facebook cookie if it exists * Note that this is a synchronized call */ inline val webCookie: String? - get() = CookieManager.getInstance().getCookie(FB_URL_BASE) + get() = CookieManager.getInstance().getCookie(COOKIE_DOMAIN) private fun CookieManager.setWebCookie(cookie: String?, callback: (() -> Unit)?) { removeAllCookies { _ -> @@ -39,7 +42,7 @@ object FbCookie { } 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) } } + cookies.forEach { (cookie, callback) -> setCookie(COOKIE_DOMAIN, cookie) { callback.onSuccess(it) } } Observable.zip<Boolean, Unit>(cookies.map { (_, callback) -> callback.toObservable() }) {} .observeOn(AndroidSchedulers.mainThread()) .subscribe { @@ -53,22 +56,27 @@ object FbCookie { private suspend fun CookieManager.suspendSetWebCookie(cookie: String?): Boolean { cookie ?: return true + L.test { "Orig ${webCookie}" } removeAllCookies() - val result = cookie.split(":").all { - setSingleWebCookie(it) - } + L.test { "Save $cookie" } + // Save all cookies regardless of result, then check if all succeeded + val result = cookie.split(";").map { setSingleWebCookie(it) }.all { it } + L.test { "AAAA ${webCookie}" } flush() + L.test { "SSSS ${webCookie}" } return result } private suspend fun CookieManager.removeAllCookies(): Boolean = suspendCoroutine { cont -> removeAllCookies { + L.test { "Removed all cookies $webCookie" } cont.resume(it) } } private suspend fun CookieManager.setSingleWebCookie(cookie: String): Boolean = suspendCoroutine { cont -> - setCookie(FB_URL_BASE, cookie) { + setCookie(COOKIE_DOMAIN, cookie.trim()) { + L.test { "Save single $cookie\n\n\t$webCookie" } cont.resume(it) } } @@ -81,7 +89,7 @@ object FbCookie { val dbCookie = loadFbCookie(Prefs.userId)?.cookie if (dbCookie != null && webCookie == null) { L.d { "DbCookie found & WebCookie is null; setting webcookie" } - GlobalScope.launch { + GlobalScope.launch(Dispatchers.Main) { manager.suspendSetWebCookie(dbCookie) } } |