pull/49/head
kunfei 5 years ago
parent 9da82d8ed9
commit f0daf7dc39
  1. 51
      app/src/main/java/io/legado/app/help/storage/Restore.kt

@ -17,8 +17,6 @@ import io.legado.app.help.ReadBookConfig
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.storage.Backup.defaultPath import io.legado.app.help.storage.Backup.defaultPath
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import org.jetbrains.anko.defaultSharedPreferences import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.doAsync import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
@ -103,11 +101,9 @@ object Restore {
fun importYueDuData(context: Context) { fun importYueDuData(context: Context) {
Coroutine.async { Coroutine.async {
val file = File(FileUtils.getSdCardPath(), "YueDu") val file = File(FileUtils.getSdCardPath(), "YueDu")
// 导入书架 // 导入书架
val shelfFile = File(file, "myBookShelf.json") val shelfFile = File(file, "myBookShelf.json")
val books = mutableListOf<Book>() val books = mutableListOf<Book>()
if (shelfFile.exists()) try {
val items: List<Map<String, Any>> = jsonPath.parse(shelfFile.readText()).read("$") val items: List<Map<String, Any>> = jsonPath.parse(shelfFile.readText()).read("$")
val existingBooks = App.db.bookDao().allBookUrls.toSet() val existingBooks = App.db.bookDao().allBookUrls.toSet()
for (item in items) { for (item in items) {
@ -145,20 +141,18 @@ object Restore {
books.add(book) books.add(book)
} }
App.db.bookDao().insert(*books.toTypedArray()) App.db.bookDao().insert(*books.toTypedArray())
withContext(IO) { books
context.toast("成功导入书籍${books.size}") }.onSuccess {
} context.toast("成功导入书籍${it?.size}")
} catch (e: Exception) { }.onError {
e.printStackTrace() context.toast("导入书籍失败\n${it.localizedMessage}")
withContext(IO) {
context.toast("导入书籍失败\n${e.localizedMessage}")
}
} }
Coroutine.async {
// Book source // Book source
val file = File(FileUtils.getSdCardPath(), "YueDu")
val sourceFile = File(file, "myBookSource.json") val sourceFile = File(file, "myBookSource.json")
val bookSources = mutableListOf<BookSource>() val bookSources = mutableListOf<BookSource>()
if (sourceFile.exists()) try {
val items: List<Map<String, Any>> = jsonPath.parse(sourceFile.readText()).read("$") val items: List<Map<String, Any>> = jsonPath.parse(sourceFile.readText()).read("$")
for (item in items) { for (item in items) {
val jsonItem = jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
@ -167,20 +161,19 @@ object Restore {
} }
} }
App.db.bookSourceDao().insert(*bookSources.toTypedArray()) App.db.bookSourceDao().insert(*bookSources.toTypedArray())
withContext(IO) { bookSources
context.toast("成功导入书源${bookSources.size}") }.onSuccess {
} context.toast("成功导入书源${it?.size}")
} catch (e: Exception) { }.onError {
e.printStackTrace() context.toast("导入源失败\n${it.localizedMessage}")
withContext(IO) {
context.toast("导入源失败\n${e.localizedMessage}")
}
} }
Coroutine.async {
// Book source
val file = File(FileUtils.getSdCardPath(), "YueDu")
// Replace rules // Replace rules
val ruleFile = File(file, "myBookReplaceRule.json") val ruleFile = File(file, "myBookReplaceRule.json")
val replaceRules = mutableListOf<ReplaceRule>() val replaceRules = mutableListOf<ReplaceRule>()
if (ruleFile.exists()) try {
val items: List<Map<String, Any>> = jsonPath.parse(ruleFile.readText()).read("$") val items: List<Map<String, Any>> = jsonPath.parse(ruleFile.readText()).read("$")
val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet() val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet()
for ((index: Int, item: Map<String, Any>) in items.withIndex()) { for ((index: Int, item: Map<String, Any>) in items.withIndex()) {
@ -197,16 +190,12 @@ object Restore {
rule.order = jsonItem.readInt("$.serialNumber") ?: index rule.order = jsonItem.readInt("$.serialNumber") ?: index
replaceRules.add(rule) replaceRules.add(rule)
} }
withContext(IO) { replaceRules
context.toast("成功导入替换规则${replaceRules.size}") }.onSuccess {
} context.toast("成功导入替换规则${it?.size}")
} catch (e: Exception) { }.onError {
e.printStackTrace() context.toast("导入替换规则失败\n${it.localizedMessage}")
withContext(IO) {
context.toast("导入替换规则失败\n${e.localizedMessage}")
}
} }
} }
} }
}
Loading…
Cancel
Save