diff options
-rw-r--r-- | app/build.gradle | 2 | ||||
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt | 72 | ||||
-rw-r--r-- | gradle.properties | 2 |
3 files changed, 53 insertions, 23 deletions
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<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) } /** diff --git a/gradle.properties b/gradle.properties index 825b2b33..08365da7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,6 +17,8 @@ APP_GROUP=com.pitchedapps KAU=572d470 KOTLIN=1.3.10 +# https://github.com/Kotlin/kotlinx.coroutines/releases +COROUTINES=1.0.1 # https://github.com/bugsnag/bugsnag-android/releases BUGSNAG=4.9.2 # https://github.com/bugsnag/bugsnag-android-gradle-plugin/releases |