From 35185958b077880465696d686bd797895cd3ebd4 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 3 Jun 2017 13:22:06 -0700 Subject: setup login activity --- .../com/pitchedapps/frost/facebook/FbCookie.kt | 40 ++++++---------------- .../pitchedapps/frost/facebook/UsernameFetcher.kt | 19 +++++----- 2 files changed, 18 insertions(+), 41 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook') 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 7829998f..038eb1a6 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -32,57 +32,37 @@ object FbCookie { } } - private val userMatcher: Regex by lazy { Regex("c_user=([0-9]*);") } - - fun hasLoggedIn(url: String, cookie: String?):Boolean { - if (cookie == null || !url.contains("facebook") || !cookie.contains(userMatcher)) return false - L.d("Checking cookie for $url\n\t$cookie") - val id = userMatcher.find(cookie)?.groups?.get(1)?.value - if (id != null) { - try { - save(id.toLong(), -1) - return true - } catch (e: NumberFormatException) { - //todo send report that id has changed - } - } - return false - } - - fun save(id: Long, sender: Int) { + fun save(id: Long) { L.d("New cookie found for $id") Prefs.userId = id CookieManager.getInstance().flush() val cookie = CookieModel(Prefs.userId, "", webCookie) - EventBus.getDefault().post(FbAccountEvent(cookie, sender, FbAccountEvent.FLAG_NEW)) saveFbCookie(cookie) } - //TODO reset when new account is added; reset and clear when account is logged out - fun reset(loggedOut: Boolean = false, sender: Int) { + fun reset() { Prefs.userId = Prefs.userIdDefault with(CookieManager.getInstance()) { removeAllCookies(null) flush() } - EventBus.getDefault().post(FbAccountEvent(CookieModel(), sender, if (loggedOut) FbAccountEvent.FLAG_LOGOUT else FbAccountEvent.FLAG_RESET)) } - fun switchUser(id: Long, sender: Int) = switchUser(loadFbCookie(id), sender) + fun switchUser(id: Long) = switchUser(loadFbCookie(id)) - fun switchUser(name: String, sender: Int) = switchUser(loadFbCookie(name), sender) + fun switchUser(name: String) = switchUser(loadFbCookie(name)) - fun switchUser(cookie: CookieModel?, sender: Int) { + fun switchUser(cookie: CookieModel?) { if (cookie == null) return Prefs.userId = cookie.id dbCookie = cookie.cookie webCookie = dbCookie - EventBus.getDefault().post(FbAccountEvent(cookie, sender, FbAccountEvent.FLAG_SWITCH)) + //TODO add webview refresh event } - fun logout(sender: Int) { - L.d("Logging out user ${Prefs.userId}") - removeCookie(Prefs.userId) - reset(true, sender) + fun logout(id:Long) { + L.d("Logging out user $id") + removeCookie(id) + reset() } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/UsernameFetcher.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/UsernameFetcher.kt index da244ba3..5bc58fa9 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/UsernameFetcher.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/UsernameFetcher.kt @@ -2,9 +2,8 @@ package com.pitchedapps.frost.facebook import com.pitchedapps.frost.dbflow.CookieModel import com.pitchedapps.frost.dbflow.saveFbCookie -import com.pitchedapps.frost.events.FbAccountEvent import com.pitchedapps.frost.utils.L -import org.greenrobot.eventbus.EventBus +import io.reactivex.subjects.SingleSubject import org.jsoup.Jsoup import kotlin.concurrent.thread @@ -13,22 +12,20 @@ import kotlin.concurrent.thread */ object UsernameFetcher { - fun fetch(data: CookieModel, sender: Int) { + fun fetch(data: CookieModel, callback: SingleSubject) { thread { + var name = "" try { - val title = Jsoup.connect(FbTab.PROFILE.url) + name = Jsoup.connect(FbTab.PROFILE.url) .cookie(FACEBOOK_COM, data.cookie) .get().title() - L.d("User name found: $title") - data.name = title + L.d("User name found: $name") } catch (e: Exception) { L.e("User name fetching failed: ${e.message}") - data.name = "" } finally { - if (data.name != null) { - saveFbCookie(data) - EventBus.getDefault().post(FbAccountEvent(data, sender, FbAccountEvent.FLAG_USER_NAME)) - } + data.name = name + saveFbCookie(data) + callback.onSuccess(name) } } } -- cgit v1.2.3