优化
This commit is contained in:
@@ -127,6 +127,9 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
}
|
}
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
GSON.fromJsonObject<BookSource>(mText).getOrThrow().let {
|
GSON.fromJsonObject<BookSource>(mText).getOrThrow().let {
|
||||||
|
if (it.bookSourceUrl.isEmpty()) {
|
||||||
|
throw NoStackTraceException("不是书源")
|
||||||
|
}
|
||||||
allSources.add(it)
|
allSources.add(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,6 +137,10 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
|
|
||||||
mText.isJsonArray() -> GSON.fromJsonArray<BookSource>(mText).getOrThrow()
|
mText.isJsonArray() -> GSON.fromJsonArray<BookSource>(mText).getOrThrow()
|
||||||
.let { items ->
|
.let { items ->
|
||||||
|
val source = items.firstOrNull() ?: return@let
|
||||||
|
if (source.bookSourceUrl.isEmpty()) {
|
||||||
|
throw NoStackTraceException("不是书源")
|
||||||
|
}
|
||||||
allSources.addAll(items)
|
allSources.addAll(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,8 +150,14 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
|
|
||||||
mText.isUri() -> {
|
mText.isUri() -> {
|
||||||
val uri = Uri.parse(mText)
|
val uri = Uri.parse(mText)
|
||||||
uri.inputStream(context).getOrThrow().use {
|
uri.inputStream(context).getOrThrow().use { inputS ->
|
||||||
allSources.addAll(GSON.fromJsonArray<BookSource>(it).getOrThrow())
|
GSON.fromJsonArray<BookSource>(inputS).getOrThrow().let {
|
||||||
|
val source = it.firstOrNull() ?: return@let
|
||||||
|
if (source.bookSourceUrl.isEmpty()) {
|
||||||
|
throw NoStackTraceException("不是书源")
|
||||||
|
}
|
||||||
|
allSources.addAll(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,8 +179,14 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
} else {
|
} else {
|
||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.byteStream().use {
|
}.byteStream().use { body ->
|
||||||
allSources.addAll(GSON.fromJsonArray<BookSource>(it).getOrThrow())
|
GSON.fromJsonArray<BookSource>(body).getOrThrow().let {
|
||||||
|
val source = it.firstOrNull() ?: return@let
|
||||||
|
if (source.bookSourceUrl.isEmpty()) {
|
||||||
|
throw NoStackTraceException("不是书源")
|
||||||
|
}
|
||||||
|
allSources.addAll(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,27 +91,38 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
execute {
|
execute {
|
||||||
val mText = text.trim()
|
val mText = text.trim()
|
||||||
when {
|
when {
|
||||||
mText.isJsonObject() -> {
|
mText.isJsonObject() -> kotlin.runCatching {
|
||||||
val json = JsonPath.parse(mText)
|
val json = JsonPath.parse(mText)
|
||||||
val urls = json.read<List<String>>("$.sourceUrls")
|
val urls = json.read<List<String>>("$.sourceUrls")
|
||||||
if (!urls.isNullOrEmpty()) {
|
if (!urls.isNullOrEmpty()) {
|
||||||
urls.forEach {
|
urls.forEach {
|
||||||
importSourceUrl(it)
|
importSourceUrl(it)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
|
||||||
allSources.addAll(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}.onFailure {
|
||||||
mText.isJsonArray() -> {
|
|
||||||
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||||
|
val source = it.firstOrNull() ?: return@let
|
||||||
|
if (source.sourceUrl.isEmpty()) {
|
||||||
|
throw NoStackTraceException("不是订阅源")
|
||||||
|
}
|
||||||
allSources.addAll(it)
|
allSources.addAll(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mText.isJsonArray() -> {
|
||||||
|
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||||
|
val source = it.firstOrNull() ?: return@let
|
||||||
|
if (source.sourceUrl.isEmpty()) {
|
||||||
|
throw NoStackTraceException("不是订阅源")
|
||||||
|
}
|
||||||
|
allSources.addAll(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mText.isAbsUrl() -> {
|
mText.isAbsUrl() -> {
|
||||||
importSourceUrl(mText)
|
importSourceUrl(mText)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
|
else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
|
||||||
}
|
}
|
||||||
}.onError {
|
}.onError {
|
||||||
@@ -133,6 +144,9 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
}.byteStream().let { body ->
|
}.byteStream().let { body ->
|
||||||
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
||||||
for (item in items) {
|
for (item in items) {
|
||||||
|
if (!item.containsKey("sourceUrl")) {
|
||||||
|
throw NoStackTraceException("不是订阅源")
|
||||||
|
}
|
||||||
val jsonItem = jsonPath.parse(item)
|
val jsonItem = jsonPath.parse(item)
|
||||||
GSON.fromJsonObject<RssSource>(jsonItem.jsonString()).getOrThrow().let { source ->
|
GSON.fromJsonObject<RssSource>(jsonItem.jsonString()).getOrThrow().let { source ->
|
||||||
allSources.add(source)
|
allSources.add(source)
|
||||||
|
|||||||
Reference in New Issue
Block a user