aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-03-07 00:32:17 -0500
committerAllan Wang <me@allanwang.ca>2019-03-07 00:32:17 -0500
commitd7ee076bdc1a179a0fdb45b812642b8656d74415 (patch)
tree78e660d012f4287360896298b5e841db3922e937
parentcf5fd28ec74069746b49757b6a95a36851a56105 (diff)
downloadfrost-d7ee076bdc1a179a0fdb45b812642b8656d74415.tar.gz
frost-d7ee076bdc1a179a0fdb45b812642b8656d74415.tar.bz2
frost-d7ee076bdc1a179a0fdb45b812642b8656d74415.zip
Allow migration for fbtabs
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt12
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt30
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt17
3 files changed, 30 insertions, 29 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
index e81df3fa..3b7418e1 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
@@ -35,7 +35,9 @@ 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.facebook.FbCookie
import com.pitchedapps.frost.utils.EXTRA_COOKIES
import com.pitchedapps.frost.utils.L
@@ -56,6 +58,7 @@ import java.util.ArrayList
class StartActivity : KauBaseActivity() {
private val cookieDao: CookieDao by inject()
+ private val tabDao: FbTabDao by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -98,13 +101,14 @@ class StartActivity : KauBaseActivity() {
/**
* Migrate from dbflow to room
+ * TODO delete dbflow data
*/
private suspend fun migrate() = withContext(Dispatchers.IO) {
if (cookieDao.selectAll().isNotEmpty()) return@withContext
- val cookies = (select from CookieModel::class).queryList()
- cookieDao.insertCookies(cookies.map { CookieEntity(it.id, it.name, it.cookie) })
- // TODO
- val tabs = (select from FbTabModel::class).queryList()
+ val cookies = (select from CookieModel::class).queryList().map { CookieEntity(it.id, it.name, it.cookie) }
+ cookieDao.insertCookies(cookies)
+ val tabs = (select from FbTabModel::class).queryList().map(FbTabModel::tab)
+ tabDao.save(tabs)
}
private fun showInvalidWebView() =
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 73e6419e..a5dceb3a 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt
@@ -25,6 +25,7 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import ca.allanwang.kau.kotlin.lazyContext
+import ca.allanwang.kau.utils.launchMain
import ca.allanwang.kau.utils.scaleXY
import ca.allanwang.kau.utils.setIcon
import ca.allanwang.kau.utils.withAlpha
@@ -33,14 +34,18 @@ 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.TAB_COUNT
-import com.pitchedapps.frost.db.loadFbTabs
import com.pitchedapps.frost.db.save
+import com.pitchedapps.frost.db.selectAll
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.iitems.TabIItem
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.setFrostColors
import kotlinx.android.synthetic.main.activity_tab_customizer.*
+import kotlinx.coroutines.NonCancellable
+import kotlinx.coroutines.launch
+import org.koin.android.ext.android.inject
import java.util.Collections
/**
@@ -48,6 +53,8 @@ import java.util.Collections
*/
class TabCustomizerActivity : BaseActivity() {
+ private val tabDao: FbTabDao by inject()
+
private val adapter = FastItemAdapter<TabIItem>()
private val wobble = lazyContext { AnimationUtils.loadAnimation(it, R.anim.rotate_delta) }
@@ -65,12 +72,14 @@ class TabCustomizerActivity : BaseActivity() {
divider.setBackgroundColor(Prefs.textColor.withAlpha(30))
instructions.setTextColor(Prefs.textColor)
- val tabs = loadFbTabs().toMutableList()
- val remaining = FbItem.values().filter { it.name[0] != '_' }.toMutableList()
- remaining.removeAll(tabs)
- tabs.addAll(remaining)
+ launch {
+ val tabs = tabDao.selectAll().toMutableList()
+ val remaining = FbItem.values().filter { it.name[0] != '_' }.toMutableList()
+ remaining.removeAll(tabs)
+ tabs.addAll(remaining)
+ adapter.add(tabs.map(::TabIItem))
+ }
- adapter.add(tabs.map(::TabIItem))
bindSwapper(adapter, tab_recycler)
adapter.withOnClickListener { view, _, _, _ -> view!!.wobble(); true }
@@ -80,9 +89,12 @@ class TabCustomizerActivity : BaseActivity() {
fab_save.setIcon(GoogleMaterial.Icon.gmd_check, Prefs.iconColor)
fab_save.backgroundTintList = ColorStateList.valueOf(Prefs.accentColor)
fab_save.setOnClickListener {
- adapter.adapterItems.subList(0, TAB_COUNT).map(TabIItem::item).save()
- setResult(Activity.RESULT_OK)
- finish()
+ launchMain(NonCancellable) {
+ val tabs = adapter.adapterItems.subList(0, TAB_COUNT).map(TabIItem::item)
+ tabDao.save(tabs)
+ setResult(Activity.RESULT_OK)
+ finish()
+ }
}
fab_cancel.setIcon(GoogleMaterial.Icon.gmd_close, Prefs.iconColor)
fab_cancel.backgroundTintList = ColorStateList.valueOf(Prefs.accentColor)
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 582d57fb..c2bb0837 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt
@@ -103,19 +103,4 @@ object FbTabsDb {
}
@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<FbItem> {
- val tabs: List<FbTabModel>? = (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<FbItem>.save() {
- database<FbTabsDb>().beginTransactionAsync(mapIndexed(::FbTabModel).fastSave().build()).execute()
-}
+data class FbTabModel(@PrimaryKey var position: Int = -1, var tab: FbItem = FbItem.FEED) : BaseModel() \ No newline at end of file