From 6c2ab77cbedd446f600b8b5665b7433a92fcb1ad Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 18 Nov 2019 11:14:40 +0800 Subject: [PATCH] up --- .../java/io/legado/app/data/AppDatabase.kt | 7 ++++ .../io/legado/app/ui/main/MainActivity.kt | 37 +++++++++++-------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 04dda1df6..3d4549de3 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -4,8 +4,10 @@ import android.content.Context import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase +import androidx.sqlite.db.SupportSQLiteDatabase import io.legado.app.data.dao.* import io.legado.app.data.entities.* +import io.legado.app.utils.putPrefBoolean @Database( @@ -22,6 +24,11 @@ abstract class AppDatabase : RoomDatabase() { fun createDatabase(context: Context): AppDatabase { return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) .fallbackToDestructiveMigration() + .addCallback(object : Callback() { + override fun onDestructiveMigration(db: SupportSQLiteDatabase) { + context.putPrefBoolean("dbChange", true) + } + }) .build() } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 5509812c1..ac81b3982 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -17,6 +17,7 @@ import io.legado.app.help.ActivityHelp import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat import io.legado.app.help.storage.Backup +import io.legado.app.help.storage.Restore import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.yesButton @@ -47,7 +48,7 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager_main.addOnPageChangeListener(this) bottom_navigation_view.setOnNavigationItemSelectedListener(this) - importYueDu() + restore() upVersion() } @@ -61,21 +62,25 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), return false } - private fun importYueDu() { - launch { - if (withContext(IO) { App.db.bookDao().allBookCount == 0 }) { - alert(title = "导入") { - message = "是否导入旧版本数据" - yesButton { - PermissionsCompat.Builder(this@MainActivity) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { viewModel.importYueDuData() } - .request() - } - noButton { - } - }.show().applyTint() + private fun restore() { + if (getPrefBoolean("dbChange")) { + Restore.restore() + } else if (getPrefInt("versionCode") == 0) { + launch { + if (withContext(IO) { App.db.bookDao().allBookCount == 0 }) { + alert(title = "导入") { + message = "是否导入旧版本数据" + yesButton { + PermissionsCompat.Builder(this@MainActivity) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { viewModel.importYueDuData() } + .request() + } + noButton { + } + }.show().applyTint() + } } } }