pull/49/head
kunfei 5 years ago
parent 1f2fefd4f7
commit 17ac95ca22
  1. 8
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 2
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@ -11,21 +11,23 @@ import io.legado.app.help.storage.Restore
@Database(
entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, ReplaceRule::class,
SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class,
RssArticle::class],
entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class,
ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class,
RssSource::class, Bookmark::class, RssArticle::class],
version = 3,
exportSchema = true
)
abstract class AppDatabase : RoomDatabase() {
companion object {
var DestructiveMigration = false
private const val DATABASE_NAME = "legado.db"
fun createDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.fallbackToDestructiveMigration()
.addCallback(object : Callback() {
override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
DestructiveMigration = true
Restore.restore()
}
})

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

Loading…
Cancel
Save