aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-20 23:12:41 -0500
committerAllan Wang <me@allanwang.ca>2018-12-20 23:12:41 -0500
commit46fb61e53327c6eb1ebc3bfced956f3e05f55abc (patch)
tree089af862fca40af108801d2f5f0de81b05326d90 /app
parent813be84a518ca39bba91b26b43b3db56a9d186c7 (diff)
downloadfrost-46fb61e53327c6eb1ebc3bfced956f3e05f55abc.tar.gz
frost-46fb61e53327c6eb1ebc3bfced956f3e05f55abc.tar.bz2
frost-46fb61e53327c6eb1ebc3bfced956f3e05f55abc.zip
Add initial coroutines
Diffstat (limited to 'app')
-rw-r--r--app/build.gradle2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt72
2 files changed, 51 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)
}
/**