From 4db4a67ad12912b4731e0d7f985f53a45953e17c Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 19 Oct 2020 11:47:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../assets/{ => defaultData}/httpTTS.json | 0 .../assets/{ => defaultData}/readConfig.json | 0 .../assets/{ => defaultData}/themeConfig.json | 0 .../assets/{ => defaultData}/txtTocRule.json | 0 .../java/io/legado/app/help/DefaultData.kt | 47 +++++++++++++++++++ .../io/legado/app/help/DefaultValueHelp.kt | 19 -------- .../java/io/legado/app/help/ReadBookConfig.kt | 8 +--- .../java/io/legado/app/help/ThemeConfig.kt | 7 +-- .../java/io/legado/app/help/storage/Backup.kt | 9 ++-- .../io/legado/app/help/storage/Restore.kt | 13 ++--- .../app/model/localBook/AnalyzeTxtFile.kt | 20 +++----- .../ui/book/read/config/SpeakEngineDialog.kt | 4 ++ .../book/read/config/SpeakEngineViewModel.kt | 12 +++-- .../ui/book/read/config/TocRegexViewModel.kt | 6 ++- .../io/legado/app/ui/main/MainViewModel.kt | 6 ++- 15 files changed, 87 insertions(+), 64 deletions(-) rename app/src/main/assets/{ => defaultData}/httpTTS.json (100%) rename app/src/main/assets/{ => defaultData}/readConfig.json (100%) rename app/src/main/assets/{ => defaultData}/themeConfig.json (100%) rename app/src/main/assets/{ => defaultData}/txtTocRule.json (100%) create mode 100644 app/src/main/java/io/legado/app/help/DefaultData.kt delete mode 100644 app/src/main/java/io/legado/app/help/DefaultValueHelp.kt diff --git a/app/src/main/assets/httpTTS.json b/app/src/main/assets/defaultData/httpTTS.json similarity index 100% rename from app/src/main/assets/httpTTS.json rename to app/src/main/assets/defaultData/httpTTS.json diff --git a/app/src/main/assets/readConfig.json b/app/src/main/assets/defaultData/readConfig.json similarity index 100% rename from app/src/main/assets/readConfig.json rename to app/src/main/assets/defaultData/readConfig.json diff --git a/app/src/main/assets/themeConfig.json b/app/src/main/assets/defaultData/themeConfig.json similarity index 100% rename from app/src/main/assets/themeConfig.json rename to app/src/main/assets/defaultData/themeConfig.json diff --git a/app/src/main/assets/txtTocRule.json b/app/src/main/assets/defaultData/txtTocRule.json similarity index 100% rename from app/src/main/assets/txtTocRule.json rename to app/src/main/assets/defaultData/txtTocRule.json diff --git a/app/src/main/java/io/legado/app/help/DefaultData.kt b/app/src/main/java/io/legado/app/help/DefaultData.kt new file mode 100644 index 000000000..d1c016b90 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/DefaultData.kt @@ -0,0 +1,47 @@ +package io.legado.app.help + +import io.legado.app.App +import io.legado.app.data.entities.HttpTTS +import io.legado.app.data.entities.TxtTocRule +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonArray +import java.io.File + +object DefaultData { + + const val httpTtsFileName = "httpTTS.json" + const val txtTocRuleFileName = "txtTocRule.json" + + val defaultHttpTTS by lazy { + val json = + String( + App.INSTANCE.assets.open("defaultData${File.separator}$httpTtsFileName") + .readBytes() + ) + GSON.fromJsonArray(json)!! + } + + val defaultReadConfigs by lazy { + val json = String( + App.INSTANCE.assets.open("defaultData${File.separator}${ReadBookConfig.configFileName}") + .readBytes() + ) + GSON.fromJsonArray(json)!! + } + + val defaultTxtTocRules by lazy { + val json = String( + App.INSTANCE.assets.open("defaultData${File.separator}$txtTocRuleFileName") + .readBytes() + ) + GSON.fromJsonArray(json)!! + } + + val defaultThemeConfigs by lazy { + val json = String( + App.INSTANCE.assets.open("defaultData${File.separator}${ThemeConfig.configFileName}") + .readBytes() + ) + GSON.fromJsonArray(json)!! + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/DefaultValueHelp.kt b/app/src/main/java/io/legado/app/help/DefaultValueHelp.kt deleted file mode 100644 index 49338e960..000000000 --- a/app/src/main/java/io/legado/app/help/DefaultValueHelp.kt +++ /dev/null @@ -1,19 +0,0 @@ -package io.legado.app.help - -import io.legado.app.App -import io.legado.app.data.entities.HttpTTS -import io.legado.app.utils.GSON -import io.legado.app.utils.fromJsonArray - -object DefaultValueHelp { - - - fun initHttpTTS() { - val json = String(App.INSTANCE.assets.open("httpTTS.json").readBytes()) - GSON.fromJsonArray(json)?.let { - App.db.httpTTSDao().insert(*it.toTypedArray()) - } - } - - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt index 94eb04ee5..9ad5a9250 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -26,10 +26,6 @@ object ReadBookConfig { val shareConfigFilePath = FileUtils.getPath(App.INSTANCE.filesDir, shareConfigFileName) val configList: ArrayList = arrayListOf() lateinit var shareConfig: Config - private val defaultConfigs by lazy { - val json = String(App.INSTANCE.assets.open(configFileName).readBytes()) - GSON.fromJsonArray(json)!! - } var durConfig get() = getConfig(styleSelect) set(value) { @@ -68,7 +64,7 @@ object ReadBookConfig { e.printStackTrace() } } - (configs ?: defaultConfigs).let { + (configs ?: DefaultData.defaultReadConfigs).let { configList.clear() configList.addAll(it) } @@ -131,7 +127,7 @@ object ReadBookConfig { } private fun resetAll() { - defaultConfigs.let { + DefaultData.defaultReadConfigs.let { configList.clear() configList.addAll(it) save() diff --git a/app/src/main/java/io/legado/app/help/ThemeConfig.kt b/app/src/main/java/io/legado/app/help/ThemeConfig.kt index 5061d129f..44948e17c 100644 --- a/app/src/main/java/io/legado/app/help/ThemeConfig.kt +++ b/app/src/main/java/io/legado/app/help/ThemeConfig.kt @@ -13,10 +13,7 @@ import java.io.File object ThemeConfig { const val configFileName = "themeConfig.json" val configFilePath = FileUtils.getPath(App.INSTANCE.filesDir, configFileName) - private val defaultConfigs by lazy { - val json = String(App.INSTANCE.assets.open(configFileName).readBytes()) - GSON.fromJsonArray(json)!! - } + val configList = arrayListOf() init { @@ -24,7 +21,7 @@ object ThemeConfig { } fun upConfig() { - (getConfigs() ?: defaultConfigs).let { + (getConfigs() ?: DefaultData.defaultThemeConfigs).let { configList.clear() configList.addAll(it) } diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index 6e6cb6791..d0ecac945 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -5,6 +5,7 @@ import android.net.Uri import androidx.documentfile.provider.DocumentFile import io.legado.app.App import io.legado.app.constant.PreferKey +import io.legado.app.help.DefaultData import io.legado.app.help.ReadBookConfig import io.legado.app.help.ThemeConfig import io.legado.app.help.coroutine.Coroutine @@ -31,9 +32,9 @@ object Backup { "rssSource.json", "rssStar.json", "replaceRule.json", - "txtTocRule.json", "readRecord.json", - "httpTTS.json", + DefaultData.txtTocRuleFileName, + DefaultData.httpTtsFileName, ReadBookConfig.configFileName, ReadBookConfig.shareConfigFileName, ThemeConfig.configFileName, @@ -62,9 +63,9 @@ object Backup { writeListToJson(App.db.rssSourceDao().all, "rssSource.json", backupPath) writeListToJson(App.db.rssStarDao().all, "rssStar.json", backupPath) writeListToJson(App.db.replaceRuleDao().all, "replaceRule.json", backupPath) - writeListToJson(App.db.txtTocRule().all, "txtTocRule.json", backupPath) writeListToJson(App.db.readRecordDao().all, "readRecord.json", backupPath) - writeListToJson(App.db.httpTTSDao().all, "httpTTS.json", backupPath) + writeListToJson(App.db.txtTocRule().all, DefaultData.txtTocRuleFileName, backupPath) + writeListToJson(App.db.httpTTSDao().all, DefaultData.httpTtsFileName, backupPath) GSON.toJson(ReadBookConfig.configList).let { FileUtils.createFileIfNotExist(backupPath + File.separator + ReadBookConfig.configFileName) .writeText(it) diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 174367557..1c837c9b5 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -13,10 +13,7 @@ import io.legado.app.R import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.data.entities.* -import io.legado.app.help.AppConfig -import io.legado.app.help.LauncherIconHelp -import io.legado.app.help.ReadBookConfig -import io.legado.app.help.ThemeConfig +import io.legado.app.help.* import io.legado.app.service.help.ReadBook import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.utils.* @@ -132,9 +129,12 @@ object Restore { fileToListT(path, "replaceRule.json")?.let { App.db.replaceRuleDao().insert(*it.toTypedArray()) } - fileToListT(path, "txtTocRule.json")?.let { + fileToListT(path, DefaultData.txtTocRuleFileName)?.let { App.db.txtTocRule().insert(*it.toTypedArray()) } + fileToListT(path, DefaultData.httpTtsFileName)?.let { + App.db.httpTTSDao().insert(*it.toTypedArray()) + } fileToListT(path, "readRecord.json")?.let { it.forEach { readRecord -> //判断是不是本机记录 @@ -149,9 +149,6 @@ object Restore { } } } - fileToListT(path, "httpTTS.json")?.let { - App.db.httpTTSDao().insert(*it.toTypedArray()) - } } } diff --git a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt index 6dfa74594..77689d8a5 100644 --- a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt @@ -5,6 +5,7 @@ import io.legado.app.App import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.TxtTocRule +import io.legado.app.help.DefaultData import io.legado.app.utils.* import java.io.File import java.io.RandomAccessFile @@ -281,24 +282,17 @@ class AnalyzeTxtFile { } private fun getTocRules(): List { - val rules = App.db.txtTocRule().enabled + var rules = App.db.txtTocRule().enabled if (rules.isEmpty()) { - return getDefaultEnabledRules() + rules = DefaultData.defaultTxtTocRules.apply { + App.db.txtTocRule().insert(*this.toTypedArray()) + }.filter { + it.enable + } } return rules } - fun getDefaultEnabledRules(): List { - App.INSTANCE.assets.open("txtTocRule.json").readBytes().let { byteArray -> - GSON.fromJsonArray(String(byteArray))?.let { txtTocRules -> - App.db.txtTocRule().insert(*txtTocRules.toTypedArray()) - return txtTocRules.filter { - it.enable - } - } - } - return emptyList() - } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt index 6ee1d5bda..fe1d5be76 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt @@ -24,6 +24,7 @@ import io.legado.app.lib.dialogs.customView import io.legado.app.lib.dialogs.okButton import io.legado.app.lib.theme.primaryColor import io.legado.app.service.help.ReadAloud +import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.* import kotlinx.android.synthetic.main.dialog_http_tts_edit.view.* import kotlinx.android.synthetic.main.dialog_recycler_view.* @@ -126,6 +127,9 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener ReadAloud.upReadAloudClass() } } + neutralButton(R.string.help) { + TextDialog.show(childFragmentManager, "") + } }.show().applyTint() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt index 490eb6efd..bc30c346b 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt @@ -3,8 +3,8 @@ package io.legado.app.ui.book.read.config import android.app.Application import io.legado.app.App import io.legado.app.base.BaseViewModel -import io.legado.app.data.entities.TxtTocRule -import io.legado.app.help.DefaultValueHelp +import io.legado.app.data.entities.HttpTTS +import io.legado.app.help.DefaultData import io.legado.app.help.http.HttpHelper import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonArray @@ -13,15 +13,17 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application fun importDefault() { execute { - DefaultValueHelp.initHttpTTS() + DefaultData.defaultHttpTTS.let { + App.db.httpTTSDao().insert(*it.toTypedArray()) + } } } fun importOnLine(url: String, finally: (msg: String) -> Unit) { execute { HttpHelper.simpleGetAsync(url)?.let { json -> - GSON.fromJsonArray(json)?.let { - App.db.txtTocRule().insert(*it.toTypedArray()) + GSON.fromJsonArray(json)?.let { + App.db.httpTTSDao().insert(*it.toTypedArray()) } } }.onSuccess { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt index 140a781e4..cebc521b8 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt @@ -4,8 +4,8 @@ import android.app.Application import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.TxtTocRule +import io.legado.app.help.DefaultData import io.legado.app.help.http.HttpHelper -import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonArray @@ -23,7 +23,9 @@ class TocRegexViewModel(application: Application) : BaseViewModel(application) { fun importDefault() { execute { App.db.txtTocRule().deleteDefault() - AnalyzeTxtFile.getDefaultEnabledRules() + DefaultData.defaultTxtTocRules.let { + App.db.txtTocRule().insert(*it.toTypedArray()) + } } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 8f45c4a9c..904df39e4 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -9,7 +9,7 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.RssSource import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp -import io.legado.app.help.DefaultValueHelp +import io.legado.app.help.DefaultData import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.Restore import io.legado.app.model.webBook.WebBook @@ -162,7 +162,9 @@ class MainViewModel(application: Application) : BaseViewModel(application) { execute { FileUtils.deleteFile(FileUtils.getPath(context.cacheDir, "Fonts")) if (App.db.httpTTSDao().count == 0) { - DefaultValueHelp.initHttpTTS() + DefaultData.defaultHttpTTS.let { + App.db.httpTTSDao().insert(*it.toTypedArray()) + } } } }