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" /> + +