From b623b4d3b6a65c2b2971f3a7d81c8df4525436fc Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 1 Nov 2020 10:05:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/local/ImportBookActivity.kt | 34 +++++++------ .../app/ui/book/local/ImportBookViewModel.kt | 50 +++++++++---------- 2 files changed, 43 insertions(+), 41 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 3e0f5beec..20d6ea3ed 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,21 +235,27 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity * 扫描当前文件夹 */ private fun scanFolder() { - rootDoc?.let { doc -> - adapter.clearItems() - val lastDoc = subDocs.lastOrNull() ?: doc - viewModel.scanDoc(lastDoc) { - adapter.addItem(it) - } - } ?: let { - val lastPath = AppConfig.importBookPath - if (lastPath.isNullOrEmpty()) { - toast(R.string.empty_msg_import_book) - } else { + launch(IO) { + rootDoc?.let { doc -> adapter.clearItems() - val file = File(path) - viewModel.scanFile(file) { - adapter.addItem(it) + val lastDoc = subDocs.lastOrNull() ?: doc + viewModel.scanDoc(lastDoc) { + launch { + adapter.addItem(it) + } + } + } ?: 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) { + launch { + adapter.addItem(it) + } + } } } } 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 a8a603eb8..be88e40d5 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 @@ -34,40 +34,36 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application) } fun scanDoc(documentFile: DocumentFile, find: (docItem: DocItem) -> Unit) { - execute { - val docList = DocumentUtils.listFiles(context, documentFile.uri) - docList.forEach { docItem -> - if (docItem.isDir) { - DocumentFile.fromSingleUri(context, docItem.uri)?.let { - scanDoc(it, find) - } - } else if (docItem.name.endsWith(".txt", true) - || docItem.name.endsWith(".epub", true) - ) { - find(docItem) + val docList = DocumentUtils.listFiles(context, documentFile.uri) + docList.forEach { docItem -> + if (docItem.isDir) { + DocumentFile.fromSingleUri(context, docItem.uri)?.let { + scanDoc(it, find) } + } else if (docItem.name.endsWith(".txt", true) + || docItem.name.endsWith(".epub", true) + ) { + find(docItem) } } } fun scanFile(file: File, find: (docItem: DocItem) -> Unit) { - execute { - file.listFiles()?.forEach { - if (it.isDirectory) { - scanFile(it, find) - } else if (it.name.endsWith(".txt", true) - || it.name.endsWith(".epub", true) - ) { - find( - DocItem( - it.name, - it.extension, - it.length(), - Date(it.lastModified()), - Uri.parse(it.absolutePath) - ) + file.listFiles()?.forEach { + if (it.isDirectory) { + scanFile(it, find) + } else if (it.name.endsWith(".txt", true) + || it.name.endsWith(".epub", true) + ) { + find( + DocItem( + it.name, + it.extension, + it.length(), + Date(it.lastModified()), + Uri.parse(it.absolutePath) ) - } + ) } } }