From c53e343f039b65c0aee2ee316b8c844a5b596bb8 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 30 May 2017 19:55:57 -0700 Subject: working tab db and initial cookie map --- .../kotlin/com/pitchedapps/frost/MainActivity.kt | 10 ++++- .../com/pitchedapps/frost/dbflow/CookiesDb.kt | 2 +- .../com/pitchedapps/frost/dbflow/FbTabsDb.kt | 32 +++++++-------- .../com/pitchedapps/frost/facebook/CookieMap.kt | 45 ++++++++++++++++++++++ .../kotlin/com/pitchedapps/frost/utils/DbUtils.kt | 37 ++++++++++-------- .../kotlin/com/pitchedapps/frost/utils/Prefs.kt | 7 +++- .../com/pitchedapps/frost/views/FrostWebView.kt | 1 + 7 files changed, 97 insertions(+), 37 deletions(-) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/CookieMap.kt (limited to 'app/src/main/kotlin/com/pitchedapps') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt index fae83580..5c7c3a9e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt @@ -15,6 +15,7 @@ import android.view.MenuItem import butterknife.ButterKnife import com.pitchedapps.frost.dbflow.FbTab import com.pitchedapps.frost.dbflow.loadFbTabs +import com.pitchedapps.frost.dbflow.saveAsync import com.pitchedapps.frost.facebook.retro.FrostApi.frostApi import com.pitchedapps.frost.facebook.retro.enqueueFrost import com.pitchedapps.frost.fragments.BaseFragment @@ -74,7 +75,14 @@ class MainActivity : AppCompatActivity(), KeyPairObservable { finish() } R.id.action_changelog -> Changelog.show(this) - R.id.action_call -> frostApi.me().enqueueFrost { call, response -> L.e(response.toString())} + R.id.action_call -> frostApi.me().enqueueFrost { _, response -> L.e(response.toString())} + R.id.action_db -> adapter.pages.saveAsync(this) + R.id.action_restart -> { + finish(); + overridePendingTransition(0, 0); //No transitions + startActivity(intent); + overridePendingTransition(0, 0); + } else -> return super.onOptionsItemSelected(item) } return true diff --git a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt index ab39118a..38d55a17 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt @@ -57,4 +57,4 @@ data class CookieModel(@PrimaryKey var name: String, //} @Table(database = CookiesDb::class) -data class Cookies(@PrimaryKey var url: String = "", @ForeignKey var cookie: CookieModel = CookieModel()) : BaseModel(), Serializable \ No newline at end of file +data class Cookies(@PrimaryKey var url: String = "", @ForeignKey var cookie: CookieModel = CookieModel()) \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/FbTabsDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/FbTabsDb.kt index 05dac758..662b97e8 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/FbTabsDb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/FbTabsDb.kt @@ -8,14 +8,14 @@ import com.mikepenz.iconics.typeface.IIcon import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic import com.pitchedapps.frost.R import com.pitchedapps.frost.facebook.FB_KEY +import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.replace import com.raizlabs.android.dbflow.annotation.Database import com.raizlabs.android.dbflow.annotation.ForeignKey import com.raizlabs.android.dbflow.annotation.PrimaryKey import com.raizlabs.android.dbflow.annotation.Table import com.raizlabs.android.dbflow.kotlinextensions.from import com.raizlabs.android.dbflow.sql.language.SQLite -import com.raizlabs.android.dbflow.structure.BaseModel -import java.io.Serializable /** * Created by Allan Wang on 2017-05-30. @@ -31,21 +31,20 @@ data class FbTab(val title: String, val icon: IIcon, val url: String) @Table(database = FbTabsDb::class, allFields = true) data class FbTabModel( - var title: String = "Home", - @ForeignKey var icon: IIconModel = IIconModel(), - @PrimaryKey var url: String = "" -) : BaseModel(), Serializable { + var title: String = "", + @ForeignKey(saveForeignKeyModel = true, deleteForeignKeyModel = false) var icon: IIconModel = IIconModel(), + @PrimaryKey var url: String = "") { constructor(fbTab: FbTab) : this(fbTab.title, IIconModel(fbTab.icon), fbTab.url) fun toFbTab() = FbTab(title, icon.toIIcon(), url) } @Table(database = FbTabsDb::class, allFields = true) -data class IIconModel(var type: Int = -1, @PrimaryKey var name: String = "") : BaseModel(), Serializable { +data class IIconModel(var type: Int = -1, @PrimaryKey var name: String = "") { constructor(icon: IIcon) : this(when (icon) { - is CommunityMaterial -> 0 - is GoogleMaterial -> 1 - is MaterialDesignIconic -> 2 + is CommunityMaterial.Icon -> 0 + is GoogleMaterial.Icon -> 1 + is MaterialDesignIconic.Icon -> 2 else -> -1 }, icon.toString()) @@ -73,13 +72,14 @@ enum class FbUrl(@StringRes val titleId: Int, val icon: IIcon, val url: String) //SEARCH("https://touch.facebook.com/search"), fun loadFbTabs(c: Context): List { - val tabs = SQLite.select() - .from(FbTabModel::class) - .queryList() - if (tabs.isNotEmpty()) return tabs.map { it.toFbTab() } + val tabs: List? = SQLite.select().from(FbTabModel::class).queryList() + if (tabs?.isNotEmpty() ?: false) return tabs!!.map { it.toFbTab() } + L.e("No tabs; loading default") return listOf(FbUrl.FEED, FbUrl.MESSAGES, FbUrl.FRIENDS, FbUrl.NOTIFICATIONS).map { it.tabInfo(c) } } -fun List.saveAsync() { - +fun List.saveAsync(c: Context) { + map { FbTabModel(it) }.replace(c, FbTabsDb.NAME, { + L.e("Saved successfully") + }) } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/CookieMap.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/CookieMap.kt new file mode 100644 index 00000000..96b1f2de --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/CookieMap.kt @@ -0,0 +1,45 @@ +package com.pitchedapps.frost.facebook + +import android.webkit.CookieManager +import com.pitchedapps.frost.utils.Prefs + +/** + * Created by Allan Wang on 2017-05-30. + */ +object CookieMap { + + var userId: Int = -1 + private val userMatcher = "c_user=([0-9]*);" + private val map = HashMap() + + operator fun get(key: String) = map[key] + + operator fun set(key: String, value: String) { + map[key] = value + } + + fun put(url: String, cookie: String) { + map.put(url, cookie) + checkUserId(url, cookie) + } + + fun checkUserId(url: String, cookie: String) { + if (userId != -1) return + if (!url.contains("facebook") || !cookie.contains(userMatcher)) return + val id = Regex(userMatcher).find(cookie)?.value + if (id != null) { + userId = id.toInt() + save() + } + } + + fun save() { + Prefs.userId = userId + CookieManager.getInstance().flush() + + } + + fun reset() { + + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt index dc16f6cc..5b10e581 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt @@ -3,31 +3,34 @@ package com.pitchedapps.frost.utils import android.content.Context import com.raizlabs.android.dbflow.config.FlowManager import com.raizlabs.android.dbflow.kotlinextensions.processInTransactionAsync -import com.raizlabs.android.dbflow.structure.BaseModel +import com.raizlabs.android.dbflow.kotlinextensions.save import com.raizlabs.android.dbflow.structure.database.transaction.Transaction /** * Created by Allan Wang on 2017-05-30. */ + object DbUtils { fun db(name: String) = FlowManager.getDatabase(name) fun dbName(name: String) = "$name.db" + fun deleteDatabase(c: Context, name: String) = c.deleteDatabase(dbName(name)) - inline fun replace( - context: Context, dbName: String, type: Class, data: List, - crossinline callback: ((successful: Boolean) -> Unit)) { - db(dbName).reset(context) - data.processInTransactionAsync({ - t, databaseWrapper -> - t.save(databaseWrapper) - }, - Transaction.Success { - callback.invoke(true) - }, - Transaction.Error { _, error -> - L.e(error.message ?: "DbReplace error") - callback.invoke(false) - }) - } } + +inline fun List.replace(context: Context, dbName: String, + crossinline callback: ((successful: Boolean) -> Unit)) { + L.d("Replacing $dbName.db") + DbUtils.db(dbName).reset(context) + this.processInTransactionAsync({ + t, databaseWrapper -> + t.save(databaseWrapper) + }, + Transaction.Success { + callback.invoke(true) + }, + Transaction.Error { _, error -> + L.e(error.message ?: "DbReplace error") + callback.invoke(false) + }) +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt index 710da845..2ac58113 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt @@ -9,11 +9,10 @@ import android.content.SharedPreferences private val PREFERENCE_NAME = "${com.pitchedapps.frost.BuildConfig.APPLICATION_ID}.prefs" private val LAST_ACTIVE = "last_active" +private val USER_ID = "user_id" object Prefs { - val prefs: Prefs by lazy { this } - lateinit private var c: Context operator fun invoke(c: Context) { this.c = c @@ -26,6 +25,10 @@ object Prefs { get() = sp.getLong(LAST_ACTIVE, -1) set(value) = set(LAST_ACTIVE, System.currentTimeMillis()) + var userId: Int + get() = sp.getInt(USER_ID, -1) + set(value) = set(USER_ID, value) + private fun set(key: String, value: Boolean) = sp.edit().putBoolean(key, value).apply() private fun set(key: String, value: Int) = sp.edit().putInt(key, value).apply() private fun set(key: String, value: Long) = sp.edit().putLong(key, value).apply() diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt index a59fa3b1..c529b224 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt @@ -67,6 +67,7 @@ class FrostWebView @JvmOverloads constructor( val cookie = CookieManager.getInstance().getCookie(url) L.d("Loaded $url") L.d("Cookie $cookie") + CookieManager.getInstance().flush() } }) } -- cgit v1.2.3