diff options
author | Allan Wang <me@allanwang.ca> | 2017-05-31 17:11:46 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-05-31 17:11:46 -0700 |
commit | 8618670b82641d5fbaec9c333f1290bab429ce27 (patch) | |
tree | 737c9a04f108ea68547eef2db1ae6e96caa64df6 /app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt | |
parent | 9a41937a33539dbfaae4d072361caaec79865c29 (diff) | |
download | frost-8618670b82641d5fbaec9c333f1290bab429ce27.tar.gz frost-8618670b82641d5fbaec9c333f1290bab429ce27.tar.bz2 frost-8618670b82641d5fbaec9c333f1290bab429ce27.zip |
add more cookie handling
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 | 45 |
1 files changed, 32 insertions, 13 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 80fc3b72..3316bb65 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -3,24 +3,30 @@ package com.pitchedapps.frost.facebook import android.webkit.CookieManager import com.pitchedapps.frost.dbflow.FB_URL_BASE import com.pitchedapps.frost.dbflow.loadFbCookie +import com.pitchedapps.frost.dbflow.removeCookie import com.pitchedapps.frost.dbflow.saveFbCookie +import com.pitchedapps.frost.events.WebEvent +import com.pitchedapps.frost.utils.GlideUtils import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs +import org.greenrobot.eventbus.EventBus /** * Created by Allan Wang on 2017-05-30. */ object FbCookie { - var userId: Long = Prefs.userIdDefault var dbCookie: String? = null var webCookie: String? get() = CookieManager.getInstance().getCookie(FB_URL_BASE) - set(value) = CookieManager.getInstance().setCookie(FB_URL_BASE, value) + set(value) { + CookieManager.getInstance().setCookie(FB_URL_BASE, value) + CookieManager.getInstance().flush() + } - fun init() { - userId = Prefs.userId - dbCookie = loadFbCookie()?.cookie + operator fun invoke() { + L.d("User ${Prefs.userId}") + dbCookie = loadFbCookie(Prefs.userId)?.cookie if (dbCookie != null && webCookie == null) { L.d("DbCookie found & WebCookie is null; setting webcookie") webCookie = dbCookie @@ -30,34 +36,47 @@ object FbCookie { private val userMatcher: Regex by lazy { Regex("c_user=([0-9]*);") } fun checkUserId(url: String, cookie: String?) { - if (userId != Prefs.userIdDefault || cookie == null) return + if (Prefs.userId != Prefs.userIdDefault || cookie == null) return L.d("Checking cookie for $url\n\t$cookie") if (!url.contains("facebook") || !cookie.contains(userMatcher)) return val id = userMatcher.find(cookie)?.groups?.get(1)?.value if (id != null) { try { - userId = id.toLong() - save() + save(id.toLong()) } catch (e: NumberFormatException) { //todo send report that id has changed } } } - fun save() { - L.d("New cookie found for $userId") - Prefs.userId = userId + fun save(id: Long) { + L.d("New cookie found for $id") + Prefs.userId = id CookieManager.getInstance().flush() - saveFbCookie() + EventBus.getDefault().post(WebEvent(WebEvent.REFRESH_BASE)) + saveFbCookie(Prefs.userId, webCookie) + GlideUtils.downloadProfile(id) } //TODO reset when new account is added; reset and clear when account is logged out fun reset() { Prefs.userId = Prefs.userIdDefault - userId = Prefs.userIdDefault with(CookieManager.getInstance()) { removeAllCookies(null) flush() } } + + fun switchUser(id: Long) { + val cookie = loadFbCookie(id) ?: return + Prefs.userId = id + dbCookie = cookie.cookie + webCookie = dbCookie + } + + fun logout() { + L.d("Logging out user ${Prefs.userId}") + removeCookie(Prefs.userId) + reset() + } }
\ No newline at end of file |