diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 1754a047f..2a8847f3d 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -15,6 +15,7 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager +import com.google.android.material.snackbar.Snackbar import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -105,7 +106,10 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity intent.data?.let { when (it.path) { "/importonline" -> it.getQueryParameter("src")?.let { url -> - viewModel.importSource(url) + Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE) + viewModel.importSource(url) { msg -> + title_bar.snackbar(msg) + } } else -> { toast("格式不对") @@ -197,7 +201,10 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity cacheUrls.add(0, it) aCache.put("sourceUrl", cacheUrls.joinToString(",")) } - viewModel.importSource(it) + Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE) + viewModel.importSource(it) { msg -> + title_bar.snackbar(msg) + } } } cancelButton() @@ -228,7 +235,10 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity override fun onFilePicked(requestCode: Int, currentPath: String) { if (requestCode == importSource) { - viewModel.importSourceFromFilePath(currentPath) + Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE) + viewModel.importSourceFromFilePath(currentPath) { msg -> + title_bar.snackbar(msg) + } } } @@ -268,14 +278,20 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity when (requestCode) { qrRequestCode -> if (resultCode == RESULT_OK) { data?.getStringExtra("result")?.let { - viewModel.importSource(it) + Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE) + viewModel.importSource(it) { msg -> + title_bar.snackbar(msg) + } } } importSource -> if (resultCode == Activity.RESULT_OK) { data?.data?.let { val path = FileUtils.getPath(this, it) if (path != null) { - viewModel.importSourceFromFilePath(path) + Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE) + viewModel.importSourceFromFilePath(path) { msg -> + title_bar.snackbar(msg) + } } else { toast(R.string.uri_to_path_fail) } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index 6e1c98db2..34b757b81 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -120,28 +120,34 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } } - fun importSourceFromFilePath(path: String) { + fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { execute { val file = File(path) if (file.exists()) { - importSource(file.readText()) + importSource(file.readText(), finally) + } else { + finally("文件无法打开") } } } - fun importSource(text: String) { + 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>("$.sourceUrls") if (!urls.isNullOrEmpty()) { - urls.forEach { importSourceUrl(it) } + var count = 0 + urls.forEach { + count += importSourceUrl(it) + } + finally("导入${count}条") } else { OldRule.jsonToBookSource(text1)?.let { App.db.bookSourceDao().insert(it) } - toast("成功导入1条") + finally("导入1条") } } else if (text1.isJsonArray()) { val bookSources = mutableListOf() @@ -153,9 +159,10 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } } App.db.bookSourceDao().insert(*bookSources.toTypedArray()) - toast("成功导入${bookSources.size}条") + finally("导入${bookSources.size}条") } else if (text1.isAbsUrl()) { - importSourceUrl(text1) + val count = importSourceUrl(text1) + finally("导入${count}条") } else { toast("格式不对") } @@ -164,16 +171,22 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } } - private fun importSourceUrl(url: String) { - execute { - NetworkUtils.getBaseUrl(url)?.let { - val response = HttpHelper.getApiService(it).get(url, mapOf()).execute() - response.body()?.let { body -> - importSource(body) + private fun importSourceUrl(url: String): Int { + NetworkUtils.getBaseUrl(url)?.let { + val response = HttpHelper.getApiService(it).get(url, mapOf()).execute() + response.body()?.let { body -> + val bookSources = mutableListOf() + val items: List> = jsonPath.parse(body).read("$") + for (item in items) { + val jsonItem = jsonPath.parse(item) + OldRule.jsonToBookSource(jsonItem.jsonString())?.let { source -> + bookSources.add(source) + } } + App.db.bookSourceDao().insert(*bookSources.toTypedArray()) + return bookSources.size } - }.onError { - toast(it.localizedMessage) } + return 0 } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d606691e4..a59bd05c7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -559,5 +559,6 @@ 文本 音频 转到后台 + 正在导入