diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt index 5899cd3d8..3a9024aa7 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt @@ -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>("$.sourceUrls") - if (!urls.isNullOrEmpty()) { - urls.forEach { - importSourceUrl(it) - } - } - }.onFailure { - GSON.fromJsonArray(mText).getOrThrow().let { - val source = it.firstOrNull() ?: return@let - if (source.sourceUrl.isEmpty()) { - throw NoStackTraceException("不是订阅源") - } - allSources.addAll(it) - } - } - - mText.isJsonArray() -> { - GSON.fromJsonArray(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>("$.sourceUrls") + if (!urls.isNullOrEmpty()) { + urls.forEach { + importSourceUrl(it) + } + } + }.onFailure { + GSON.fromJsonArray(mText).getOrThrow().let { + val source = it.firstOrNull() ?: return@let + if (source.sourceUrl.isEmpty()) { + throw NoStackTraceException("不是订阅源") + } + allSources.addAll(it) + } + } + + mText.isJsonArray() -> { + GSON.fromJsonArray(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")) {