aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt105
1 files changed, 38 insertions, 67 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 2eb37ba4..5683526a 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
@@ -23,19 +23,14 @@ import com.pitchedapps.frost.dbflow.CookieModel
import com.pitchedapps.frost.dbflow.loadFbCookie
import com.pitchedapps.frost.dbflow.removeCookie
import com.pitchedapps.frost.dbflow.saveFbCookie
-import com.pitchedapps.frost.facebook.FbCookie.webCookie
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.cookies
import com.pitchedapps.frost.utils.launchLogin
-import io.reactivex.Observable
-import io.reactivex.android.schedulers.AndroidSchedulers
-import io.reactivex.subjects.SingleSubject
-import kotlinx.coroutines.Deferred
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.GlobalScope
-import kotlinx.coroutines.launch
-import kotlinx.coroutines.yield
+import kotlinx.coroutines.NonCancellable
+import kotlinx.coroutines.async
+import kotlinx.coroutines.awaitAll
+import kotlinx.coroutines.withContext
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@@ -46,7 +41,7 @@ import kotlin.coroutines.suspendCoroutine
*/
object FbCookie {
- const val COOKIE_DOMAIN = FACEBOOK_COM
+ const val COOKIE_DOMAIN = FB_URL_BASE
/**
* Retrieves the facebook cookie if it exists
@@ -55,49 +50,29 @@ object FbCookie {
inline val webCookie: String?
get() = CookieManager.getInstance().getCookie(COOKIE_DOMAIN)
- 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()
- L.d { "Cookies set" }
- 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
+ return withContext(NonCancellable) {
+ removeAllCookies()
+ // Save all cookies regardless of result, then check if all succeeded
+ val result = cookie.split(";")
+ .map { async { setSingleWebCookie(it) } }
+ .awaitAll().all { it }
+ flush()
+ L.d { "Cookies set" }
+ L._d { "Set $cookie\n\tResult $webCookie" }
+ 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)
}
}
@@ -110,67 +85,63 @@ object FbCookie {
saveFbCookie(cookie)
}
- fun reset(callback: () -> Unit) {
+ suspend fun reset() {
Prefs.userId = -1L
with(CookieManager.getInstance()) {
- removeAllCookies {
- flush()
- callback()
- }
+ removeAllCookies()
+ flush()
}
}
- fun switchUser(id: Long, callback: () -> Unit) = switchUser(loadFbCookie(id), callback)
+ suspend fun switchUser(id: Long) = switchUser(loadFbCookie(id))
- fun switchUser(name: String, callback: () -> Unit) = switchUser(loadFbCookie(name), callback)
+ suspend fun switchUser(name: String) = switchUser(loadFbCookie(name))
- fun switchUser(cookie: CookieModel?, callback: () -> Unit) {
+ suspend fun switchUser(cookie: CookieModel?) {
if (cookie == null) {
L.d { "Switching User; null cookie" }
- callback()
return
}
- L.d { "Switching User" }
- Prefs.userId = cookie.id
- CookieManager.getInstance().setWebCookie(cookie.cookie, callback)
+ withContext(NonCancellable) {
+ L.d { "Switching User" }
+ Prefs.userId = cookie.id
+ CookieManager.getInstance().suspendSetWebCookie(cookie.cookie)
+ }
}
/**
* Helper function to remove the current cookies
* and launch the proper login page
*/
- fun logout(context: Context) {
+ suspend fun logout(context: Context) {
val cookies = arrayListOf<CookieModel>()
if (context is Activity)
cookies.addAll(context.cookies().filter { it.id != Prefs.userId })
- logout(Prefs.userId) {
- context.launchLogin(cookies, true)
- }
+ logout(Prefs.userId)
+ context.launchLogin(cookies, true)
}
/**
* Clear the cookies of the given id
*/
- fun logout(id: Long, callback: () -> Unit) {
+ suspend fun logout(id: Long) {
L.d { "Logging out user" }
removeCookie(id)
- reset(callback)
+ reset()
}
/**
* Notifications may come from different accounts, and we need to switch the cookies to load them
* When coming back to the main app, switch back to our original account before continuing
*/
- fun switchBackUser(callback: () -> Unit) {
- if (Prefs.prevId == -1L) return callback()
+ suspend fun switchBackUser() {
+ if (Prefs.prevId == -1L) return
val prevId = Prefs.prevId
Prefs.prevId = -1L
if (prevId != Prefs.userId) {
- switchUser(prevId) {
- L.d { "Switch back user" }
- L._d { "${Prefs.userId} to $prevId" }
- callback()
- }
- } else callback()
+ switchUser(prevId)
+ L.d { "Switch back user" }
+ L._d { "${Prefs.userId} to $prevId" }
+ }
}
}