From d7ee076bdc1a179a0fdb45b812642b8656d74415 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 7 Mar 2019 00:32:17 -0500 Subject: Allow migration for fbtabs --- .../kotlin/com/pitchedapps/frost/StartActivity.kt | 12 ++++++--- .../frost/activities/TabCustomizerActivity.kt | 30 +++++++++++++++------- .../kotlin/com/pitchedapps/frost/db/FbTabsDb.kt | 17 +----------- 3 files changed, 30 insertions(+), 29 deletions(-) (limited to 'app/src') 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() 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 { - 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() -} +data class FbTabModel(@PrimaryKey var position: Int = -1, var tab: FbItem = FbItem.FEED) : BaseModel() \ No newline at end of file -- cgit v1.2.3