aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-03-07 19:36:09 -0500
committerAllan Wang <me@allanwang.ca>2019-03-07 19:36:09 -0500
commit3f5d2cf2a55d28528c88e118f09a91fd6c59ac43 (patch)
tree74d0e876c5689f11d9134d5ebd25b03860b1aeb0 /app/src/main/kotlin
parentcac563f6c6a2656c74527bfa2c8b5780765baf69 (diff)
downloadfrost-3f5d2cf2a55d28528c88e118f09a91fd6c59ac43.tar.gz
frost-3f5d2cf2a55d28528c88e118f09a91fd6c59ac43.tar.bz2
frost-3f5d2cf2a55d28528c88e118f09a91fd6c59ac43.zip
Replace tab dao with generic dao
Diffstat (limited to 'app/src/main/kotlin')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt12
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt8
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt12
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt7
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt63
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/GenericDb.kt29
6 files changed, 33 insertions, 98 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
index 24b848fe..87244864 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
@@ -35,10 +35,10 @@ import com.pitchedapps.frost.activities.SelectorActivity
import com.pitchedapps.frost.db.CookieDao
import com.pitchedapps.frost.db.CookieEntity
import com.pitchedapps.frost.db.CookieModel
-import com.pitchedapps.frost.db.FbTabDao
import com.pitchedapps.frost.db.FbTabModel
-import com.pitchedapps.frost.db.save
-import com.pitchedapps.frost.db.selectAll
+import com.pitchedapps.frost.db.GenericDao
+import com.pitchedapps.frost.db.getTabs
+import com.pitchedapps.frost.db.saveTabs
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.utils.EXTRA_COOKIES
import com.pitchedapps.frost.utils.L
@@ -59,7 +59,7 @@ import java.util.ArrayList
class StartActivity : KauBaseActivity() {
private val cookieDao: CookieDao by inject()
- private val tabDao: FbTabDao by inject()
+ private val genericDao: GenericDao by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -114,8 +114,8 @@ class StartActivity : KauBaseActivity() {
}
val tabs = (select from FbTabModel::class).queryList().map(FbTabModel::tab)
if (tabs.isNotEmpty()) {
- tabDao.save(tabs)
- L._d { "Migrated tabs ${tabDao.selectAll()}" }
+ genericDao.saveTabs(tabs)
+ L._d { "Migrated tabs ${genericDao.getTabs()}" }
}
deleteDatabase("Cookies.db")
deleteDatabase("FrostTabs.db")
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
index 90fc8ea7..c43c31a2 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/BaseMainActivity.kt
@@ -70,9 +70,9 @@ import com.pitchedapps.frost.contracts.FileChooserDelegate
import com.pitchedapps.frost.contracts.MainActivityContract
import com.pitchedapps.frost.contracts.VideoViewHolder
import com.pitchedapps.frost.db.CookieDao
-import com.pitchedapps.frost.db.FbTabDao
+import com.pitchedapps.frost.db.GenericDao
import com.pitchedapps.frost.db.currentCookie
-import com.pitchedapps.frost.db.selectAll
+import com.pitchedapps.frost.db.getTabs
import com.pitchedapps.frost.enums.MainActivityLayout
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.FbItem
@@ -129,7 +129,7 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract,
override val frameWrapper: FrameLayout get() = frame_wrapper
val viewPager: FrostViewPager get() = container
val cookieDao: CookieDao by inject()
- val tabDao: FbTabDao by inject()
+ val genericDao: GenericDao by inject()
/*
* Components with the same id in multiple layout files
@@ -165,7 +165,7 @@ abstract class BaseMainActivity : BaseActivity(), MainActivityContract,
onNestedCreate(savedInstanceState)
L.i { "Main finished loading UI in ${System.currentTimeMillis() - start} ms" }
launch {
- adapter.setPages(tabDao.selectAll())
+ adapter.setPages(genericDao.getTabs())
}
controlWebview = WebView(this)
if (BuildConfig.VERSION_CODE > Prefs.versionCode) {
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt
index 7781e190..a380157f 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt
@@ -34,10 +34,10 @@ import com.mikepenz.fastadapter_extensions.drag.ItemTouchCallback
import com.mikepenz.fastadapter_extensions.drag.SimpleDragCallback
import com.mikepenz.google_material_typeface_library.GoogleMaterial
import com.pitchedapps.frost.R
-import com.pitchedapps.frost.db.FbTabDao
+import com.pitchedapps.frost.db.GenericDao
import com.pitchedapps.frost.db.TAB_COUNT
-import com.pitchedapps.frost.db.save
-import com.pitchedapps.frost.db.selectAll
+import com.pitchedapps.frost.db.getTabs
+import com.pitchedapps.frost.db.saveTabs
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.iitems.TabIItem
import com.pitchedapps.frost.utils.L
@@ -54,7 +54,7 @@ import java.util.Collections
*/
class TabCustomizerActivity : BaseActivity() {
- private val tabDao: FbTabDao by inject()
+ private val genericDao: GenericDao by inject()
private val adapter = FastItemAdapter<TabIItem>()
@@ -74,7 +74,7 @@ class TabCustomizerActivity : BaseActivity() {
instructions.setTextColor(Prefs.textColor)
launch {
- val tabs = tabDao.selectAll().toMutableList()
+ val tabs = genericDao.getTabs().toMutableList()
L.d { "Tabs $tabs" }
val remaining = FbItem.values().filter { it.name[0] != '_' }.toMutableList()
remaining.removeAll(tabs)
@@ -94,7 +94,7 @@ class TabCustomizerActivity : BaseActivity() {
fab_save.setOnClickListener {
launchMain(NonCancellable) {
val tabs = adapter.adapterItems.subList(0, TAB_COUNT).map(TabIItem::item)
- tabDao.save(tabs)
+ genericDao.saveTabs(tabs)
setResult(Activity.RESULT_OK)
finish()
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt
index 5fc986e3..0c009634 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt
@@ -27,11 +27,10 @@ abstract class FrostPrivateDatabase : RoomDatabase(), FrostPrivateDao {
}
interface FrostPublicDao {
- fun tabDao(): FbTabDao
+ fun genericDao(): GenericDao
}
-@Database(entities = [FbTabEntity::class], version = 1, exportSchema = true)
-@TypeConverters(FbItemConverter::class)
+@Database(entities = [GenericEntity::class], version = 1, exportSchema = true)
abstract class FrostPublicDatabase : RoomDatabase(), FrostPublicDao {
companion object {
const val DATABASE_NAME = "frost-db"
@@ -71,9 +70,9 @@ class FrostDatabase(private val privateDb: FrostPrivateDatabase, private val pub
fun module(context: Context) = module {
single { create(context) }
single { get<FrostDatabase>().cookieDao() }
- single { get<FrostDatabase>().tabDao() }
single { get<FrostDatabase>().cacheDao() }
single { get<FrostDatabase>().notifDao() }
+ single { get<FrostDatabase>().genericDao() }
}
/**
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt
index f4e74509..c63be794 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt
@@ -16,79 +16,16 @@
*/
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.raizlabs.android.dbflow.annotation.Database
import com.raizlabs.android.dbflow.annotation.PrimaryKey
import com.raizlabs.android.dbflow.annotation.Table
import com.raizlabs.android.dbflow.structure.BaseModel
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.withContext
/**
* Created by Allan Wang on 2017-05-30.
*/
-@Entity(tableName = "tabs")
-data class FbTabEntity(@androidx.room.PrimaryKey val position: Int, val tab: FbItem)
-
-@Dao
-interface FbTabDao {
-
- @Query("SELECT * FROM tabs ORDER BY position ASC")
- fun _selectAll(): List<FbTabEntity>
-
- @Query("DELETE FROM tabs")
- fun _deleteAll()
-
- @Insert(onConflict = OnConflictStrategy.REPLACE)
- fun _insertAll(items: List<FbTabEntity>)
-
- @Transaction
- fun _save(items: List<FbTabEntity>) {
- _deleteAll()
- _insertAll(items)
- }
-}
-
-/**
- * Saving tabs operates by deleting all db items and saving the new list.
- * Transactions can't be done with suspensions in room as switching threads during the process
- * may result in a deadlock.
- * That's why we disallow thread switching within the transaction, but wrap the entire thing in a coroutine
- */
-suspend fun FbTabDao.save(items: List<FbItem>) {
- withContext(Dispatchers.IO) {
- val entities = (items.takeIf { it.isNotEmpty() } ?: defaultTabs()).mapIndexed { index, fbItem ->
- FbTabEntity(
- index,
- fbItem
- )
- }
- _save(entities)
- }
-}
-
-suspend fun FbTabDao.selectAll(): List<FbItem> = withContext(Dispatchers.IO) {
- _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)
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/GenericDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/GenericDb.kt
index c191b673..c1f05092 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/GenericDb.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/GenericDb.kt
@@ -24,7 +24,6 @@ import androidx.room.PrimaryKey
import androidx.room.Query
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.facebook.defaultTabs
-import com.pitchedapps.frost.utils.L
/**
* Created by Allan Wang on 2017-05-30.
@@ -52,21 +51,21 @@ interface GenericDao {
@Query("DELETE FROM frost_generic WHERE type = :type")
suspend fun delete(type: String)
- suspend fun saveTabs(tabs: List<FbItem>) {
- val content = tabs.joinToString(",") { it.name }
- save(GenericEntity(TYPE_TABS, content))
- }
-
- suspend fun getTabs(): List<FbItem> {
- val allTabs = FbItem.values.map { it.name to it }.toMap()
- return select(TYPE_TABS)
- ?.split(",")
- ?.mapNotNull { allTabs[it] }
- ?.takeIf { it.isNotEmpty() }
- ?: defaultTabs()
- }
-
companion object {
const val TYPE_TABS = "generic_tabs"
}
+}
+
+suspend fun GenericDao.saveTabs(tabs: List<FbItem>) {
+ val content = tabs.joinToString(",") { it.name }
+ save(GenericEntity(GenericDao.TYPE_TABS, content))
+}
+
+suspend fun GenericDao.getTabs(): List<FbItem> {
+ val allTabs = FbItem.values.map { it.name to it }.toMap()
+ return select(GenericDao.TYPE_TABS)
+ ?.split(",")
+ ?.mapNotNull { allTabs[it] }
+ ?.takeIf { it.isNotEmpty() }
+ ?: defaultTabs()
} \ No newline at end of file