pull/66/head
kunfei 5 years ago
parent 45d74ba7c1
commit a64d5b8cfb
  1. 1
      app/src/main/java/io/legado/app/help/storage/OldRule.kt
  2. 157
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  3. 25
      app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt

@ -205,4 +205,5 @@ object OldRule {
return GSON.toJson(map)
}
}

@ -111,47 +111,12 @@ object Restore {
fun importYueDuData(context: Context) {
GlobalScope.launch(IO) {
try {// 导入书架
val shelfFile = FileHelp.getFile(Backup.defaultPath + File.separator + "myBookShelf.json")
val books = mutableListOf<Book>()
val items: List<Map<String, Any>> = jsonPath.parse(shelfFile.readText()).read("$")
val existingBooks = App.db.bookDao().allBookUrls.toSet()
for (item in items) {
val jsonItem = jsonPath.parse(item)
val book = Book()
book.bookUrl = jsonItem.readString("$.noteUrl") ?: ""
if (book.bookUrl.isBlank()) continue
book.name = jsonItem.readString("$.bookInfoBean.name") ?: ""
if (book.bookUrl in existingBooks) {
Log.d(AppConst.APP_TAG, "Found existing book: ${book.name}")
continue
}
book.origin = jsonItem.readString("$.tag") ?: ""
book.originName = jsonItem.readString("$.bookInfoBean.origin") ?: ""
book.author = jsonItem.readString("$.bookInfoBean.author") ?: ""
book.type =
if (jsonItem.readString("$.bookInfoBean.bookSourceType") == "AUDIO") 1 else 0
book.tocUrl = jsonItem.readString("$.bookInfoBean.chapterUrl") ?: book.bookUrl
book.coverUrl = jsonItem.readString("$.bookInfoBean.coverUrl")
book.customCoverUrl = jsonItem.readString("$.customCoverPath")
book.lastCheckTime = jsonItem.readLong("$.bookInfoBean.finalRefreshData") ?: 0
book.canUpdate = jsonItem.readBool("$.allowUpdate") == true
book.totalChapterNum = jsonItem.readInt("$.chapterListSize") ?: 0
book.durChapterIndex = jsonItem.readInt("$.durChapter") ?: 0
book.durChapterTitle = jsonItem.readString("$.durChapterName")
book.durChapterPos = jsonItem.readInt("$.durChapterPage") ?: 0
book.durChapterTime = jsonItem.readLong("$.finalDate") ?: 0
book.group = jsonItem.readInt("$.group") ?: 0
book.intro = jsonItem.readString("$.bookInfoBean.introduce")
book.latestChapterTitle = jsonItem.readString("$.lastChapterName")
book.lastCheckCount = jsonItem.readInt("$.newChapters") ?: 0
book.order = jsonItem.readInt("$.serialNumber") ?: 0
book.useReplaceRule = jsonItem.readBool("$.useReplaceRule") == true
book.variable = jsonItem.readString("$.variable")
books.add(book)
}
App.db.bookDao().insert(*books.toTypedArray())
val shelfFile =
FileHelp.getFile(Backup.defaultPath + File.separator + "myBookShelf.json")
val json = shelfFile.readText()
val importCount = importOldBookshelf(json)
withContext(Main) {
context.toast("成功导入书籍${books.size}")
context.toast("成功导入书籍${importCount}")
}
} catch (e: Exception) {
withContext(Main) {
@ -162,17 +127,10 @@ object Restore {
try {// Book source
val sourceFile =
FileHelp.getFile(Backup.defaultPath + File.separator + "myBookSource.json")
val bookSources = mutableListOf<BookSource>()
val items: List<Map<String, Any>> = jsonPath.parse(sourceFile.readText()).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
OldRule.jsonToBookSource(jsonItem.jsonString())?.let {
bookSources.add(it)
}
}
App.db.bookSourceDao().insert(*bookSources.toTypedArray())
val json = sourceFile.readText()
val importCount = importOldSource(json)
withContext(Main) {
context.toast("成功导入书源${bookSources.size}")
context.toast("成功导入书源${importCount}")
}
} catch (e: Exception) {
withContext(Main) {
@ -183,26 +141,10 @@ object Restore {
try {// Replace rules
val ruleFile =
FileHelp.getFile(Backup.defaultPath + File.separator + "myBookReplaceRule.json")
val replaceRules = mutableListOf<ReplaceRule>()
val items: List<Map<String, Any>> = jsonPath.parse(ruleFile.readText()).read("$")
val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet()
for ((index: Int, item: Map<String, Any>) in items.withIndex()) {
val jsonItem = jsonPath.parse(item)
val rule = ReplaceRule()
rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis().plus(index)
rule.pattern = jsonItem.readString("$.regex") ?: ""
if (rule.pattern.isEmpty() || rule.pattern in existingRules) continue
rule.name = jsonItem.readString("$.replaceSummary") ?: ""
rule.replacement = jsonItem.readString("$.replacement") ?: ""
rule.isRegex = jsonItem.readBool("$.isRegex") == true
rule.scope = jsonItem.readString("$.useTo")
rule.isEnabled = jsonItem.readBool("$.enable") == true
rule.order = jsonItem.readInt("$.serialNumber") ?: index
replaceRules.add(rule)
}
App.db.replaceRuleDao().insert(*replaceRules.toTypedArray())
val json = ruleFile.readText()
val importCount = importOldReplaceRule(json)
withContext(Main) {
context.toast("成功导入替换规则${replaceRules.size}")
context.toast("成功导入替换规则${importCount}")
}
} catch (e: Exception) {
withContext(Main) {
@ -211,4 +153,81 @@ object Restore {
}
}
}
fun importOldBookshelf(json: String): Int {
val books = mutableListOf<Book>()
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
val existingBooks = App.db.bookDao().allBookUrls.toSet()
for (item in items) {
val jsonItem = jsonPath.parse(item)
val book = Book()
book.bookUrl = jsonItem.readString("$.noteUrl") ?: ""
if (book.bookUrl.isBlank()) continue
book.name = jsonItem.readString("$.bookInfoBean.name") ?: ""
if (book.bookUrl in existingBooks) {
Log.d(AppConst.APP_TAG, "Found existing book: ${book.name}")
continue
}
book.origin = jsonItem.readString("$.tag") ?: ""
book.originName = jsonItem.readString("$.bookInfoBean.origin") ?: ""
book.author = jsonItem.readString("$.bookInfoBean.author") ?: ""
book.type =
if (jsonItem.readString("$.bookInfoBean.bookSourceType") == "AUDIO") 1 else 0
book.tocUrl = jsonItem.readString("$.bookInfoBean.chapterUrl") ?: book.bookUrl
book.coverUrl = jsonItem.readString("$.bookInfoBean.coverUrl")
book.customCoverUrl = jsonItem.readString("$.customCoverPath")
book.lastCheckTime = jsonItem.readLong("$.bookInfoBean.finalRefreshData") ?: 0
book.canUpdate = jsonItem.readBool("$.allowUpdate") == true
book.totalChapterNum = jsonItem.readInt("$.chapterListSize") ?: 0
book.durChapterIndex = jsonItem.readInt("$.durChapter") ?: 0
book.durChapterTitle = jsonItem.readString("$.durChapterName")
book.durChapterPos = jsonItem.readInt("$.durChapterPage") ?: 0
book.durChapterTime = jsonItem.readLong("$.finalDate") ?: 0
book.group = jsonItem.readInt("$.group") ?: 0
book.intro = jsonItem.readString("$.bookInfoBean.introduce")
book.latestChapterTitle = jsonItem.readString("$.lastChapterName")
book.lastCheckCount = jsonItem.readInt("$.newChapters") ?: 0
book.order = jsonItem.readInt("$.serialNumber") ?: 0
book.useReplaceRule = jsonItem.readBool("$.useReplaceRule") == true
book.variable = jsonItem.readString("$.variable")
books.add(book)
}
App.db.bookDao().insert(*books.toTypedArray())
return books.size
}
fun importOldSource(json: String): Int {
val bookSources = mutableListOf<BookSource>()
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
OldRule.jsonToBookSource(jsonItem.jsonString())?.let {
bookSources.add(it)
}
}
App.db.bookSourceDao().insert(*bookSources.toTypedArray())
return bookSources.size
}
fun importOldReplaceRule(json: String): Int {
val replaceRules = mutableListOf<ReplaceRule>()
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet()
for ((index: Int, item: Map<String, Any>) in items.withIndex()) {
val jsonItem = jsonPath.parse(item)
val rule = ReplaceRule()
rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis().plus(index)
rule.pattern = jsonItem.readString("$.regex") ?: ""
if (rule.pattern.isEmpty() || rule.pattern in existingRules) continue
rule.name = jsonItem.readString("$.replaceSummary") ?: ""
rule.replacement = jsonItem.readString("$.replacement") ?: ""
rule.isRegex = jsonItem.readBool("$.isRegex") == true
rule.scope = jsonItem.readString("$.useTo")
rule.isEnabled = jsonItem.readBool("$.enable") == true
rule.order = jsonItem.readInt("$.serialNumber") ?: index
replaceRules.add(rule)
}
App.db.replaceRuleDao().insert(*replaceRules.toTypedArray())
return replaceRules.size
}
}

@ -23,6 +23,7 @@ import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.yesButton
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.LogUtils
import io.legado.app.utils.applyTint
import io.legado.app.utils.getPrefString
@ -142,7 +143,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference
private fun needInstallApps(callback: () -> Unit) {
fun canRequestPackageInstalls() :Boolean {
fun canRequestPackageInstalls(): Boolean {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
return requireContext().packageManager.canRequestPackageInstalls()
}
@ -158,7 +159,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference
}
}.show().applyTint()
} else {
LogUtils.d("xxx","import old")
LogUtils.d("xxx", "import old")
callback()
}
}
@ -166,13 +167,25 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
oldDataRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let {
DocumentFile.fromTreeUri(requireContext(), it)?.listFiles()?.forEach {
oldDataRequestCode -> if (resultCode == RESULT_OK) data?.data?.let { uri ->
DocumentFile.fromTreeUri(requireContext(), uri)?.listFiles()?.forEach {
when (it.name) {
"myBookShelf.json" ->
DocumentUtils.readText(requireContext(), it.uri)?.let { json ->
Restore.importOldBookshelf(json)
}
"myBookSource.json" ->
DocumentUtils.readText(requireContext(), it.uri)?.let { json ->
Restore.importOldSource(json)
}
"myBookReplaceRule.json" ->
DocumentUtils.readText(requireContext(), it.uri)?.let { json ->
Restore.importOldReplaceRule(json)
}
}
}
}
}
}
}
Loading…
Cancel
Save