From dc719ac462d53d683f92d9b475e8743f9fe2abcc Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 1 Nov 2020 10:28:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5=E6=9C=AC=E5=9C=B0=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=99=BA=E8=83=BD=E6=89=AB=E6=8F=8F,=E8=8F=9C?= =?UTF-8?q?=E5=8D=95-=E6=99=BA=E8=83=BD=E6=89=AB=E6=8F=8F,=E6=89=AB?= =?UTF-8?q?=E6=8F=8F=E5=BD=93=E5=89=8D=E6=96=87=E4=BB=B6=E5=A4=B9=E5=8C=85?= =?UTF-8?q?=E6=8B=AC=E5=AD=90=E6=96=87=E4=BB=B6=E5=A4=B9=E4=B8=8B=E6=89=80?= =?UTF-8?q?=E6=9C=89=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/local/ImportBookActivity.kt | 40 ++++++++++++------- .../app/ui/book/local/ImportBookViewModel.kt | 24 +++++++++-- .../main/res/layout/activity_import_book.xml | 8 +++- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt index 20d6ea3ed..6adb6e26f 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt @@ -235,25 +235,29 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity * 扫描当前文件夹 */ private fun scanFolder() { - launch(IO) { - rootDoc?.let { doc -> - adapter.clearItems() - val lastDoc = subDocs.lastOrNull() ?: doc - viewModel.scanDoc(lastDoc) { + rootDoc?.let { doc -> + adapter.clearItems() + val lastDoc = subDocs.lastOrNull() ?: doc + refresh_progress_bar.isAutoLoading = true + launch(IO) { + viewModel.scanDoc(lastDoc, true, find) { launch { - adapter.addItem(it) + refresh_progress_bar.isAutoLoading = false } } - } ?: let { - val lastPath = AppConfig.importBookPath - if (lastPath.isNullOrEmpty()) { - toast(R.string.empty_msg_import_book) - } else { - adapter.clearItems() - val file = File(path) - viewModel.scanFile(file) { + } + } ?: let { + val lastPath = AppConfig.importBookPath + if (lastPath.isNullOrEmpty()) { + toast(R.string.empty_msg_import_book) + } else { + adapter.clearItems() + val file = File(path) + refresh_progress_bar.isAutoLoading = true + launch(IO) { + viewModel.scanFile(file, true, find) { launch { - adapter.addItem(it) + refresh_progress_bar.isAutoLoading = false } } } @@ -261,6 +265,12 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity } } + private val find: (docItem: DocItem) -> Unit = { + launch { + adapter.addItem(it) + } + } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt index be88e40d5..4665b15ca 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt @@ -33,12 +33,17 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application) } } - fun scanDoc(documentFile: DocumentFile, find: (docItem: DocItem) -> Unit) { + fun scanDoc( + documentFile: DocumentFile, + isRoot: Boolean, + find: (docItem: DocItem) -> Unit, + finally: (() -> Unit)? = null + ) { val docList = DocumentUtils.listFiles(context, documentFile.uri) docList.forEach { docItem -> if (docItem.isDir) { DocumentFile.fromSingleUri(context, docItem.uri)?.let { - scanDoc(it, find) + scanDoc(it, false, find) } } else if (docItem.name.endsWith(".txt", true) || docItem.name.endsWith(".epub", true) @@ -46,12 +51,20 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application) find(docItem) } } + if (isRoot) { + finally?.invoke() + } } - fun scanFile(file: File, find: (docItem: DocItem) -> Unit) { + fun scanFile( + file: File, + isRoot: Boolean, + find: (docItem: DocItem) -> Unit, + finally: (() -> Unit)? = null + ) { file.listFiles()?.forEach { if (it.isDirectory) { - scanFile(it, find) + scanFile(it, false, find) } else if (it.name.endsWith(".txt", true) || it.name.endsWith(".epub", true) ) { @@ -66,6 +79,9 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application) ) } } + if (isRoot) { + finally?.invoke() + } } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_import_book.xml b/app/src/main/res/layout/activity_import_book.xml index 38f12f595..73635c2f4 100644 --- a/app/src/main/res/layout/activity_import_book.xml +++ b/app/src/main/res/layout/activity_import_book.xml @@ -53,11 +53,17 @@ tools:ignore="UnusedAttribute" /> + +