pull/49/head
kunfei 5 years ago
parent 18d7655a6c
commit e5c03a068b
  1. 4
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 4
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@ -20,14 +20,14 @@ import io.legado.app.help.storage.Restore
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
companion object { companion object {
var DestructiveMigration = false
private const val DATABASE_NAME = "legado.db" private const val DATABASE_NAME = "legado.db"
fun createDatabase(context: Context): AppDatabase { fun createDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.addCallback(object : Callback() { .addCallback(object : Callback() {
override fun onDestructiveMigration(db: SupportSQLiteDatabase) { override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
DestructiveMigration = true
Restore.restore() Restore.restore()
} }
}) })

@ -13,7 +13,6 @@ import io.legado.app.BuildConfig
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.data.AppDatabase
import io.legado.app.help.ActivityHelp import io.legado.app.help.ActivityHelp
import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.Permissions
import io.legado.app.help.permission.PermissionsCompat import io.legado.app.help.permission.PermissionsCompat
@ -48,7 +47,6 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager) view_pager_main.adapter = TabFragmentPageAdapter(supportFragmentManager)
view_pager_main.addOnPageChangeListener(this) view_pager_main.addOnPageChangeListener(this)
bottom_navigation_view.setOnNavigationItemSelectedListener(this) bottom_navigation_view.setOnNavigationItemSelectedListener(this)
restore()
upVersion() upVersion()
} }
@ -63,7 +61,7 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
} }
private fun restore() { private fun restore() {
if (AppDatabase.DestructiveMigration) return
launch { launch {
if (withContext(IO) { App.db.bookDao().allBookCount == 0 }) { if (withContext(IO) { App.db.bookDao().allBookCount == 0 }) {
alert(title = "导入") { alert(title = "导入") {

Loading…
Cancel
Save