pull/48/head
kunfei 5 years ago
parent 3640b3a4b2
commit 5793f63a0b
  1. 63
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  2. 70
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.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<List<String>>("$.sourceUrls")
if (!urls.isNullOrEmpty()) {
when {
text1.isJsonObject() -> {
val json = JsonPath.parse(text1)
val urls = json.read<List<String>>("$.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<BookSource>()
val items: List<Map<String, Any>> = 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<BookSource>()
val items: List<Map<String, Any>> = 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 ?: "导入完成")
}
}

@ -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<List<String>>("$.sourceUrls")
var count = 0
if (!urls.isNullOrEmpty()) {
urls.forEach {
count += importSourceUrl(it)
}
} else {
GSON.fromJsonArray<RssSource>(text1)?.let {
App.db.rssSourceDao().insert(*it.toTypedArray())
count = 1
}
}
"导入${count}"
}
text1.isJsonArray() -> {
val rssSources = mutableListOf<RssSource>()
val items: List<Map<String, Any>> = jsonPath.parse(text1).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
GSON.fromJsonObject<RssSource>(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<IHttpGetApi>(it).get(url, mapOf()).execute()
response.body()?.let { body ->
val sources = mutableListOf<RssSource>()
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
GSON.fromJsonObject<RssSource>(jsonItem.jsonString())?.let { source ->
sources.add(source)
}
}
App.db.rssSourceDao().insert(*sources.toTypedArray())
return sources.size
}
}
return 0
}
}
Loading…
Cancel
Save