pull/49/head
kunfei 5 years ago
parent ece41a27e5
commit 6c2ab77cbe
  1. 7
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 37
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@ -4,8 +4,10 @@ import android.content.Context
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import io.legado.app.data.dao.* import io.legado.app.data.dao.*
import io.legado.app.data.entities.* import io.legado.app.data.entities.*
import io.legado.app.utils.putPrefBoolean
@Database( @Database(
@ -22,6 +24,11 @@ abstract class AppDatabase : RoomDatabase() {
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() {
override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
context.putPrefBoolean("dbChange", true)
}
})
.build() .build()
} }
} }

@ -17,6 +17,7 @@ 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
import io.legado.app.help.storage.Backup 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.alert
import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.yesButton import io.legado.app.lib.dialogs.yesButton
@ -47,7 +48,7 @@ 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)
importYueDu() restore()
upVersion() upVersion()
} }
@ -61,21 +62,25 @@ class MainActivity : VMBaseActivity<MainViewModel>(R.layout.activity_main),
return false return false
} }
private fun importYueDu() { private fun restore() {
launch { if (getPrefBoolean("dbChange")) {
if (withContext(IO) { App.db.bookDao().allBookCount == 0 }) { Restore.restore()
alert(title = "导入") { } else if (getPrefInt("versionCode") == 0) {
message = "是否导入旧版本数据" launch {
yesButton { if (withContext(IO) { App.db.bookDao().allBookCount == 0 }) {
PermissionsCompat.Builder(this@MainActivity) alert(title = "导入") {
.addPermissions(*Permissions.Group.STORAGE) message = "是否导入旧版本数据"
.rationale(R.string.tip_perm_request_storage) yesButton {
.onGranted { viewModel.importYueDuData() } PermissionsCompat.Builder(this@MainActivity)
.request() .addPermissions(*Permissions.Group.STORAGE)
} .rationale(R.string.tip_perm_request_storage)
noButton { .onGranted { viewModel.importYueDuData() }
} .request()
}.show().applyTint() }
noButton {
}
}.show().applyTint()
}
} }
} }
} }

Loading…
Cancel
Save