diff --git a/app/src/main/java/io/legado/app/ui/association/BaseAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/association/BaseAssociationViewModel.kt new file mode 100644 index 000000000..f0e252a3e --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/association/BaseAssociationViewModel.kt @@ -0,0 +1,85 @@ +package io.legado.app.ui.association + +import android.app.Application +import io.legado.app.R +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.model.NoStackTraceException +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonArray +import io.legado.app.utils.fromJsonObject +import io.legado.app.utils.isJsonArray + +abstract class BaseAssociationViewModel(application: Application) : BaseViewModel(application) { + + + fun importTextTocRule(json: String, finally: (title: String, msg: String) -> Unit) { + execute { + if (json.isJsonArray()) { + GSON.fromJsonArray(json)?.let { + appDb.txtTocRuleDao.insert(*it.toTypedArray()) + } ?: throw NoStackTraceException("格式不对") + } else { + GSON.fromJsonObject(json)?.let { + appDb.txtTocRuleDao.insert(it) + } ?: throw NoStackTraceException("格式不对") + } + }.onSuccess { + finally.invoke(context.getString(R.string.success), "导入Txt规则成功") + }.onError { + finally.invoke( + context.getString(R.string.error), + it.localizedMessage ?: context.getString(R.string.unknown_error) + ) + } + } + + fun importHttpTTS(json: String, finally: (title: String, msg: String) -> Unit) { + execute { + if (json.isJsonArray()) { + HttpTTS.fromJsonArray(json).let { + appDb.httpTTSDao.insert(*it.toTypedArray()) + return@execute it.size + } + } else { + HttpTTS.fromJson(json)?.let { + appDb.httpTTSDao.insert(it) + return@execute 1 + } ?: throw NoStackTraceException("格式不对") + } + }.onSuccess { + finally.invoke(context.getString(R.string.success), "导入${it}朗读引擎") + }.onError { + finally.invoke( + context.getString(R.string.error), + it.localizedMessage ?: context.getString(R.string.unknown_error) + ) + } + } + + fun importTheme(json: String, finally: (title: String, msg: String) -> Unit) { + execute { + if (json.isJsonArray()) { + GSON.fromJsonArray(json)?.forEach { + ThemeConfig.addConfig(it) + } ?: throw NoStackTraceException("格式不对") + } else { + GSON.fromJsonObject(json)?.let { + ThemeConfig.addConfig(it) + } ?: throw NoStackTraceException("格式不对") + } + }.onSuccess { + finally.invoke(context.getString(R.string.success), "导入主题成功") + }.onError { + finally.invoke( + context.getString(R.string.error), + it.localizedMessage ?: context.getString(R.string.unknown_error) + ) + } + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt index 9e9861025..4b0391a80 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt @@ -4,6 +4,7 @@ import android.os.Bundle import androidx.activity.viewModels import io.legado.app.base.VMBaseActivity import io.legado.app.databinding.ActivityTranslucenceBinding +import io.legado.app.lib.dialogs.alert import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.utils.showDialogFragment import io.legado.app.utils.startActivity @@ -50,7 +51,16 @@ class FileAssociationActivity : finish() }) intent.data?.let { data -> - viewModel.dispatchIndent(data) + viewModel.dispatchIndent(data, this::finallyDialog) + } + } + + private fun finallyDialog(title: String, msg: String) { + alert(title, msg) { + okButton() + onDismiss { + finish() + } } } diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt index 53b3164b0..73880c671 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt @@ -4,16 +4,14 @@ import android.app.Application import android.net.Uri import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData -import io.legado.app.base.BaseViewModel import io.legado.app.model.NoStackTraceException import io.legado.app.model.localBook.LocalBook import io.legado.app.utils.isJson - import io.legado.app.utils.readText import timber.log.Timber import java.io.File -class FileAssociationViewModel(application: Application) : BaseViewModel(application) { +class FileAssociationViewModel(application: Application) : BaseAssociationViewModel(application) { val onLineImportLive = MutableLiveData() val importBookSourceLive = MutableLiveData() val importRssSourceLive = MutableLiveData() @@ -21,7 +19,7 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica val openBookLiveData = MutableLiveData() val errorLiveData = MutableLiveData() - fun dispatchIndent(uri: Uri) { + fun dispatchIndent(uri: Uri, finally: (title: String, msg: String) -> Unit) { execute { //如果是普通的url,需要根据返回的内容判断是什么 if (uri.scheme == "file" || uri.scheme == "content") { @@ -46,6 +44,13 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica importReplaceRuleLive.postValue(it) return@execute } + content.contains("themeName") -> + importTheme(content, finally) + content.contains("name") && content.contains("rule") -> + importTextTocRule(content, finally) + content.contains("name") && content.contains("url") -> + importHttpTTS(content, finally) + else -> errorLiveData.postValue("格式不对") } } val book = LocalBook.importFile(uri) 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 29f06ddeb..245179b8a 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 @@ -3,23 +3,13 @@ package io.legado.app.ui.association import android.app.Application import androidx.lifecycle.MutableLiveData import io.legado.app.R -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.ReadBookConfig -import io.legado.app.help.ThemeConfig import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.text -import io.legado.app.model.NoStackTraceException -import io.legado.app.utils.GSON -import io.legado.app.utils.fromJsonArray -import io.legado.app.utils.fromJsonObject -import io.legado.app.utils.isJsonArray import okhttp3.MediaType.Companion.toMediaType -class OnLineImportViewModel(app: Application) : BaseViewModel(app) { +class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) { val successLive = MutableLiveData>() val errorLive = MutableLiveData() @@ -52,71 +42,6 @@ class OnLineImportViewModel(app: Application) : BaseViewModel(app) { } } - fun importTextTocRule(json: String, finally: (title: String, msg: String) -> Unit) { - execute { - if (json.isJsonArray()) { - GSON.fromJsonArray(json)?.let { - appDb.txtTocRuleDao.insert(*it.toTypedArray()) - } ?: throw NoStackTraceException("格式不对") - } else { - GSON.fromJsonObject(json)?.let { - appDb.txtTocRuleDao.insert(it) - } ?: throw NoStackTraceException("格式不对") - } - }.onSuccess { - finally.invoke(context.getString(R.string.success), "导入Txt规则成功") - }.onError { - finally.invoke( - context.getString(R.string.error), - it.localizedMessage ?: context.getString(R.string.unknown_error) - ) - } - } - - fun importHttpTTS(json: String, finally: (title: String, msg: String) -> Unit) { - execute { - if (json.isJsonArray()) { - HttpTTS.fromJsonArray(json).let { - appDb.httpTTSDao.insert(*it.toTypedArray()) - return@execute it.size - } - } else { - HttpTTS.fromJson(json)?.let { - appDb.httpTTSDao.insert(it) - return@execute 1 - } ?: throw NoStackTraceException("格式不对") - } - }.onSuccess { - finally.invoke(context.getString(R.string.success), "导入${it}朗读引擎") - }.onError { - finally.invoke( - context.getString(R.string.error), - it.localizedMessage ?: context.getString(R.string.unknown_error) - ) - } - } - - fun importTheme(json: String, finally: (title: String, msg: String) -> Unit) { - execute { - if (json.isJsonArray()) { - GSON.fromJsonArray(json)?.forEach { - ThemeConfig.addConfig(it) - } ?: throw NoStackTraceException("格式不对") - } else { - GSON.fromJsonObject(json)?.let { - ThemeConfig.addConfig(it) - } ?: throw NoStackTraceException("格式不对") - } - }.onSuccess { - finally.invoke(context.getString(R.string.success), "导入主题成功") - }.onError { - finally.invoke( - context.getString(R.string.error), - it.localizedMessage ?: context.getString(R.string.unknown_error) - ) - } - } - fun importReadConfig(bytes: ByteArray, finally: (title: String, msg: String) -> Unit) { execute { val config = ReadBookConfig.import(bytes)