pull/1171/head
gedoor 3 years ago
parent 20fa2bfc19
commit 3906e1a6df
  1. 8
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt
  2. 9
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleDialog.kt
  3. 8
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt
  4. 30
      app/src/main/java/io/legado/app/ui/association/OnLineImportActivity.kt
  5. 105
      app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt
  6. 1
      app/src/main/res/values-es-rES/strings.xml
  7. 1
      app/src/main/res/values-ja-rJP/strings.xml
  8. 1
      app/src/main/res/values-pt-rBR/strings.xml
  9. 1
      app/src/main/res/values-zh-rHK/strings.xml
  10. 1
      app/src/main/res/values-zh-rTW/strings.xml
  11. 1
      app/src/main/res/values-zh/strings.xml
  12. 1
      app/src/main/res/values/strings.xml

@ -135,12 +135,10 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList
dismiss()
return
}
if (source.isAbsUrl()) {
viewModel.importSource(source)
} else {
IntentDataHelp.getData<String>(source)?.let {
when {
source.isAbsUrl() || source.isJson() -> viewModel.importSource(source)
else -> IntentDataHelp.getData<String>(source)?.let {
viewModel.importSource(it)
return
}
}
}

@ -19,6 +19,7 @@ import io.legado.app.databinding.ItemSourceImportBinding
import io.legado.app.help.IntentDataHelp
import io.legado.app.ui.widget.dialog.WaitDialog
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isJson
import io.legado.app.utils.viewbindingdelegate.viewBinding
import io.legado.app.utils.visible
@ -121,12 +122,10 @@ class ImportReplaceRuleDialog : BaseDialogFragment() {
dismiss()
return
}
if (source.isAbsUrl()) {
viewModel.import(source)
} else {
IntentDataHelp.getData<String>(source)?.let {
when {
source.isAbsUrl() || source.isJson() -> viewModel.import(source)
else -> IntentDataHelp.getData<String>(source)?.let {
viewModel.import(it)
return
}
}
}

@ -133,12 +133,10 @@ class ImportRssSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListe
dismiss()
return
}
if (source.isAbsUrl()) {
viewModel.importSource(source)
} else {
IntentDataHelp.getData<String>(source)?.let {
when {
source.isAbsUrl() || source.isJson() -> viewModel.importSource(source)
else -> IntentDataHelp.getData<String>(source)?.let {
viewModel.importSource(it)
return
}
}
}

@ -2,6 +2,7 @@ package io.legado.app.ui.association
import android.os.Bundle
import androidx.activity.viewModels
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.Theme
import io.legado.app.databinding.ActivityTranslucenceBinding
@ -20,6 +21,19 @@ class OnLineImportActivity :
override val viewModel by viewModels<OnLineImportViewModel>()
override fun onActivityCreated(savedInstanceState: Bundle?) {
viewModel.successLive.observe(this) {
when (it.first) {
"bookSource" -> ImportBookSourceDialog
.start(supportFragmentManager, it.second, true)
"rssSource" -> ImportRssSourceDialog
.start(supportFragmentManager, it.second, true)
"replaceRule" -> ImportReplaceRuleDialog
.start(supportFragmentManager, it.second, true)
}
}
viewModel.errorLive.observe(this) {
finallyDialog(getString(R.string.error), it)
}
intent.data?.let {
val url = it.getQueryParameter("src")
if (url.isNullOrBlank()) {
@ -30,10 +44,18 @@ class OnLineImportActivity :
"/bookSource" -> ImportBookSourceDialog.start(supportFragmentManager, url, true)
"/rssSource" -> ImportRssSourceDialog.start(supportFragmentManager, url, true)
"/replaceRule" -> ImportReplaceRuleDialog.start(supportFragmentManager, url, true)
"/textTocRule" -> viewModel.importTextTocRule(url, this::finallyDialog)
"/httpTTS" -> viewModel.importHttpTTS(url, this::finallyDialog)
"/theme" -> viewModel.importTheme(url, this::finallyDialog)
"/readConfig" -> viewModel.importReadConfig(url, this::finallyDialog)
"/textTocRule" -> viewModel.getText(url) { json ->
viewModel.importTextTocRule(json, this::finallyDialog)
}
"/httpTTS" -> viewModel.getText(url) { json ->
viewModel.importHttpTTS(json, this::finallyDialog)
}
"/theme" -> viewModel.getText(url) { json ->
viewModel.importTheme(json, this::finallyDialog)
}
"/readConfig" -> viewModel.getBytes(url) { bytes ->
viewModel.importReadConfig(bytes, this::finallyDialog)
}
"/importonline" -> when (it.host) {
"booksource" -> ImportBookSourceDialog.start(supportFragmentManager, url, true)
"rsssource" -> ImportRssSourceDialog.start(supportFragmentManager, url, true)

@ -1,5 +1,6 @@
package io.legado.app.ui.association
import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseViewModel
@ -15,14 +16,43 @@ 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: App) : BaseViewModel(app) {
val successLive = MutableLiveData<Pair<String, String>>()
val errorLive = MutableLiveData<String>()
fun importTextTocRule(url: String, finally: (title: String, msg: String) -> Unit) {
fun getText(url: String, success: (text: String) -> Unit) {
execute {
okHttpClient.newCall {
url(url)
}.text("utf-8").let { json ->
}.text("utf-8")
}.onSuccess {
success.invoke(it)
}.onError {
errorLive.postValue(
it.localizedMessage ?: context.getString(R.string.unknown_error)
)
}
}
fun getBytes(url: String, success: (bytes: ByteArray) -> Unit) {
execute {
@Suppress("BlockingMethodInNonBlockingContext")
okHttpClient.newCall {
url(url)
}.bytes()
}.onSuccess {
success.invoke(it)
}.onError {
errorLive.postValue(
it.localizedMessage ?: context.getString(R.string.unknown_error)
)
}
}
fun importTextTocRule(json: String, finally: (title: String, msg: String) -> Unit) {
execute {
if (json.isJsonArray()) {
GSON.fromJsonArray<TxtTocRule>(json)?.let {
appDb.txtTocRuleDao.insert(*it.toTypedArray())
@ -32,19 +62,18 @@ class OnLineImportViewModel(app: App) : BaseViewModel(app) {
appDb.txtTocRuleDao.insert(it)
} ?: throw Exception("格式不对")
}
}
}.onSuccess {
finally.invoke(context.getString(R.string.success), "导入Txt规则成功")
}.onError {
finally.invoke(context.getString(R.string.error), it.localizedMessage ?: "未知错误")
finally.invoke(
context.getString(R.string.error),
it.localizedMessage ?: context.getString(R.string.unknown_error)
)
}
}
fun importHttpTTS(url: String, finally: (title: String, msg: String) -> Unit) {
fun importHttpTTS(json: String, finally: (title: String, msg: String) -> Unit) {
execute {
okHttpClient.newCall {
url(url)
}.text("utf-8").let { json ->
if (json.isJsonArray()) {
GSON.fromJsonArray<HttpTTS>(json)?.let {
appDb.httpTTSDao.insert(*it.toTypedArray())
@ -56,19 +85,18 @@ class OnLineImportViewModel(app: App) : BaseViewModel(app) {
return@execute 1
} ?: throw Exception("格式不对")
}
}
}.onSuccess {
finally.invoke(context.getString(R.string.success), "导入${it}朗读引擎")
}.onError {
finally.invoke(context.getString(R.string.error), it.localizedMessage ?: "未知错误")
finally.invoke(
context.getString(R.string.error),
it.localizedMessage ?: context.getString(R.string.unknown_error)
)
}
}
fun importTheme(url: String, finally: (title: String, msg: String) -> Unit) {
fun importTheme(json: 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)
@ -78,20 +106,18 @@ class OnLineImportViewModel(app: App) : BaseViewModel(app) {
ThemeConfig.addConfig(it)
} ?: throw Exception("格式不对")
}
}
}.onSuccess {
finally.invoke(context.getString(R.string.success), "导入主题成功")
}.onError {
finally.invoke(context.getString(R.string.error), it.localizedMessage ?: "未知错误")
finally.invoke(
context.getString(R.string.error),
it.localizedMessage ?: context.getString(R.string.unknown_error)
)
}
}
fun importReadConfig(url: String, finally: (title: String, msg: String) -> Unit) {
fun importReadConfig(bytes: ByteArray, finally: (title: String, msg: String) -> Unit) {
execute {
@Suppress("BlockingMethodInNonBlockingContext")
val bytes = okHttpClient.newCall {
url(url)
}.bytes()
val config = ReadBookConfig.import(bytes)
ReadBookConfig.configList.forEachIndexed { index, c ->
if (c.name == config.name) {
@ -104,7 +130,42 @@ class OnLineImportViewModel(app: App) : BaseViewModel(app) {
}.onSuccess {
finally.invoke(context.getString(R.string.success), "导入排版成功")
}.onError {
finally.invoke(context.getString(R.string.error), it.localizedMessage ?: "未知错误")
finally.invoke(
context.getString(R.string.error),
it.localizedMessage ?: context.getString(R.string.unknown_error)
)
}
}
fun determineType(url: String, finally: (title: String, msg: String) -> Unit) {
execute {
val rs = okHttpClient.newCall {
url(url)
}
when (rs.contentType()) {
"application/zip".toMediaType(),
"application/octet-stream".toMediaType() -> {
@Suppress("BlockingMethodInNonBlockingContext")
importReadConfig(rs.bytes(), finally)
}
else -> {
val json = rs.text("utf-8")
when {
json.contains("bookSourceUrl") ->
successLive.postValue(Pair("bookSource", json))
json.contains("sourceUrl") ->
successLive.postValue(Pair("rssSource", json))
json.contains("replacement") ->
successLive.postValue(Pair("replaceRule", json))
json.contains("themeName") ->
importTextTocRule(json, finally)
json.contains("name") && json.contains("rule") ->
importTextTocRule(json, finally)
json.contains("name") && json.contains("url") ->
importTextTocRule(json, finally)
}
}
}
}
}

@ -849,4 +849,5 @@
<string name="reset">Restablecer</string>
<string name="null_url">URL nula</string>
<string name="dict">字典</string>
<string name="unknown_error">未知错误</string>
</resources>

@ -849,4 +849,5 @@
<string name="reset">重置</string>
<string name="null_url">url为空</string>
<string name="dict">字典</string>
<string name="unknown_error">未知错误</string>
</resources>

@ -849,4 +849,5 @@
<string name="reset">Resetar</string>
<string name="null_url">Nenhuma url</string>
<string name="dict">Dicionários</string>
<string name="unknown_error">未知错误</string>
</resources>

@ -847,5 +847,6 @@
<string name="reset">重置</string>
<string name="null_url">url为空</string>
<string name="dict">字典</string>
<string name="unknown_error">未知错误</string>
</resources>

@ -848,5 +848,6 @@
<string name="reset">重設</string>
<string name="null_url">url为空</string>
<string name="dict">字典</string>
<string name="unknown_error">未知错误</string>
</resources>

@ -848,5 +848,6 @@
<string name="reset">重置</string>
<string name="null_url">url为空</string>
<string name="dict">字典</string>
<string name="unknown_error">未知错误</string>
</resources>

@ -849,5 +849,6 @@
<string name="reset">Reset</string>
<string name="null_url">Null url</string>
<string name="dict">字典</string>
<string name="unknown_error">未知错误</string>
</resources>

Loading…
Cancel
Save