From 616611db6afa6474829c3391772120214e1fe051 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 30 Jan 2020 13:57:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/http/HttpHelper.kt | 14 +++++++++--- .../book/source/manage/BookSourceViewModel.kt | 22 ++++++++----------- .../ui/replacerule/ReplaceRuleViewModel.kt | 10 ++++++++- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index da70b17a7..3e6d6c3ab 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -1,5 +1,7 @@ package io.legado.app.help.http +import io.legado.app.data.api.IHttpGetApi +import io.legado.app.utils.NetworkUtils import kotlinx.coroutines.suspendCancellableCoroutine import okhttp3.* import retrofit2.Retrofit @@ -35,11 +37,17 @@ object HttpHelper { builder.build() } - inline fun getApiService(baseUrl: String): T { - return getRetrofit(baseUrl).create(T::class.java) + fun simpleGet(url: String, encode: String? = null): String? { + NetworkUtils.getBaseUrl(url)?.let { baseUrl -> + val response = getApiService(baseUrl, encode) + .get(url, mapOf()) + .execute() + return response.body() + } + return null } - inline fun getApiService(baseUrl: String, encode: String): T { + inline fun getApiService(baseUrl: String, encode: String? = null): T { return getRetrofit(baseUrl, encode).create(T::class.java) } 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 06edf4033..554ae4165 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 @@ -5,7 +5,6 @@ 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.BookSource import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.Backup @@ -202,20 +201,17 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } private fun importSourceUrl(url: String): Int { - NetworkUtils.getBaseUrl(url)?.let { - val response = HttpHelper.getApiService(it).get(url, mapOf()).execute() - response.body()?.let { body -> - val bookSources = mutableListOf() - val items: List> = jsonPath.parse(body).read("$") - for (item in items) { - val jsonItem = jsonPath.parse(item) - OldRule.jsonToBookSource(jsonItem.jsonString())?.let { source -> - bookSources.add(source) - } + HttpHelper.simpleGet(url)?.let { body -> + val bookSources = mutableListOf() + val items: List> = jsonPath.parse(body).read("$") + for (item in items) { + val jsonItem = jsonPath.parse(item) + OldRule.jsonToBookSource(jsonItem.jsonString())?.let { source -> + bookSources.add(source) } - App.db.bookSourceDao().insert(*bookSources.toTypedArray()) - return bookSources.size } + App.db.bookSourceDao().insert(*bookSources.toTypedArray()) + return bookSources.size } return 0 } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index 00a279a33..f8c9dda24 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -6,10 +6,12 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.ReplaceRule +import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Restore import io.legado.app.utils.FileUtils import io.legado.app.utils.GSON +import io.legado.app.utils.isAbsUrl import io.legado.app.utils.splitNotBlank import org.jetbrains.anko.toast import java.io.File @@ -18,7 +20,13 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application fun importSource(text: String, showMsg: (msg: String) -> Unit) { execute { - Restore.importOldReplaceRule(text) + if (text.isAbsUrl()) { + HttpHelper.simpleGet(text)?.let { + Restore.importOldReplaceRule(it) + } + } else { + Restore.importOldReplaceRule(text) + } }.onError { showMsg(it.localizedMessage ?: "ERROR") }.onSuccess {