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 package io.legado.app.help.http
import io.legado.app.data.api.IHttpGetApi
import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.* import okhttp3.*
import retrofit2.Retrofit import retrofit2.Retrofit
@ -35,11 +37,17 @@ object HttpHelper {
builder.build() builder.build()
} }
inline fun <reified T> getApiService(baseUrl: String): T { fun simpleGet(url: String, encode: String? = null): String? {
return getRetrofit(baseUrl).create(T::class.java) 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) return getRetrofit(baseUrl, encode).create(T::class.java)
} }

@ -5,7 +5,6 @@ import android.text.TextUtils
import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.JsonPath
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.api.IHttpGetApi
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Backup
@ -202,20 +201,17 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
} }
private fun importSourceUrl(url: String): Int { private fun importSourceUrl(url: String): Int {
NetworkUtils.getBaseUrl(url)?.let { HttpHelper.simpleGet(url)?.let { body ->
val response = HttpHelper.getApiService<IHttpGetApi>(it).get(url, mapOf()).execute() val bookSources = mutableListOf<BookSource>()
response.body()?.let { body -> val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
val bookSources = mutableListOf<BookSource>() for (item in items) {
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$") val jsonItem = jsonPath.parse(item)
for (item in items) { OldRule.jsonToBookSource(jsonItem.jsonString())?.let { source ->
val jsonItem = jsonPath.parse(item) bookSources.add(source)
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 return 0
} }

@ -6,10 +6,12 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.ReplaceRule 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.Backup
import io.legado.app.help.storage.Restore import io.legado.app.help.storage.Restore
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.splitNotBlank import io.legado.app.utils.splitNotBlank
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
import java.io.File import java.io.File
@ -18,7 +20,13 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
fun importSource(text: String, showMsg: (msg: String) -> Unit) { fun importSource(text: String, showMsg: (msg: String) -> Unit) {
execute { execute {
Restore.importOldReplaceRule(text) if (text.isAbsUrl()) {
HttpHelper.simpleGet(text)?.let {
Restore.importOldReplaceRule(it)
}
} else {
Restore.importOldReplaceRule(text)
}
}.onError { }.onError {
showMsg(it.localizedMessage ?: "ERROR") showMsg(it.localizedMessage ?: "ERROR")
}.onSuccess { }.onSuccess {

Loading…
Cancel
Save