diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index 2fd90a887..475b3ba52 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -11,6 +11,8 @@ import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.OldRule import io.legado.app.help.storage.Restore.jsonPath import io.legado.app.utils.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import java.io.File class BookSourceViewModel(application: Application) : BaseViewModel(application) { @@ -126,7 +128,9 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) if (file.exists()) { importSource(file.readText(), finally) } else { - finally("文件无法打开") + withContext(Dispatchers.Main) { + finally("文件无法打开") + } } } } @@ -134,40 +138,45 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) fun importSource(text: String, finally: (msg: String) -> Unit) { execute { val text1 = text.trim() - if (text1.isJsonObject()) { - val json = JsonPath.parse(text1) - val urls = json.read>("$.sourceUrls") - if (!urls.isNullOrEmpty()) { + when { + text1.isJsonObject() -> { + val json = JsonPath.parse(text1) + val urls = json.read>("$.sourceUrls") var count = 0 - urls.forEach { - count += importSourceUrl(it) - } - finally("导入${count}条") - } else { - OldRule.jsonToBookSource(text1)?.let { - App.db.bookSourceDao().insert(it) + if (!urls.isNullOrEmpty()) { + urls.forEach { + count += importSourceUrl(it) + } + } else { + OldRule.jsonToBookSource(text1)?.let { + App.db.bookSourceDao().insert(it) + count = 1 + } } - finally("导入1条") + "导入${count}条" } - } else if (text1.isJsonArray()) { - val bookSources = mutableListOf() - val items: List> = jsonPath.parse(text1).read("$") - for (item in items) { - val jsonItem = jsonPath.parse(item) - OldRule.jsonToBookSource(jsonItem.jsonString())?.let { - bookSources.add(it) + text1.isJsonArray() -> { + val bookSources = mutableListOf() + val items: List> = jsonPath.parse(text1).read("$") + for (item in items) { + val jsonItem = jsonPath.parse(item) + OldRule.jsonToBookSource(jsonItem.jsonString())?.let { + bookSources.add(it) + } } + App.db.bookSourceDao().insert(*bookSources.toTypedArray()) + "导入${bookSources.size}条" } - App.db.bookSourceDao().insert(*bookSources.toTypedArray()) - finally("导入${bookSources.size}条") - } else if (text1.isAbsUrl()) { - val count = importSourceUrl(text1) - finally("导入${count}条") - } else { - finally("格式不对") + text1.isAbsUrl() -> { + val count = importSourceUrl(text1) + "导入${count}条" + } + else -> "格式不对" } }.onError { finally(it.localizedMessage) + }.onSuccess { + finally(it ?: "导入完成") } } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt index a1c8e2cdf..67d12e25a 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt @@ -2,12 +2,14 @@ package io.legado.app.ui.rss.source.manage import android.app.Application import android.text.TextUtils +import com.jayway.jsonpath.JsonPath import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.data.api.IHttpGetApi import io.legado.app.data.entities.RssSource -import io.legado.app.utils.GSON -import io.legado.app.utils.fromJsonArray -import io.legado.app.utils.splitNotBlank +import io.legado.app.help.http.HttpHelper +import io.legado.app.help.storage.Restore.jsonPath +import io.legado.app.utils.* import java.io.File class RssSourceViewModel(application: Application) : BaseViewModel(application) { @@ -108,7 +110,67 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) } } - fun importSource(sourceStr: String, finally: (msg: String) -> Unit) { + fun importSource(text: String, finally: (msg: String) -> Unit) { + execute { + val text1 = text.trim() + when { + text1.isJsonObject() -> { + val json = JsonPath.parse(text1) + val urls = json.read>("$.sourceUrls") + var count = 0 + if (!urls.isNullOrEmpty()) { + urls.forEach { + count += importSourceUrl(it) + } + } else { + GSON.fromJsonArray(text1)?.let { + App.db.rssSourceDao().insert(*it.toTypedArray()) + count = 1 + } + } + "导入${count}条" + } + text1.isJsonArray() -> { + val rssSources = mutableListOf() + val items: List> = jsonPath.parse(text1).read("$") + for (item in items) { + val jsonItem = jsonPath.parse(item) + GSON.fromJsonObject(jsonItem.jsonString())?.let { + rssSources.add(it) + } + } + App.db.rssSourceDao().insert(*rssSources.toTypedArray()) + "导入${rssSources.size}条" + } + text1.isAbsUrl() -> { + val count = importSourceUrl(text1) + "导入${count}条" + } + else -> "格式不对" + } + }.onError { + finally(it.localizedMessage) + }.onSuccess { + finally(it ?: "导入完成") + } + } + private fun importSourceUrl(url: String): Int { + NetworkUtils.getBaseUrl(url)?.let { + val response = HttpHelper.getApiService(it).get(url, mapOf()).execute() + response.body()?.let { body -> + val sources = mutableListOf() + val items: List> = jsonPath.parse(body).read("$") + for (item in items) { + val jsonItem = jsonPath.parse(item) + GSON.fromJsonObject(jsonItem.jsonString())?.let { source -> + sources.add(source) + } + } + App.db.rssSourceDao().insert(*sources.toTypedArray()) + return sources.size + } + } + return 0 } } \ No newline at end of file