From c917dc13dabe7781a097383ae89f2d00f32fffcb Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 5 Mar 2019 18:31:47 -0500 Subject: Create initial room models --- .../com/pitchedapps/frost/db/FrostDatabaseTest.kt | 46 ++++++++ app/src/main/AndroidManifest.xml | 1 - .../main/kotlin/com/pitchedapps/frost/FrostApp.kt | 14 ++- .../kotlin/com/pitchedapps/frost/StartActivity.kt | 4 +- .../frost/activities/BaseMainActivity.kt | 6 +- .../pitchedapps/frost/activities/LoginActivity.kt | 6 +- .../frost/activities/TabCustomizerActivity.kt | 6 +- .../kotlin/com/pitchedapps/frost/db/CookiesDb.kt | 126 +++++++++++++++++++++ .../kotlin/com/pitchedapps/frost/db/Database.kt | 62 ++++++++++ .../kotlin/com/pitchedapps/frost/db/DbUtils.kt | 38 +++++++ .../kotlin/com/pitchedapps/frost/db/FbTabsDb.kt | 108 ++++++++++++++++++ .../com/pitchedapps/frost/db/NotificationDb.kt | 72 ++++++++++++ .../com/pitchedapps/frost/dbflow/CookiesDb.kt | 92 --------------- .../kotlin/com/pitchedapps/frost/dbflow/DbUtils.kt | 39 ------- .../com/pitchedapps/frost/dbflow/FbTabsDb.kt | 58 ---------- .../com/pitchedapps/frost/dbflow/NotificationDb.kt | 72 ------------ .../com/pitchedapps/frost/facebook/FbCookie.kt | 8 +- .../frost/facebook/parsers/FrostParser.kt | 2 +- .../frost/facebook/parsers/MessageParser.kt | 2 +- .../frost/facebook/parsers/NotifParser.kt | 2 +- .../frost/services/FrostNotifications.kt | 6 +- .../frost/services/NotificationService.kt | 4 +- .../pitchedapps/frost/settings/Notifications.kt | 4 +- .../com/pitchedapps/frost/utils/Downloader.kt | 2 +- .../kotlin/com/pitchedapps/frost/utils/Utils.kt | 2 +- .../com/pitchedapps/frost/views/AccountItem.kt | 2 +- .../kotlin/com/pitchedapps/frost/web/FrostJSI.kt | 2 +- .../com/pitchedapps/frost/web/LoginWebView.kt | 2 +- .../1.json | 45 ++++++++ .../1.json | 39 +++++++ 30 files changed, 577 insertions(+), 295 deletions(-) create mode 100644 app/src/androidTest/kotlin/com/pitchedapps/frost/db/FrostDatabaseTest.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/db/CookiesDb.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/db/DbUtils.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/dbflow/DbUtils.kt delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/dbflow/FbTabsDb.kt delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/dbflow/NotificationDb.kt create mode 100644 app/src/schemas/com.pitchedapps.frost.db.FrostPrivateDatabase/1.json create mode 100644 app/src/schemas/com.pitchedapps.frost.db.FrostPublicDatabase/1.json (limited to 'app/src') diff --git a/app/src/androidTest/kotlin/com/pitchedapps/frost/db/FrostDatabaseTest.kt b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/FrostDatabaseTest.kt new file mode 100644 index 00000000..6b68e804 --- /dev/null +++ b/app/src/androidTest/kotlin/com/pitchedapps/frost/db/FrostDatabaseTest.kt @@ -0,0 +1,46 @@ +package com.pitchedapps.frost.db + +import android.content.Context +import androidx.room.Room +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import kotlinx.coroutines.runBlocking +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import kotlin.test.assertEquals + +@RunWith(AndroidJUnit4::class) +class FrostDatabaseTest { + + private lateinit var db: FrostDatabase + + @Before + fun before() { + + val context = ApplicationProvider.getApplicationContext() + val privateDb = Room.inMemoryDatabaseBuilder( + context, FrostPrivateDatabase::class.java + ).build() + val publicDb = Room.inMemoryDatabaseBuilder( + context, FrostPublicDatabase::class.java + ).build() + db = FrostDatabase(privateDb, publicDb) + } + + @After + fun after() { + db.close() + } + + @Test + fun basic() { + val cookie = CookieEntity(id = 1234L, name = "testName", cookie = "testCookie") + runBlocking { + db.cookieDao().insertCookie(cookie) + val cookies = db.cookieDao().selectAll() + assertEquals(listOf(cookie), cookies, "Cookie mismatch") + } + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ad1fcbdc..68e84e4d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,7 +17,6 @@ . + */ +package com.pitchedapps.frost.db + +import android.os.Parcelable +import androidx.room.Dao +import androidx.room.Entity +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import com.pitchedapps.frost.utils.L +import com.raizlabs.android.dbflow.annotation.ConflictAction +import com.raizlabs.android.dbflow.annotation.Database +import com.raizlabs.android.dbflow.annotation.PrimaryKey +import com.raizlabs.android.dbflow.annotation.Table +import com.raizlabs.android.dbflow.kotlinextensions.async +import com.raizlabs.android.dbflow.kotlinextensions.delete +import com.raizlabs.android.dbflow.kotlinextensions.eq +import com.raizlabs.android.dbflow.kotlinextensions.from +import com.raizlabs.android.dbflow.kotlinextensions.save +import com.raizlabs.android.dbflow.kotlinextensions.select +import com.raizlabs.android.dbflow.kotlinextensions.where +import com.raizlabs.android.dbflow.structure.BaseModel +import kotlinx.android.parcel.Parcelize +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +/** + * Created by Allan Wang on 2017-05-30. + */ + +@Entity(tableName = "cookies") +@Parcelize +data class CookieEntity( + @androidx.room.PrimaryKey + var id: Long, + var name: String, + var cookie: String +) : Parcelable { + override fun toString(): String = "CookieModel(${hashCode()})" + + fun toSensitiveString(): String = "CookieModel(id=$id, name=$name, cookie=$cookie)" +} + +@Dao +interface CookieDao { + + @Query("SELECT * FROM cookies") + suspend fun selectAll(): List + + @Query("SELECT * FROM cookies WHERE id = :id") + suspend fun selectById(id: Long): CookieEntity? + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insertCookie(cookie: CookieEntity) + + @Query("DELETE FROM cookies WHERE id = :id") + suspend fun deleteById(id: Long) +} + +@Database(version = CookiesDb.VERSION) +object CookiesDb { + const val NAME = "Cookies" + const val VERSION = 2 +} + +@Parcelize +@Table(database = CookiesDb::class, allFields = true, primaryKeyConflict = ConflictAction.REPLACE) +data class CookieModel(@PrimaryKey var id: Long = -1L, var name: String? = null, var cookie: String? = null) : + BaseModel(), Parcelable { + + override fun toString(): String = "CookieModel(${hashCode()})" + + fun toSensitiveString(): String = "CookieModel(id=$id, name=$name, cookie=$cookie)" +} + +fun loadFbCookie(id: Long): CookieModel? = + (select from CookieModel::class where (CookieModel_Table.id eq id)).querySingle() + +fun loadFbCookie(name: String): CookieModel? = + (select from CookieModel::class where (CookieModel_Table.name eq name)).querySingle() + +/** + * Loads cookies sorted by name + */ +fun loadFbCookiesAsync(callback: (cookies: List) -> Unit) { + (select from CookieModel::class).orderBy(CookieModel_Table.name, true).async() + .queryListResultCallback { _, tResult -> callback(tResult) }.execute() +} + +fun loadFbCookiesSync(): List = + (select from CookieModel::class).orderBy(CookieModel_Table.name, true).queryList() + +// TODO temp method until dbflow supports coroutines +suspend fun loadFbCookiesSuspend(): List = withContext(Dispatchers.IO) { + loadFbCookiesSync() +} + +inline fun saveFbCookie(cookie: CookieModel, crossinline callback: (() -> Unit) = {}) { + cookie.async save { + L.d { "Fb cookie saved" } + L._d { cookie.toSensitiveString() } + callback() + } +} + +fun removeCookie(id: Long) { + loadFbCookie(id)?.async?.delete { + L.d { "Fb cookie deleted" } + L._d { id } + } +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt new file mode 100644 index 00000000..34de5e07 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt @@ -0,0 +1,62 @@ +package com.pitchedapps.frost.db + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import androidx.room.TypeConverters + +interface FrostPrivateDao { + fun cookieDao(): CookieDao +} + +@Database(entities = [CookieEntity::class], version = 1, exportSchema = true) +abstract class FrostPrivateDatabase : RoomDatabase(), FrostPrivateDao { + companion object { + const val DATABASE_NAME = "frost-priv-db" + } +} + +interface FrostPublicDao { + fun tabDao(): FbTabDao +} + +@Database(entities = [FbTabEntity::class], version = 1, exportSchema = true) +@TypeConverters(FbItemConverter::class) +abstract class FrostPublicDatabase : RoomDatabase(), FrostPublicDao { + companion object { + const val DATABASE_NAME = "frost-db" + } +} + +interface FrostDao : FrostPrivateDao, FrostPublicDao { + fun close() +} + +/** + * Composition of all database interfaces + */ +class FrostDatabase(private val privateDb: FrostPrivateDatabase, private val publicDb: FrostPublicDatabase) : + FrostDao, + FrostPrivateDao by privateDb, + FrostPublicDao by publicDb { + + override fun close() { + privateDb.close() + publicDb.close() + } + + companion object { + fun create(context: Context): FrostDatabase { + val privateDb = Room.databaseBuilder( + context, FrostPrivateDatabase::class.java, + FrostPrivateDatabase.DATABASE_NAME + ).build() + val publicDb = Room.databaseBuilder( + context, FrostPublicDatabase::class.java, + FrostPublicDatabase.DATABASE_NAME + ).build() + return FrostDatabase(privateDb, publicDb) + } + } +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/DbUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/DbUtils.kt new file mode 100644 index 00000000..03aac059 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/db/DbUtils.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2018 Allan Wang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.pitchedapps.frost.db + +import android.content.Context +import com.pitchedapps.frost.utils.L +import com.raizlabs.android.dbflow.config.FlowManager +import com.raizlabs.android.dbflow.structure.database.transaction.FastStoreModelTransaction + +/** + * 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 List.replace(dbName: String) { + L.d { "Replacing $dbName.db" } + DbUtils.db(dbName).reset() + FastStoreModelTransaction.saveBuilder(FlowManager.getModelAdapter(T::class.java)).addAll(this).build() +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt new file mode 100644 index 00000000..2e2a9d62 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt @@ -0,0 +1,108 @@ +/* + * Copyright 2018 Allan Wang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.pitchedapps.frost.db + +import androidx.room.Dao +import androidx.room.Entity +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import androidx.room.Transaction +import com.pitchedapps.frost.facebook.FbItem +import com.pitchedapps.frost.facebook.defaultTabs +import com.pitchedapps.frost.utils.L +import com.raizlabs.android.dbflow.annotation.Database +import com.raizlabs.android.dbflow.annotation.PrimaryKey +import com.raizlabs.android.dbflow.annotation.Table +import com.raizlabs.android.dbflow.kotlinextensions.database +import com.raizlabs.android.dbflow.kotlinextensions.fastSave +import com.raizlabs.android.dbflow.kotlinextensions.from +import com.raizlabs.android.dbflow.kotlinextensions.select +import com.raizlabs.android.dbflow.structure.BaseModel + +/** + * Created by Allan Wang on 2017-05-30. + */ + +@Entity(tableName = "tabs") +data class FbTabEntity(@androidx.room.PrimaryKey var position: Int, var tab: FbItem) + +@Dao +interface FbTabDao { + + @Query("SELECT * FROM tabs ORDER BY position ASC") + suspend fun _selectAll(): List + + @Query("DELETE FROM tabs") + suspend fun _deleteAll() + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun _insertAll(items: List) + + @Transaction + suspend fun _save(items: List) { + _deleteAll() + _insertAll(items) + } + +// suspend fun save(items: List) { +// _save((items.takeIf { it.isNotEmpty() } ?: defaultTabs()).mapIndexed { index, fbItem -> +// FbTabEntity( +// index, +// fbItem +// ) +// }) +// } +// +// suspend fun selectAll(): List = _selectAll().map { it.tab }.takeIf { it.isNotEmpty() } ?: defaultTabs() +} + +object FbItemConverter { + @androidx.room.TypeConverter + @JvmStatic + fun fromItem(item: FbItem): String = item.name + + @androidx.room.TypeConverter + @JvmStatic + fun toItem(value: String): FbItem = FbItem.valueOf(value) +} + +const val TAB_COUNT = 4 + +@Database(version = FbTabsDb.VERSION) +object FbTabsDb { + const val NAME = "FrostTabs" + const val VERSION = 1 +} + +@Table(database = FbTabsDb::class, allFields = true) +data class FbTabModel(@PrimaryKey var position: Int = -1, var tab: FbItem = FbItem.FEED) : BaseModel() + +/** + * Load tabs synchronously + * Note that tab length should never be a big number anyways + */ +fun loadFbTabs(): List { + val tabs: List? = (select from (FbTabModel::class)).orderBy(FbTabModel_Table.position, true).queryList() + if (tabs?.size == TAB_COUNT) return tabs.map(FbTabModel::tab) + L.d { "No tabs (${tabs?.size}); loading default" } + return defaultTabs() +} + +fun List.save() { + database().beginTransactionAsync(mapIndexed(::FbTabModel).fastSave().build()).execute() +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt new file mode 100644 index 00000000..5b501792 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt @@ -0,0 +1,72 @@ +/* + * Copyright 2018 Allan Wang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.pitchedapps.frost.db + +import com.pitchedapps.frost.utils.L +import com.raizlabs.android.dbflow.annotation.ConflictAction +import com.raizlabs.android.dbflow.annotation.Database +import com.raizlabs.android.dbflow.annotation.Migration +import com.raizlabs.android.dbflow.annotation.PrimaryKey +import com.raizlabs.android.dbflow.annotation.Table +import com.raizlabs.android.dbflow.kotlinextensions.async +import com.raizlabs.android.dbflow.kotlinextensions.eq +import com.raizlabs.android.dbflow.kotlinextensions.from +import com.raizlabs.android.dbflow.kotlinextensions.save +import com.raizlabs.android.dbflow.kotlinextensions.select +import com.raizlabs.android.dbflow.kotlinextensions.where +import com.raizlabs.android.dbflow.sql.SQLiteType +import com.raizlabs.android.dbflow.sql.migration.AlterTableMigration +import com.raizlabs.android.dbflow.structure.BaseModel + +/** + * Created by Allan Wang on 2017-05-30. + */ + +@Database(version = NotificationDb.VERSION) +object NotificationDb { + const val NAME = "Notifications" + const val VERSION = 2 +} + +@Migration(version = 2, database = NotificationDb::class) +class NotificationMigration2(modelClass: Class) : + AlterTableMigration(modelClass) { + override fun onPreMigrate() { + super.onPreMigrate() + addColumn(SQLiteType.INTEGER, "epochIm") + L.d { "Added column" } + } +} + +@Table(database = NotificationDb::class, allFields = true, primaryKeyConflict = ConflictAction.REPLACE) +data class NotificationModel( + @PrimaryKey var id: Long = -1L, + var epoch: Long = -1L, + var epochIm: Long = -1L +) : BaseModel() + +fun lastNotificationTime(id: Long): NotificationModel = + (select from NotificationModel::class where (NotificationModel_Table.id eq id)).querySingle() + ?: NotificationModel(id = id) + +fun saveNotificationTime(notificationModel: NotificationModel, callback: (() -> Unit)? = null) { + notificationModel.async save { + L.d { "Fb notification model saved" } + L._d { notificationModel } + callback?.invoke() + } +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt deleted file mode 100644 index 8411b8d7..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.pitchedapps.frost.dbflow - -import android.os.Parcelable -import com.pitchedapps.frost.utils.L -import com.raizlabs.android.dbflow.annotation.ConflictAction -import com.raizlabs.android.dbflow.annotation.Database -import com.raizlabs.android.dbflow.annotation.PrimaryKey -import com.raizlabs.android.dbflow.annotation.Table -import com.raizlabs.android.dbflow.kotlinextensions.async -import com.raizlabs.android.dbflow.kotlinextensions.delete -import com.raizlabs.android.dbflow.kotlinextensions.eq -import com.raizlabs.android.dbflow.kotlinextensions.from -import com.raizlabs.android.dbflow.kotlinextensions.save -import com.raizlabs.android.dbflow.kotlinextensions.select -import com.raizlabs.android.dbflow.kotlinextensions.where -import com.raizlabs.android.dbflow.structure.BaseModel -import kotlinx.android.parcel.Parcelize -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext - -/** - * Created by Allan Wang on 2017-05-30. - */ - -@Database(version = CookiesDb.VERSION) -object CookiesDb { - const val NAME = "Cookies" - const val VERSION = 2 -} - -@Parcelize -@Table(database = CookiesDb::class, allFields = true, primaryKeyConflict = ConflictAction.REPLACE) -data class CookieModel(@PrimaryKey var id: Long = -1L, var name: String? = null, var cookie: String? = null) : - BaseModel(), Parcelable { - - override fun toString(): String = "CookieModel(${hashCode()})" - - fun toSensitiveString(): String = "CookieModel(id=$id, name=$name, cookie=$cookie)" -} - -fun loadFbCookie(id: Long): CookieModel? = - (select from CookieModel::class where (CookieModel_Table.id eq id)).querySingle() - -fun loadFbCookie(name: String): CookieModel? = - (select from CookieModel::class where (CookieModel_Table.name eq name)).querySingle() - -/** - * Loads cookies sorted by name - */ -fun loadFbCookiesAsync(callback: (cookies: List) -> Unit) { - (select from CookieModel::class).orderBy(CookieModel_Table.name, true).async() - .queryListResultCallback { _, tResult -> callback(tResult) }.execute() -} - -fun loadFbCookiesSync(): List = - (select from CookieModel::class).orderBy(CookieModel_Table.name, true).queryList() - -// TODO temp method until dbflow supports coroutines -suspend fun loadFbCookiesSuspend(): List = withContext(Dispatchers.IO) { - loadFbCookiesSync() -} - -inline fun saveFbCookie(cookie: CookieModel, crossinline callback: (() -> Unit) = {}) { - cookie.async save { - L.d { "Fb cookie saved" } - L._d { cookie.toSensitiveString() } - callback() - } -} - -fun removeCookie(id: Long) { - loadFbCookie(id)?.async?.delete { - L.d { "Fb cookie deleted" } - L._d { id } - } -} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/DbUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/DbUtils.kt deleted file mode 100644 index 740fef62..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/DbUtils.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.pitchedapps.frost.dbflow - -import android.content.Context -import com.pitchedapps.frost.utils.L -import com.raizlabs.android.dbflow.config.FlowManager -import com.raizlabs.android.dbflow.structure.database.transaction.FastStoreModelTransaction - -/** - * 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 List.replace(dbName: String) { - L.d { "Replacing $dbName.db" } - DbUtils.db(dbName).reset() - FastStoreModelTransaction.saveBuilder(FlowManager.getModelAdapter(T::class.java)).addAll(this).build() -} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/FbTabsDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/FbTabsDb.kt deleted file mode 100644 index 9d330169..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/FbTabsDb.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.pitchedapps.frost.dbflow - -import com.pitchedapps.frost.facebook.FbItem -import com.pitchedapps.frost.facebook.defaultTabs -import com.pitchedapps.frost.utils.L -import com.raizlabs.android.dbflow.annotation.Database -import com.raizlabs.android.dbflow.annotation.PrimaryKey -import com.raizlabs.android.dbflow.annotation.Table -import com.raizlabs.android.dbflow.kotlinextensions.database -import com.raizlabs.android.dbflow.kotlinextensions.fastSave -import com.raizlabs.android.dbflow.kotlinextensions.from -import com.raizlabs.android.dbflow.kotlinextensions.select -import com.raizlabs.android.dbflow.structure.BaseModel - -/** - * Created by Allan Wang on 2017-05-30. - */ -const val TAB_COUNT = 4 - -@Database(version = FbTabsDb.VERSION) -object FbTabsDb { - const val NAME = "FrostTabs" - const val VERSION = 1 -} - -@Table(database = FbTabsDb::class, allFields = true) -data class FbTabModel(@PrimaryKey var position: Int = -1, var tab: FbItem = FbItem.FEED) : BaseModel() - -/** - * Load tabs synchronously - * Note that tab length should never be a big number anyways - */ -fun loadFbTabs(): List { - val tabs: List? = (select from (FbTabModel::class)).orderBy(FbTabModel_Table.position, true).queryList() - if (tabs?.size == TAB_COUNT) return tabs.map(FbTabModel::tab) - L.d { "No tabs (${tabs?.size}); loading default" } - return defaultTabs() -} - -fun List.save() { - database().beginTransactionAsync(mapIndexed(::FbTabModel).fastSave().build()).execute() -} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/NotificationDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/NotificationDb.kt deleted file mode 100644 index a054d95e..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/NotificationDb.kt +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2018 Allan Wang - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.pitchedapps.frost.dbflow - -import com.pitchedapps.frost.utils.L -import com.raizlabs.android.dbflow.annotation.ConflictAction -import com.raizlabs.android.dbflow.annotation.Database -import com.raizlabs.android.dbflow.annotation.Migration -import com.raizlabs.android.dbflow.annotation.PrimaryKey -import com.raizlabs.android.dbflow.annotation.Table -import com.raizlabs.android.dbflow.kotlinextensions.async -import com.raizlabs.android.dbflow.kotlinextensions.eq -import com.raizlabs.android.dbflow.kotlinextensions.from -import com.raizlabs.android.dbflow.kotlinextensions.save -import com.raizlabs.android.dbflow.kotlinextensions.select -import com.raizlabs.android.dbflow.kotlinextensions.where -import com.raizlabs.android.dbflow.sql.SQLiteType -import com.raizlabs.android.dbflow.sql.migration.AlterTableMigration -import com.raizlabs.android.dbflow.structure.BaseModel - -/** - * Created by Allan Wang on 2017-05-30. - */ - -@Database(version = NotificationDb.VERSION) -object NotificationDb { - const val NAME = "Notifications" - const val VERSION = 2 -} - -@Migration(version = 2, database = NotificationDb::class) -class NotificationMigration2(modelClass: Class) : - AlterTableMigration(modelClass) { - override fun onPreMigrate() { - super.onPreMigrate() - addColumn(SQLiteType.INTEGER, "epochIm") - L.d { "Added column" } - } -} - -@Table(database = NotificationDb::class, allFields = true, primaryKeyConflict = ConflictAction.REPLACE) -data class NotificationModel( - @PrimaryKey var id: Long = -1L, - var epoch: Long = -1L, - var epochIm: Long = -1L -) : BaseModel() - -fun lastNotificationTime(id: Long): NotificationModel = - (select from NotificationModel::class where (NotificationModel_Table.id eq id)).querySingle() - ?: NotificationModel(id = id) - -fun saveNotificationTime(notificationModel: NotificationModel, callback: (() -> Unit)? = null) { - notificationModel.async save { - L.d { "Fb notification model saved" } - L._d { notificationModel } - callback?.invoke() - } -} 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 5683526a..349b415c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt @@ -19,10 +19,10 @@ package com.pitchedapps.frost.facebook import android.app.Activity import android.content.Context import android.webkit.CookieManager -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.db.CookieModel +import com.pitchedapps.frost.db.loadFbCookie +import com.pitchedapps.frost.db.removeCookie +import com.pitchedapps.frost.db.saveFbCookie import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.cookies diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/FrostParser.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/FrostParser.kt index 5709bb9f..57e7cc94 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/FrostParser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/FrostParser.kt @@ -16,7 +16,7 @@ */ package com.pitchedapps.frost.facebook.parsers -import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.db.CookieModel import com.pitchedapps.frost.facebook.FB_CSS_URL_MATCHER import com.pitchedapps.frost.facebook.formattedFbUrl import com.pitchedapps.frost.facebook.get diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/MessageParser.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/MessageParser.kt index f05c42e9..b2b59234 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/MessageParser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/MessageParser.kt @@ -16,7 +16,7 @@ */ package com.pitchedapps.frost.facebook.parsers -import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.db.CookieModel import com.pitchedapps.frost.facebook.FB_EPOCH_MATCHER import com.pitchedapps.frost.facebook.FB_MESSAGE_NOTIF_ID_MATCHER import com.pitchedapps.frost.facebook.FbItem diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/NotifParser.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/NotifParser.kt index b8aa899b..0474e35e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/NotifParser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/parsers/NotifParser.kt @@ -16,7 +16,7 @@ */ package com.pitchedapps.frost.facebook.parsers -import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.db.CookieModel import com.pitchedapps.frost.facebook.FB_EPOCH_MATCHER import com.pitchedapps.frost.facebook.FB_NOTIF_ID_MATCHER import com.pitchedapps.frost.facebook.FbItem diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt index f66f77e2..994f9a18 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt @@ -31,9 +31,9 @@ import ca.allanwang.kau.utils.string import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.R import com.pitchedapps.frost.activities.FrostWebActivity -import com.pitchedapps.frost.dbflow.CookieModel -import com.pitchedapps.frost.dbflow.NotificationModel -import com.pitchedapps.frost.dbflow.lastNotificationTime +import com.pitchedapps.frost.db.CookieModel +import com.pitchedapps.frost.db.NotificationModel +import com.pitchedapps.frost.db.lastNotificationTime import com.pitchedapps.frost.enums.OverlayContext import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.facebook.parsers.FrostParser diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt index b1e0ac8c..a895900f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt @@ -21,8 +21,8 @@ import androidx.core.app.NotificationManagerCompat import ca.allanwang.kau.utils.string import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.R -import com.pitchedapps.frost.dbflow.CookieModel -import com.pitchedapps.frost.dbflow.loadFbCookiesSync +import com.pitchedapps.frost.db.CookieModel +import com.pitchedapps.frost.db.loadFbCookiesSync import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.frostEvent diff --git a/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt index 0200f109..774b0e7f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt @@ -29,8 +29,8 @@ import ca.allanwang.kau.utils.string import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.R import com.pitchedapps.frost.activities.SettingsActivity -import com.pitchedapps.frost.dbflow.NotificationModel -import com.pitchedapps.frost.dbflow.loadFbCookiesAsync +import com.pitchedapps.frost.db.NotificationModel +import com.pitchedapps.frost.db.loadFbCookiesAsync import com.pitchedapps.frost.services.fetchNotifications import com.pitchedapps.frost.services.scheduleNotifications import com.pitchedapps.frost.utils.Prefs diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt index f4baa242..254297a6 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt @@ -29,7 +29,7 @@ import ca.allanwang.kau.utils.showAppInfo import ca.allanwang.kau.utils.string import ca.allanwang.kau.utils.toast import com.pitchedapps.frost.R -import com.pitchedapps.frost.dbflow.loadFbCookie +import com.pitchedapps.frost.db.loadFbCookie import com.pitchedapps.frost.facebook.USER_AGENT_BASIC /** diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt index 4410b26e..1222e93b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt @@ -62,7 +62,7 @@ import com.pitchedapps.frost.activities.TabCustomizerActivity import com.pitchedapps.frost.activities.WebOverlayActivity import com.pitchedapps.frost.activities.WebOverlayActivityBase import com.pitchedapps.frost.activities.WebOverlayBasicActivity -import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.db.CookieModel import com.pitchedapps.frost.facebook.FACEBOOK_COM import com.pitchedapps.frost.facebook.FBCDN_NET import com.pitchedapps.frost.facebook.FbCookie diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt index d7a7de0e..8c7435ea 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt @@ -33,7 +33,7 @@ import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.target.Target import com.mikepenz.google_material_typeface_library.GoogleMaterial import com.pitchedapps.frost.R -import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.db.CookieModel import com.pitchedapps.frost.facebook.profilePictureUrl import com.pitchedapps.frost.glide.FrostGlide import com.pitchedapps.frost.glide.GlideApp diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt index 50a5e2e1..663acfbb 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostJSI.kt @@ -21,7 +21,7 @@ import android.webkit.JavascriptInterface import com.pitchedapps.frost.activities.MainActivity import com.pitchedapps.frost.contracts.MainActivityContract import com.pitchedapps.frost.contracts.VideoViewHolder -import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.db.CookieModel import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt index c21ce93b..11a53c36 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/LoginWebView.kt @@ -29,7 +29,7 @@ import android.webkit.WebView import ca.allanwang.kau.utils.fadeIn import ca.allanwang.kau.utils.isVisible import ca.allanwang.kau.utils.launchMain -import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.db.CookieModel import com.pitchedapps.frost.facebook.FB_LOGIN_URL import com.pitchedapps.frost.facebook.FB_USER_MATCHER import com.pitchedapps.frost.facebook.FbCookie diff --git a/app/src/schemas/com.pitchedapps.frost.db.FrostPrivateDatabase/1.json b/app/src/schemas/com.pitchedapps.frost.db.FrostPrivateDatabase/1.json new file mode 100644 index 00000000..7bc2e5e9 --- /dev/null +++ b/app/src/schemas/com.pitchedapps.frost.db.FrostPrivateDatabase/1.json @@ -0,0 +1,45 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "6af67964d00193c6a3aa2a20200ea0ea", + "entities": [ + { + "tableName": "cookies", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `cookie` TEXT NOT NULL, PRIMARY KEY(`id`))", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "cookie", + "columnName": "cookie", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + } + ], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"6af67964d00193c6a3aa2a20200ea0ea\")" + ] + } +} \ No newline at end of file diff --git a/app/src/schemas/com.pitchedapps.frost.db.FrostPublicDatabase/1.json b/app/src/schemas/com.pitchedapps.frost.db.FrostPublicDatabase/1.json new file mode 100644 index 00000000..687d0bc1 --- /dev/null +++ b/app/src/schemas/com.pitchedapps.frost.db.FrostPublicDatabase/1.json @@ -0,0 +1,39 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "fde868470836ff9230f1d406922d7563", + "entities": [ + { + "tableName": "tabs", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`position` INTEGER NOT NULL, `tab` TEXT NOT NULL, PRIMARY KEY(`position`))", + "fields": [ + { + "fieldPath": "position", + "columnName": "position", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tab", + "columnName": "tab", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "position" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + } + ], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"fde868470836ff9230f1d406922d7563\")" + ] + } +} \ No newline at end of file -- cgit v1.2.3