feat: 优化代码

pull/133/head
kunfei 5 years ago
parent 722497f599
commit d9b46e3e48
  1. 2
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 2
      app/src/main/java/io/legado/app/help/BookHelp.kt
  3. 17
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  4. 3
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt
  5. 3
      app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt

@ -32,7 +32,7 @@ abstract class AppDatabase : RoomDatabase() {
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.addCallback(object : Callback() { .addCallback(object : Callback() {
override fun onDestructiveMigration(db: SupportSQLiteDatabase) { override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
GlobalScope.launch { Restore.restore(Backup.backupPath) } GlobalScope.launch { Restore.restoreDatabase(Backup.backupPath) }
} }
}) })
.build() .build()

@ -272,6 +272,7 @@ object BookHelp {
var c = content var c = content
if (enableReplace) { if (enableReplace) {
upReplaceRules(name, origin) upReplaceRules(name, origin)
kotlin.runCatching {
for (item in replaceRules) { for (item in replaceRules) {
item.pattern.let { item.pattern.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
@ -284,6 +285,7 @@ object BookHelp {
} }
} }
} }
}
if (!c.substringBefore("\n").contains(title)) { if (!c.substringBefore("\n").contains(title)) {
c = "$title\n$c" c = "$title\n$c"
} }

@ -62,10 +62,11 @@ object Restore {
} }
} }
} }
restore(Backup.backupPath) restoreDatabase()
restoreConfig()
} }
suspend fun restore(path: String) { suspend fun restoreDatabase(path: String = Backup.backupPath) {
withContext(IO) { withContext(IO) {
fileToListT<Book>(path, "bookshelf.json")?.let { fileToListT<Book>(path, "bookshelf.json")?.let {
App.db.bookDao().insert(*it.toTypedArray()) App.db.bookDao().insert(*it.toTypedArray())
@ -85,11 +86,16 @@ object Restore {
fileToListT<ReplaceRule>(path, "replaceRule.json")?.let { fileToListT<ReplaceRule>(path, "replaceRule.json")?.let {
App.db.replaceRuleDao().insert(*it.toTypedArray()) App.db.replaceRuleDao().insert(*it.toTypedArray())
} }
}
}
suspend fun restoreConfig(path: String = Backup.backupPath) {
withContext(IO) {
try { try {
val file = val file =
FileUtils.createFileIfNotExist(path + File.separator + ReadBookConfig.readConfigFileName) FileUtils.createFileIfNotExist(path + File.separator + ReadBookConfig.readConfigFileName)
val configFile = val configFile =
File(App.INSTANCE.filesDir.absolutePath + File.separator + ReadBookConfig.readConfigFileName) FileUtils.getFile(App.INSTANCE.filesDir, ReadBookConfig.readConfigFileName)
if (file.exists()) { if (file.exists()) {
file.copyTo(configFile, true) file.copyTo(configFile, true)
ReadBookConfig.upConfig() ReadBookConfig.upConfig()
@ -109,6 +115,7 @@ object Restore {
} }
edit.putInt(PreferKey.versionCode, App.INSTANCE.versionCode) edit.putInt(PreferKey.versionCode, App.INSTANCE.versionCode)
edit.apply() edit.apply()
}
ReadBookConfig.apply { ReadBookConfig.apply {
styleSelect = App.INSTANCE.getPrefInt(PreferKey.readStyleSelect) styleSelect = App.INSTANCE.getPrefInt(PreferKey.readStyleSelect)
shareLayout = App.INSTANCE.getPrefBoolean(PreferKey.shareLayout) shareLayout = App.INSTANCE.getPrefBoolean(PreferKey.shareLayout)
@ -119,16 +126,14 @@ object Restore {
} }
ChapterProvider.upStyle() ChapterProvider.upStyle()
ReadBook.loadContent() ReadBook.loadContent()
}
withContext(Main) { withContext(Main) {
App.INSTANCE.applyDayNight() App.INSTANCE.applyDayNight()
}
if (!BuildConfig.DEBUG) if (!BuildConfig.DEBUG)
LauncherIconHelp.changeIcon(App.INSTANCE.getPrefString(PreferKey.launcherIcon)) LauncherIconHelp.changeIcon(App.INSTANCE.getPrefString(PreferKey.launcherIcon))
} }
} }
}
private inline fun <reified T> fileToListT(path: String, fileName: String): List<T>? { private inline fun <reified T> fileToListT(path: String, fileName: String): List<T>? {
try { try {
val file = FileUtils.createFileIfNotExist(path + File.separator + fileName) val file = FileUtils.createFileIfNotExist(path + File.separator + fileName)

@ -95,7 +95,8 @@ object WebDavHelp {
webDav.downloadTo(zipFilePath, true) webDav.downloadTo(zipFilePath, true)
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
ZipUtils.unzipFile(zipFilePath, Backup.backupPath) ZipUtils.unzipFile(zipFilePath, Backup.backupPath)
Restore.restore(Backup.backupPath) Restore.restoreDatabase()
Restore.restoreConfig()
} }
} }
}.onSuccess { }.onSuccess {

@ -114,7 +114,8 @@ object BackupRestoreUi {
.onGranted { .onGranted {
Coroutine.async { Coroutine.async {
AppConfig.backupPath = path AppConfig.backupPath = path
Restore.restore(path) Restore.restoreDatabase(path)
Restore.restoreConfig(path)
}.onSuccess { }.onSuccess {
fragment.toast(R.string.restore_success) fragment.toast(R.string.restore_success)
} }

Loading…
Cancel
Save