|
|
|
@ -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) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|