|
|
@ -11,6 +11,8 @@ import io.legado.app.help.http.HttpHelper |
|
|
|
import io.legado.app.help.storage.OldRule |
|
|
|
import io.legado.app.help.storage.OldRule |
|
|
|
import io.legado.app.help.storage.Restore.jsonPath |
|
|
|
import io.legado.app.help.storage.Restore.jsonPath |
|
|
|
import io.legado.app.utils.* |
|
|
|
import io.legado.app.utils.* |
|
|
|
|
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
|
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
class BookSourceViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
class BookSourceViewModel(application: Application) : BaseViewModel(application) { |
|
|
@ -126,7 +128,9 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) |
|
|
|
if (file.exists()) { |
|
|
|
if (file.exists()) { |
|
|
|
importSource(file.readText(), finally) |
|
|
|
importSource(file.readText(), finally) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
finally("文件无法打开") |
|
|
|
withContext(Dispatchers.Main) { |
|
|
|
|
|
|
|
finally("文件无法打开") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -134,40 +138,45 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) |
|
|
|
fun importSource(text: String, finally: (msg: String) -> Unit) { |
|
|
|
fun importSource(text: String, finally: (msg: String) -> Unit) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
val text1 = text.trim() |
|
|
|
val text1 = text.trim() |
|
|
|
if (text1.isJsonObject()) { |
|
|
|
when { |
|
|
|
val json = JsonPath.parse(text1) |
|
|
|
text1.isJsonObject() -> { |
|
|
|
val urls = json.read<List<String>>("$.sourceUrls") |
|
|
|
val json = JsonPath.parse(text1) |
|
|
|
if (!urls.isNullOrEmpty()) { |
|
|
|
val urls = json.read<List<String>>("$.sourceUrls") |
|
|
|
var count = 0 |
|
|
|
var count = 0 |
|
|
|
urls.forEach { |
|
|
|
if (!urls.isNullOrEmpty()) { |
|
|
|
count += importSourceUrl(it) |
|
|
|
urls.forEach { |
|
|
|
} |
|
|
|
count += importSourceUrl(it) |
|
|
|
finally("导入${count}条") |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
OldRule.jsonToBookSource(text1)?.let { |
|
|
|
OldRule.jsonToBookSource(text1)?.let { |
|
|
|
App.db.bookSourceDao().insert(it) |
|
|
|
App.db.bookSourceDao().insert(it) |
|
|
|
|
|
|
|
count = 1 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
finally("导入1条") |
|
|
|
"导入${count}条" |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (text1.isJsonArray()) { |
|
|
|
text1.isJsonArray() -> { |
|
|
|
val bookSources = mutableListOf<BookSource>() |
|
|
|
val bookSources = mutableListOf<BookSource>() |
|
|
|
val items: List<Map<String, Any>> = jsonPath.parse(text1).read("$") |
|
|
|
val items: List<Map<String, Any>> = jsonPath.parse(text1).read("$") |
|
|
|
for (item in items) { |
|
|
|
for (item in items) { |
|
|
|
val jsonItem = jsonPath.parse(item) |
|
|
|
val jsonItem = jsonPath.parse(item) |
|
|
|
OldRule.jsonToBookSource(jsonItem.jsonString())?.let { |
|
|
|
OldRule.jsonToBookSource(jsonItem.jsonString())?.let { |
|
|
|
bookSources.add(it) |
|
|
|
bookSources.add(it) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
App.db.bookSourceDao().insert(*bookSources.toTypedArray()) |
|
|
|
|
|
|
|
"导入${bookSources.size}条" |
|
|
|
} |
|
|
|
} |
|
|
|
App.db.bookSourceDao().insert(*bookSources.toTypedArray()) |
|
|
|
text1.isAbsUrl() -> { |
|
|
|
finally("导入${bookSources.size}条") |
|
|
|
val count = importSourceUrl(text1) |
|
|
|
} else if (text1.isAbsUrl()) { |
|
|
|
"导入${count}条" |
|
|
|
val count = importSourceUrl(text1) |
|
|
|
} |
|
|
|
finally("导入${count}条") |
|
|
|
else -> "格式不对" |
|
|
|
} else { |
|
|
|
|
|
|
|
finally("格式不对") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
finally(it.localizedMessage) |
|
|
|
finally(it.localizedMessage) |
|
|
|
|
|
|
|
}.onSuccess { |
|
|
|
|
|
|
|
finally(it ?: "导入完成") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|