From 46fb61e53327c6eb1ebc3bfced956f3e05f55abc Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 20 Dec 2018 23:12:41 -0500 Subject: Add initial coroutines --- app/build.gradle | 2 + .../com/pitchedapps/frost/facebook/FbCookie.kt | 72 +++++++++++++++------- 2 files changed, 51 insertions(+), 23 deletions(-) (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index 34e57f84..0e2bff04 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -179,6 +179,8 @@ dependencies { //noinspection GradleDependency implementation "ca.allanwang.kau:core-ui:$KAU" + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$COROUTINES" + implementation "org.apache.commons:commons-text:${COMMONS_TEXT}" implementation "com.devbrackets.android:exomedia:${EXOMEDIA}" 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()) } - cookies.forEach { (cookie, callback) -> setCookie(FB_URL_BASE, cookie) { callback.onSuccess(it) } } - Observable.zip(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()) } + cookies.forEach { (cookie, callback) -> setCookie(FB_URL_BASE, cookie) { callback.onSuccess(it) } } + Observable.zip(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) } /** -- cgit v1.2.3