feat: 修复bug

pull/248/head
gedoor 5 years ago
parent a54062b941
commit 65864b6fd0
  1. 1
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 35
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt

@ -37,6 +37,7 @@ abstract class AppDatabase : RoomDatabase() {
GlobalScope.launch { Restore.restoreDatabase(Backup.backupPath) } GlobalScope.launch { Restore.restoreDatabase(Backup.backupPath) }
} }
}) })
.allowMainThreadQueries()
.build() .build()
} }

@ -6,116 +6,81 @@ import androidx.documentfile.provider.DocumentFile
import io.legado.app.App import io.legado.app.App
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.utils.DocumentUtils import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import java.io.File import java.io.File
object ImportOldData { object ImportOldData {
fun import(context: Context, file: File) { fun import(context: Context, file: File) {
GlobalScope.launch(Dispatchers.IO) {
try {// 导入书架 try {// 导入书架
val shelfFile = val shelfFile =
FileUtils.createFileIfNotExist(file, "myBookShelf.json") FileUtils.createFileIfNotExist(file, "myBookShelf.json")
val json = shelfFile.readText() val json = shelfFile.readText()
val importCount = importOldBookshelf(json) val importCount = importOldBookshelf(json)
withContext(Dispatchers.Main) {
context.toast("成功导入书籍${importCount}") context.toast("成功导入书籍${importCount}")
}
} catch (e: Exception) { } catch (e: Exception) {
withContext(Dispatchers.Main) {
context.toast("导入书籍失败\n${e.localizedMessage}") context.toast("导入书籍失败\n${e.localizedMessage}")
} }
}
try {// Book source try {// Book source
val sourceFile = val sourceFile =
FileUtils.getFile(file, "myBookSource.json") FileUtils.getFile(file, "myBookSource.json")
val json = sourceFile.readText() val json = sourceFile.readText()
val importCount = importOldSource(json) val importCount = importOldSource(json)
withContext(Dispatchers.Main) {
context.toast("成功导入书源${importCount}") context.toast("成功导入书源${importCount}")
}
} catch (e: Exception) { } catch (e: Exception) {
withContext(Dispatchers.Main) {
context.toast("导入源失败\n${e.localizedMessage}") context.toast("导入源失败\n${e.localizedMessage}")
} }
}
try {// Replace rules try {// Replace rules
val ruleFile = FileUtils.getFile(file, "myBookReplaceRule.json") val ruleFile = FileUtils.getFile(file, "myBookReplaceRule.json")
if (ruleFile.exists()) { if (ruleFile.exists()) {
val json = ruleFile.readText() val json = ruleFile.readText()
val importCount = importOldReplaceRule(json) val importCount = importOldReplaceRule(json)
withContext(Dispatchers.Main) {
context.toast("成功导入替换规则${importCount}") context.toast("成功导入替换规则${importCount}")
}
} else { } else {
withContext(Dispatchers.Main) {
context.toast("未找到替换规则") context.toast("未找到替换规则")
} }
}
} catch (e: Exception) { } catch (e: Exception) {
withContext(Dispatchers.Main) {
context.toast("导入替换规则失败\n${e.localizedMessage}") context.toast("导入替换规则失败\n${e.localizedMessage}")
} }
} }
}
}
fun importUri(uri: Uri) { fun importUri(uri: Uri) {
Coroutine.async {
DocumentFile.fromTreeUri(App.INSTANCE, uri)?.listFiles()?.forEach { DocumentFile.fromTreeUri(App.INSTANCE, uri)?.listFiles()?.forEach {
when (it.name) { when (it.name) {
"myBookShelf.json" -> "myBookShelf.json" ->
try { try {
DocumentUtils.readText(App.INSTANCE, it.uri)?.let { json -> DocumentUtils.readText(App.INSTANCE, it.uri)?.let { json ->
val importCount = importOldBookshelf(json) val importCount = importOldBookshelf(json)
withContext(Dispatchers.Main) {
App.INSTANCE.toast("成功导入书籍${importCount}") App.INSTANCE.toast("成功导入书籍${importCount}")
} }
}
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
withContext(Dispatchers.Main) {
App.INSTANCE.toast("导入书籍失败\n${e.localizedMessage}") App.INSTANCE.toast("导入书籍失败\n${e.localizedMessage}")
} }
}
"myBookSource.json" -> "myBookSource.json" ->
try { try {
DocumentUtils.readText(App.INSTANCE, it.uri)?.let { json -> DocumentUtils.readText(App.INSTANCE, it.uri)?.let { json ->
val importCount = importOldSource(json) val importCount = importOldSource(json)
withContext(Dispatchers.Main) {
App.INSTANCE.toast("成功导入书源${importCount}") App.INSTANCE.toast("成功导入书源${importCount}")
} }
}
} catch (e: Exception) { } catch (e: Exception) {
withContext(Dispatchers.Main) {
App.INSTANCE.toast("导入源失败\n${e.localizedMessage}") App.INSTANCE.toast("导入源失败\n${e.localizedMessage}")
} }
}
"myBookReplaceRule.json" -> "myBookReplaceRule.json" ->
try { try {
DocumentUtils.readText(App.INSTANCE, it.uri)?.let { json -> DocumentUtils.readText(App.INSTANCE, it.uri)?.let { json ->
val importCount = importOldReplaceRule(json) val importCount = importOldReplaceRule(json)
withContext(Dispatchers.Main) {
App.INSTANCE.toast("成功导入替换规则${importCount}") App.INSTANCE.toast("成功导入替换规则${importCount}")
} }
}
} catch (e: Exception) { } catch (e: Exception) {
withContext(Dispatchers.Main) {
App.INSTANCE.toast("导入替换规则失败\n${e.localizedMessage}") App.INSTANCE.toast("导入替换规则失败\n${e.localizedMessage}")
} }
} }
} }
} }
}
}
private fun importOldBookshelf(json: String): Int { private fun importOldBookshelf(json: String): Int {
val books = OldBook.toNewBook(json) val books = OldBook.toNewBook(json)

Loading…
Cancel
Save