aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/build.gradle2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt70
-rw-r--r--gradle.properties2
3 files changed, 56 insertions, 18 deletions
diff --git a/app/build.gradle b/app/build.gradle
index 1b8feda7..ebf3ecf0 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -180,6 +180,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 c6583712..c7931e53 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
@@ -30,30 +30,36 @@ 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
+import kotlin.coroutines.suspendCoroutine
/**
* Created by Allan Wang on 2017-05-30.
*/
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 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() }) {}
+ 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(COOKIE_DOMAIN, cookie) { callback.onSuccess(it) } }
+ Observable.zip<Boolean, Unit>(cookies.map { (_, callback) -> callback.toObservable() }) {}
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
callback?.invoke()
@@ -61,19 +67,47 @@ object FbCookie {
L._d { cookie }
flush()
}
- }
}
}
+ private suspend fun CookieManager.suspendSetWebCookie(cookie: String?): Boolean {
+ cookie ?: return true
+ L.test { "Orig ${webCookie}" }
+ removeAllCookies()
+ 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(COOKIE_DOMAIN, cookie.trim()) {
+ L.test { "Save single $cookie\n\n\t$webCookie" }
+ 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(Dispatchers.Main) {
+ manager.suspendSetWebCookie(dbCookie)
+ }
}
}
@@ -107,7 +141,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 1af54944..792f6ac0 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -23,6 +23,8 @@ ANDROID_GRADLE=3.2.1
# https://github.com/diffplug/spotless/blob/master/plugin-gradle/CHANGES.md
SPOTLESS=3.17.0
+# https://github.com/Kotlin/kotlinx.coroutines/releases
+COROUTINES=1.0.1
# https://github.com/bugsnag/bugsnag-android/releases
BUGSNAG=4.9.3
# https://github.com/bugsnag/bugsnag-android-gradle-plugin/releases