|
|
@ -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,30 +128,34 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) |
|
|
|
if (file.exists()) { |
|
|
|
if (file.exists()) { |
|
|
|
importSource(file.readText(), finally) |
|
|
|
importSource(file.readText(), finally) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
withContext(Dispatchers.Main) { |
|
|
|
finally("文件无法打开") |
|
|
|
finally("文件无法打开") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 { |
|
|
|
|
|
|
|
text1.isJsonObject() -> { |
|
|
|
val json = JsonPath.parse(text1) |
|
|
|
val json = JsonPath.parse(text1) |
|
|
|
val urls = json.read<List<String>>("$.sourceUrls") |
|
|
|
val urls = json.read<List<String>>("$.sourceUrls") |
|
|
|
if (!urls.isNullOrEmpty()) { |
|
|
|
|
|
|
|
var count = 0 |
|
|
|
var count = 0 |
|
|
|
|
|
|
|
if (!urls.isNullOrEmpty()) { |
|
|
|
urls.forEach { |
|
|
|
urls.forEach { |
|
|
|
count += importSourceUrl(it) |
|
|
|
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) { |
|
|
@ -159,15 +165,18 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
App.db.bookSourceDao().insert(*bookSources.toTypedArray()) |
|
|
|
App.db.bookSourceDao().insert(*bookSources.toTypedArray()) |
|
|
|
finally("导入${bookSources.size}条") |
|
|
|
"导入${bookSources.size}条" |
|
|
|
} else if (text1.isAbsUrl()) { |
|
|
|
} |
|
|
|
|
|
|
|
text1.isAbsUrl() -> { |
|
|
|
val count = importSourceUrl(text1) |
|
|
|
val count = importSourceUrl(text1) |
|
|
|
finally("导入${count}条") |
|
|
|
"导入${count}条" |
|
|
|
} else { |
|
|
|
} |
|
|
|
finally("格式不对") |
|
|
|
else -> "格式不对" |
|
|
|
} |
|
|
|
} |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
finally(it.localizedMessage) |
|
|
|
finally(it.localizedMessage) |
|
|
|
|
|
|
|
}.onSuccess { |
|
|
|
|
|
|
|
finally(it ?: "导入完成") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|