添加导入主题

pull/1155/head
gedoor 3 years ago
parent bb0eb83e61
commit 2ea0c31327
  1. 2
      app/src/main/java/io/legado/app/help/ThemeConfig.kt
  2. 50
      app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt

@ -76,7 +76,7 @@ object ThemeConfig {
return false
}
private fun addConfig(newConfig: Config) {
fun addConfig(newConfig: Config) {
configList.forEachIndexed { index, config ->
if (newConfig.themeName == config.themeName) {
configList[index] = newConfig

@ -6,11 +6,14 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb
import io.legado.app.data.entities.HttpTTS
import io.legado.app.data.entities.TxtTocRule
import io.legado.app.help.ThemeConfig
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.isJsonArray
class OnLineImportViewModel(app: App) : BaseViewModel(app) {
@ -19,9 +22,15 @@ class OnLineImportViewModel(app: App) : BaseViewModel(app) {
okHttpClient.newCall {
url(url)
}.text("utf-8").let { json ->
GSON.fromJsonArray<TxtTocRule>(json)?.let {
appDb.txtTocRuleDao.insert(*it.toTypedArray())
} ?: throw Exception("格式不对")
if (json.isJsonArray()) {
GSON.fromJsonArray<TxtTocRule>(json)?.let {
appDb.txtTocRuleDao.insert(*it.toTypedArray())
} ?: throw Exception("格式不对")
} else {
GSON.fromJsonObject<TxtTocRule>(json)?.let {
appDb.txtTocRuleDao.insert(it)
} ?: throw Exception("格式不对")
}
}
}.onSuccess {
finally.invoke(context.getString(R.string.success), "导入Txt规则成功")
@ -35,10 +44,17 @@ class OnLineImportViewModel(app: App) : BaseViewModel(app) {
okHttpClient.newCall {
url(url)
}.text("utf-8").let { json ->
GSON.fromJsonArray<HttpTTS>(json)?.let {
appDb.httpTTSDao.insert(*it.toTypedArray())
return@execute it.size
} ?: throw Exception("格式不对")
if (json.isJsonArray()) {
GSON.fromJsonArray<HttpTTS>(json)?.let {
appDb.httpTTSDao.insert(*it.toTypedArray())
return@execute it.size
} ?: throw Exception("格式不对")
} else {
GSON.fromJsonObject<HttpTTS>(json)?.let {
appDb.httpTTSDao.insert(it)
return@execute 1
} ?: throw Exception("格式不对")
}
}
}.onSuccess {
finally.invoke(context.getString(R.string.success), "导入${it}朗读引擎")
@ -48,7 +64,25 @@ class OnLineImportViewModel(app: App) : BaseViewModel(app) {
}
fun importTheme(url: String, finally: (title: String, msg: String) -> Unit) {
execute {
okHttpClient.newCall {
url(url)
}.text("utf-8").let { json ->
if (json.isJsonArray()) {
GSON.fromJsonArray<ThemeConfig.Config>(json)?.forEach {
ThemeConfig.addConfig(it)
} ?: throw Exception("格式不对")
} else {
GSON.fromJsonObject<ThemeConfig.Config>(json)?.let {
ThemeConfig.addConfig(it)
} ?: throw Exception("格式不对")
}
}
}.onSuccess {
finally.invoke(context.getString(R.string.success), "导入主题成功")
}.onError {
finally.invoke(context.getString(R.string.error), it.localizedMessage ?: "未知错误")
}
}
}
Loading…
Cancel
Save