用阅读打开朗读引擎文件也可以导入

pull/1486/head
gedoor 3 years ago
parent fd1a1ae9b8
commit 7bd914b111
  1. 85
      app/src/main/java/io/legado/app/ui/association/BaseAssociationViewModel.kt
  2. 12
      app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt
  3. 13
      app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt
  4. 77
      app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.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<TxtTocRule>(json)?.let {
appDb.txtTocRuleDao.insert(*it.toTypedArray())
} ?: throw NoStackTraceException("格式不对")
} else {
GSON.fromJsonObject<TxtTocRule>(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<ThemeConfig.Config>(json)?.forEach {
ThemeConfig.addConfig(it)
} ?: throw NoStackTraceException("格式不对")
} else {
GSON.fromJsonObject<ThemeConfig.Config>(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)
)
}
}
}

@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.activity.viewModels import androidx.activity.viewModels
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.databinding.ActivityTranslucenceBinding 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.ui.book.read.ReadBookActivity
import io.legado.app.utils.showDialogFragment import io.legado.app.utils.showDialogFragment
import io.legado.app.utils.startActivity import io.legado.app.utils.startActivity
@ -50,7 +51,16 @@ class FileAssociationActivity :
finish() finish()
}) })
intent.data?.let { data -> 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()
}
} }
} }

@ -4,16 +4,14 @@ import android.app.Application
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel
import io.legado.app.model.NoStackTraceException import io.legado.app.model.NoStackTraceException
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.isJson import io.legado.app.utils.isJson
import io.legado.app.utils.readText import io.legado.app.utils.readText
import timber.log.Timber import timber.log.Timber
import java.io.File import java.io.File
class FileAssociationViewModel(application: Application) : BaseViewModel(application) { class FileAssociationViewModel(application: Application) : BaseAssociationViewModel(application) {
val onLineImportLive = MutableLiveData<Uri>() val onLineImportLive = MutableLiveData<Uri>()
val importBookSourceLive = MutableLiveData<String>() val importBookSourceLive = MutableLiveData<String>()
val importRssSourceLive = MutableLiveData<String>() val importRssSourceLive = MutableLiveData<String>()
@ -21,7 +19,7 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica
val openBookLiveData = MutableLiveData<String>() val openBookLiveData = MutableLiveData<String>()
val errorLiveData = MutableLiveData<String>() val errorLiveData = MutableLiveData<String>()
fun dispatchIndent(uri: Uri) { fun dispatchIndent(uri: Uri, finally: (title: String, msg: String) -> Unit) {
execute { execute {
//如果是普通的url,需要根据返回的内容判断是什么 //如果是普通的url,需要根据返回的内容判断是什么
if (uri.scheme == "file" || uri.scheme == "content") { if (uri.scheme == "file" || uri.scheme == "content") {
@ -46,6 +44,13 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica
importReplaceRuleLive.postValue(it) importReplaceRuleLive.postValue(it)
return@execute 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) val book = LocalBook.importFile(uri)

@ -3,23 +3,13 @@ package io.legado.app.ui.association
import android.app.Application import android.app.Application
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.R 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.ReadBookConfig
import io.legado.app.help.ThemeConfig
import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text 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 import okhttp3.MediaType.Companion.toMediaType
class OnLineImportViewModel(app: Application) : BaseViewModel(app) { class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) {
val successLive = MutableLiveData<Pair<String, String>>() val successLive = MutableLiveData<Pair<String, String>>()
val errorLive = MutableLiveData<String>() val errorLive = MutableLiveData<String>()
@ -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<TxtTocRule>(json)?.let {
appDb.txtTocRuleDao.insert(*it.toTypedArray())
} ?: throw NoStackTraceException("格式不对")
} else {
GSON.fromJsonObject<TxtTocRule>(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<ThemeConfig.Config>(json)?.forEach {
ThemeConfig.addConfig(it)
} ?: throw NoStackTraceException("格式不对")
} else {
GSON.fromJsonObject<ThemeConfig.Config>(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) { fun importReadConfig(bytes: ByteArray, finally: (title: String, msg: String) -> Unit) {
execute { execute {
val config = ReadBookConfig.import(bytes) val config = ReadBookConfig.import(bytes)

Loading…
Cancel
Save