diff options
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.kt | 31 |
1 files changed, 16 insertions, 15 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 8d827947..fba9c07d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -35,13 +35,15 @@ import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.withContext +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-05-30. * * The following component manages all cookie transfers. */ -object FbCookie { +object FbCookie :KoinComponent { const val COOKIE_DOMAIN = FB_URL_BASE @@ -52,9 +54,8 @@ object FbCookie { inline val webCookie: String? get() = CookieManager.getInstance().getCookie(COOKIE_DOMAIN) - private val cookieDao: CookieDao by lazy { - FrostDatabase.get().cookieDao() - } + private val prefs: Prefs by inject() + private val cookieDao: CookieDao by inject() private suspend fun CookieManager.suspendSetWebCookie(cookie: String?): Boolean { cookie ?: return true @@ -86,14 +87,14 @@ object FbCookie { suspend fun save(id: Long) { L.d { "New cookie found" } - Prefs.userId = id + prefs.userId = id CookieManager.getInstance().flush() - val cookie = CookieEntity(Prefs.userId, null, webCookie) + val cookie = CookieEntity(prefs.userId, null, webCookie) cookieDao.save(cookie) } suspend fun reset() { - Prefs.userId = -1L + prefs.userId = -1L with(CookieManager.getInstance()) { removeAllCookies() flush() @@ -112,7 +113,7 @@ object FbCookie { } withContext(NonCancellable) { L.d { "Switching User" } - Prefs.userId = cookie.id + prefs.userId = cookie.id CookieManager.getInstance().suspendSetWebCookie(cookie.cookie) } } @@ -124,8 +125,8 @@ object FbCookie { suspend fun logout(context: Context) { val cookies = arrayListOf<CookieEntity>() if (context is Activity) - cookies.addAll(context.cookies().filter { it.id != Prefs.userId }) - logout(Prefs.userId) + cookies.addAll(context.cookies().filter { it.id != prefs.userId }) + logout(prefs.userId) context.launchLogin(cookies, true) } @@ -145,13 +146,13 @@ object FbCookie { * When coming back to the main app, switch back to our original account before continuing */ suspend fun switchBackUser() { - if (Prefs.prevId == -1L) return - val prevId = Prefs.prevId - Prefs.prevId = -1L - if (prevId != Prefs.userId) { + 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" } + L._d { "${prefs.userId} to $prevId" } } } } |