优化 #5566
This commit is contained in:
@@ -105,46 +105,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
|
||||
fun importSource(text: String) {
|
||||
execute {
|
||||
val mText = text.trim()
|
||||
when {
|
||||
mText.isJsonObject() -> kotlin.runCatching {
|
||||
val json = JsonPath.parse(mText)
|
||||
val urls = json.read<List<String>>("$.sourceUrls")
|
||||
if (!urls.isNullOrEmpty()) {
|
||||
urls.forEach {
|
||||
importSourceUrl(it)
|
||||
}
|
||||
}
|
||||
}.onFailure {
|
||||
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||
val source = it.firstOrNull() ?: return@let
|
||||
if (source.sourceUrl.isEmpty()) {
|
||||
throw NoStackTraceException("不是订阅源")
|
||||
}
|
||||
allSources.addAll(it)
|
||||
}
|
||||
}
|
||||
|
||||
mText.isJsonArray() -> {
|
||||
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||
val source = it.firstOrNull() ?: return@let
|
||||
if (source.sourceUrl.isEmpty()) {
|
||||
throw NoStackTraceException("不是订阅源")
|
||||
}
|
||||
allSources.addAll(it)
|
||||
}
|
||||
}
|
||||
|
||||
mText.isAbsUrl() -> {
|
||||
importSourceUrl(mText)
|
||||
}
|
||||
|
||||
mText.isUri() -> {
|
||||
importSource(mText.toUri().readText(appCtx))
|
||||
}
|
||||
|
||||
else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
|
||||
}
|
||||
importSourceAwait(text)
|
||||
}.onError {
|
||||
errorLiveData.postValue("ImportError:${it.localizedMessage}")
|
||||
AppLog.put("ImportError:${it.localizedMessage}", it)
|
||||
@@ -153,6 +114,49 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun importSourceAwait(text: String) {
|
||||
val mText = text.trim()
|
||||
when {
|
||||
mText.isJsonObject() -> kotlin.runCatching {
|
||||
val json = JsonPath.parse(mText)
|
||||
val urls = json.read<List<String>>("$.sourceUrls")
|
||||
if (!urls.isNullOrEmpty()) {
|
||||
urls.forEach {
|
||||
importSourceUrl(it)
|
||||
}
|
||||
}
|
||||
}.onFailure {
|
||||
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||
val source = it.firstOrNull() ?: return@let
|
||||
if (source.sourceUrl.isEmpty()) {
|
||||
throw NoStackTraceException("不是订阅源")
|
||||
}
|
||||
allSources.addAll(it)
|
||||
}
|
||||
}
|
||||
|
||||
mText.isJsonArray() -> {
|
||||
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||
val source = it.firstOrNull() ?: return@let
|
||||
if (source.sourceUrl.isEmpty()) {
|
||||
throw NoStackTraceException("不是订阅源")
|
||||
}
|
||||
allSources.addAll(it)
|
||||
}
|
||||
}
|
||||
|
||||
mText.isAbsUrl() -> {
|
||||
importSourceUrl(mText)
|
||||
}
|
||||
|
||||
mText.isUri() -> {
|
||||
importSourceAwait(mText.toUri().readText(appCtx))
|
||||
}
|
||||
|
||||
else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun importSourceUrl(url: String) {
|
||||
okHttpClient.newCallResponseBody {
|
||||
if (url.endsWith("#requestWithoutUA")) {
|
||||
|
||||
Reference in New Issue
Block a user