|
|
|
@ -16,6 +16,7 @@ import io.legado.app.App |
|
|
|
|
import io.legado.app.R |
|
|
|
|
import io.legado.app.base.BaseActivity |
|
|
|
|
import io.legado.app.constant.AppConst.APP_TAG |
|
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
|
import io.legado.app.data.entities.ReplaceRule |
|
|
|
|
import io.legado.app.help.permission.Permissions |
|
|
|
|
import io.legado.app.help.permission.PermissionsCompat |
|
|
|
@ -107,30 +108,85 @@ class MainActivity : BaseActivity<MainDataBinding, MainViewModel>(), NavigationV |
|
|
|
|
.build() |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// 导入书架 |
|
|
|
|
val shelfFile = File(yuedu, "myBookShelf.json") |
|
|
|
|
val books = mutableListOf<Book>() |
|
|
|
|
if (shelfFile.exists()) try { |
|
|
|
|
doAsync { |
|
|
|
|
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.descUrl = jsonItem.readString("$.noteUrl") ?: "" |
|
|
|
|
if (book.descUrl.isBlank()) continue |
|
|
|
|
book.name = jsonItem.readString("$.bookInfoBean.name") |
|
|
|
|
if (book.descUrl in existingBooks) { |
|
|
|
|
Log.d(APP_TAG, "Found existing book: ${book.name}") |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
book.author = jsonItem.readString("$.bookInfoBean.author") |
|
|
|
|
book.type = if (jsonItem.readString("$.bookInfoBean.bookSourceType") == "AUDIO") 1 else 0 |
|
|
|
|
book.tocUrl = jsonItem.readString("$.bookInfoBean.chapterUrl") ?: book.descUrl |
|
|
|
|
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. = jsonItem.readString("$.hasUpdate") |
|
|
|
|
// book. = jsonItem.readString("$.isLoading") |
|
|
|
|
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) |
|
|
|
|
Log.d(APP_TAG, "Added ${book.name}") |
|
|
|
|
} |
|
|
|
|
App.db.bookDao().insert(*books.toTypedArray()) |
|
|
|
|
val count = books.size |
|
|
|
|
|
|
|
|
|
uiThread { |
|
|
|
|
toast(if (count > 0) "成功地导入 $count 本新书和音频" else "没有发现新书或音频") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
Log.e(APP_TAG, "Failed to import book shelf.", e) |
|
|
|
|
toast("Unable to import books:\n${e.localizedMessage}") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Replace rules |
|
|
|
|
val rFile = File(yuedu, "myBookReplaceRule.json") |
|
|
|
|
val ruleFile = File(yuedu, "myBookReplaceRule.json") |
|
|
|
|
val replaceRules = mutableListOf<ReplaceRule>() |
|
|
|
|
if (rFile.exists()) try { |
|
|
|
|
val items: List<Map<String, Any>> = jsonPath.parse(rFile.readText()).read("$.*") |
|
|
|
|
if (ruleFile.exists()) try { |
|
|
|
|
doAsync { |
|
|
|
|
val items: List<Map<String, Any>> = jsonPath.parse(ruleFile.readText()).read("$") |
|
|
|
|
val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet() |
|
|
|
|
for (item in items) { |
|
|
|
|
val jsonItem = jsonPath.parse(item) |
|
|
|
|
val rRule = ReplaceRule() |
|
|
|
|
rRule.name = jsonItem.readString("$.replaceSummary") |
|
|
|
|
rRule.pattern = jsonItem.readString("$.regex") |
|
|
|
|
if (rRule.pattern.isNullOrEmpty() || rRule.pattern in existingRules) continue |
|
|
|
|
rRule.name = jsonItem.readString("$.replaceSummary") |
|
|
|
|
rRule.replacement = jsonItem.readString("$.replacement") |
|
|
|
|
rRule.isRegex = jsonItem.readBool("$.isRegex") |
|
|
|
|
rRule.isRegex = jsonItem.readBool("$.isRegex") == true |
|
|
|
|
rRule.scope = jsonItem.readString("$.useTo") |
|
|
|
|
rRule.isEnabled = jsonItem.readBool("$.enable") |
|
|
|
|
rRule.order = jsonItem.readInt("$.serialNumber") |
|
|
|
|
rRule.isEnabled = jsonItem.readBool("$.enable") == true |
|
|
|
|
rRule.order = jsonItem.readInt("$.serialNumber") ?: 0 |
|
|
|
|
replaceRules.add(rRule) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
doAsync { |
|
|
|
|
App.db.replaceRuleDao().insert(*replaceRules.toTypedArray()) |
|
|
|
|
val count = items.size |
|
|
|
|
val count = replaceRules.size |
|
|
|
|
val maxId = App.db.replaceRuleDao().maxOrder |
|
|
|
|
uiThread { |
|
|
|
|
toast("成功地导入 $count 条净化替换规则") |
|
|
|
|
toast(if (count > 0) "成功地导入 $count 条净化替换规则" else "没有发现新的净化替换规则") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|