From 61689338fe39e5a1d4359f4aa1c40073ad2d4417 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 31 Oct 2020 23:57:15 +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 | 20 ++++++++- .../app/ui/book/local/ImportBookViewModel.kt | 43 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 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 1bbd5275b..3e0f5beec 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 @@ -29,6 +29,7 @@ import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.jetbrains.anko.sdk27.listeners.onClick +import org.jetbrains.anko.toast import java.io.File import java.util.* @@ -234,8 +235,23 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity * 扫描当前文件夹 */ private fun scanFolder() { - rootDoc?.let { - + 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 { + adapter.clearItems() + val file = File(path) + viewModel.scanFile(file) { + 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 d3be59c5c..a8a603eb8 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 @@ -5,6 +5,10 @@ import android.net.Uri import androidx.documentfile.provider.DocumentFile import io.legado.app.base.BaseViewModel import io.legado.app.model.localBook.LocalBook +import io.legado.app.utils.DocItem +import io.legado.app.utils.DocumentUtils +import java.io.File +import java.util.* class ImportBookViewModel(application: Application) : BaseViewModel(application) { @@ -29,4 +33,43 @@ 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) + } + } + } + } + + 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) + ) + ) + } + } + } + } + } \ No newline at end of file