pull/83/head
kunfei 5 years ago
parent 5b7c6bcaab
commit 616611db6a
  1. 14
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  2. 22
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  3. 10
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.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 <reified T> 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<IHttpGetApi>(baseUrl, encode)
.get(url, mapOf())
.execute()
return response.body()
}
return null
}
inline fun <reified T> getApiService(baseUrl: String, encode: String): T {
inline fun <reified T> getApiService(baseUrl: String, encode: String? = null): T {
return getRetrofit(baseUrl, encode).create(T::class.java)
}

@ -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<IHttpGetApi>(it).get(url, mapOf()).execute()
response.body()?.let { body ->
val bookSources = mutableListOf<BookSource>()
val items: List<Map<String, Any>> = 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<BookSource>()
val items: List<Map<String, Any>> = 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
}

@ -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 {

Loading…
Cancel
Save