优化文件结构

pull/433/head
gedoor 4 years ago
parent 0c07b6fe42
commit 4db4a67ad1
  1. 0
      app/src/main/assets/defaultData/httpTTS.json
  2. 0
      app/src/main/assets/defaultData/readConfig.json
  3. 0
      app/src/main/assets/defaultData/themeConfig.json
  4. 0
      app/src/main/assets/defaultData/txtTocRule.json
  5. 47
      app/src/main/java/io/legado/app/help/DefaultData.kt
  6. 19
      app/src/main/java/io/legado/app/help/DefaultValueHelp.kt
  7. 8
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  8. 7
      app/src/main/java/io/legado/app/help/ThemeConfig.kt
  9. 9
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  10. 13
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  11. 20
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  12. 4
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt
  13. 12
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt
  14. 6
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt
  15. 6
      app/src/main/java/io/legado/app/ui/main/MainViewModel.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<HttpTTS>(json)!!
}
val defaultReadConfigs by lazy {
val json = String(
App.INSTANCE.assets.open("defaultData${File.separator}${ReadBookConfig.configFileName}")
.readBytes()
)
GSON.fromJsonArray<ReadBookConfig.Config>(json)!!
}
val defaultTxtTocRules by lazy {
val json = String(
App.INSTANCE.assets.open("defaultData${File.separator}$txtTocRuleFileName")
.readBytes()
)
GSON.fromJsonArray<TxtTocRule>(json)!!
}
val defaultThemeConfigs by lazy {
val json = String(
App.INSTANCE.assets.open("defaultData${File.separator}${ThemeConfig.configFileName}")
.readBytes()
)
GSON.fromJsonArray<ThemeConfig.Config>(json)!!
}
}

@ -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<HttpTTS>(json)?.let {
App.db.httpTTSDao().insert(*it.toTypedArray())
}
}
}

@ -26,10 +26,6 @@ object ReadBookConfig {
val shareConfigFilePath = FileUtils.getPath(App.INSTANCE.filesDir, shareConfigFileName)
val configList: ArrayList<Config> = arrayListOf()
lateinit var shareConfig: Config
private val defaultConfigs by lazy {
val json = String(App.INSTANCE.assets.open(configFileName).readBytes())
GSON.fromJsonArray<Config>(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()

@ -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<Config>(json)!!
}
val configList = arrayListOf<Config>()
init {
@ -24,7 +21,7 @@ object ThemeConfig {
}
fun upConfig() {
(getConfigs() ?: defaultConfigs).let {
(getConfigs() ?: DefaultData.defaultThemeConfigs).let {
configList.clear()
configList.addAll(it)
}

@ -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)

@ -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<ReplaceRule>(path, "replaceRule.json")?.let {
App.db.replaceRuleDao().insert(*it.toTypedArray())
}
fileToListT<TxtTocRule>(path, "txtTocRule.json")?.let {
fileToListT<TxtTocRule>(path, DefaultData.txtTocRuleFileName)?.let {
App.db.txtTocRule().insert(*it.toTypedArray())
}
fileToListT<HttpTTS>(path, DefaultData.httpTtsFileName)?.let {
App.db.httpTTSDao().insert(*it.toTypedArray())
}
fileToListT<ReadRecord>(path, "readRecord.json")?.let {
it.forEach { readRecord ->
//判断是不是本机记录
@ -149,9 +149,6 @@ object Restore {
}
}
}
fileToListT<HttpTTS>(path, "httpTTS.json")?.let {
App.db.httpTTSDao().insert(*it.toTypedArray())
}
}
}

@ -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<TxtTocRule> {
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<TxtTocRule> {
App.INSTANCE.assets.open("txtTocRule.json").readBytes().let { byteArray ->
GSON.fromJsonArray<TxtTocRule>(String(byteArray))?.let { txtTocRules ->
App.db.txtTocRule().insert(*txtTocRules.toTypedArray())
return txtTocRules.filter {
it.enable
}
}
}
return emptyList()
}
}
}

@ -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()
}

@ -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<TxtTocRule>(json)?.let {
App.db.txtTocRule().insert(*it.toTypedArray())
GSON.fromJsonArray<HttpTTS>(json)?.let {
App.db.httpTTSDao().insert(*it.toTypedArray())
}
}
}.onSuccess {

@ -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())
}
}
}

@ -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())
}
}
}
}

Loading…
Cancel
Save