From 2ea0c313274cf1e0ab7acadab53309adeecd09e9 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 22 Jul 2021 20:46:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=BC=E5=85=A5=E4=B8=BB?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/ThemeConfig.kt | 2 +- .../ui/association/OnLineImportViewModel.kt | 50 ++++++++++++++++--- 2 files changed, 43 insertions(+), 9 deletions(-) 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 f0fdab71b..e7a88d40f 100644 --- a/app/src/main/java/io/legado/app/help/ThemeConfig.kt +++ b/app/src/main/java/io/legado/app/help/ThemeConfig.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 diff --git a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt index 2e07cb09a..7488a895b 100644 --- a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt @@ -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(json)?.let { - appDb.txtTocRuleDao.insert(*it.toTypedArray()) - } ?: throw Exception("格式不对") + if (json.isJsonArray()) { + GSON.fromJsonArray(json)?.let { + appDb.txtTocRuleDao.insert(*it.toTypedArray()) + } ?: throw Exception("格式不对") + } else { + GSON.fromJsonObject(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(json)?.let { - appDb.httpTTSDao.insert(*it.toTypedArray()) - return@execute it.size - } ?: throw Exception("格式不对") + if (json.isJsonArray()) { + GSON.fromJsonArray(json)?.let { + appDb.httpTTSDao.insert(*it.toTypedArray()) + return@execute it.size + } ?: throw Exception("格式不对") + } else { + GSON.fromJsonObject(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(json)?.forEach { + ThemeConfig.addConfig(it) + } ?: throw Exception("格式不对") + } else { + GSON.fromJsonObject(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 ?: "未知错误") + } } } \ No newline at end of file