From b05b9a9973759bad2fba0303eacebacdcc0f8e03 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 24 Oct 2021 00:17:22 +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 | 123 ++++++------------ .../app/ui/book/local/ImportBookAdapter.kt | 4 +- .../app/ui/book/local/ImportBookViewModel.kt | 36 +---- .../io/legado/app/utils/DocumentExtensions.kt | 24 ++++ 4 files changed, 68 insertions(+), 119 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 02eb33ede..0d48016db 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 @@ -42,21 +42,18 @@ class ImportBookActivity : VMBaseActivity() private val bookFileRegex = Regex("(?i).*\\.(txt|epub|umd)") - private var rootDoc: DocumentFile? = null - private val subDocs = arrayListOf() + private var rootDoc: FileDoc? = null + private val subDocs = arrayListOf() private val adapter by lazy { ImportBookAdapter(this, this) } - private val sdPath = FileUtils.getSdCardPath() - private var path = sdPath + private val selectFolder = registerForActivityResult(HandleFileContract()) { it.uri?.let { uri -> if (uri.isContentScheme()) { AppConfig.importBookPath = uri.toString() initRootDoc() } else { - uri.path?.let { path -> - AppConfig.importBookPath = path - initRootDoc() - } + AppConfig.importBookPath = uri.path + initRootDoc() } } } @@ -140,45 +137,56 @@ class ImportBookActivity : VMBaseActivity { val rootUri = Uri.parse(lastPath) - rootDoc = DocumentFile.fromTreeUri(this, rootUri) - if (rootDoc == null) { + kotlin.runCatching { + DocumentFile.fromTreeUri(this, rootUri)?.let { + subDocs.clear() + rootDoc = FileDoc.fromDocumentFile(it) + upDocs(rootDoc!!) + } ?: let { + binding.tvEmptyMsg.visible() + selectFolder.launch(null) + } + }.onFailure { binding.tvEmptyMsg.visible() selectFolder.launch(null) - } else { - subDocs.clear() - upPath() } } Build.VERSION.SDK_INT > Build.VERSION_CODES.Q -> { binding.tvEmptyMsg.visible() selectFolder.launch(null) } - else -> { - binding.tvEmptyMsg.visible() - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - rootDoc = null - subDocs.clear() - path = lastPath - upPath() - } - .request() - } + else -> initRootPath(lastPath) } } + private fun initRootPath(path: String) { + binding.tvEmptyMsg.visible() + PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + kotlin.runCatching { + rootDoc = FileDoc.fromFile(File(path)) + subDocs.clear() + upPath() + }.onFailure { + binding.tvEmptyMsg.visible() + selectFolder.launch(null) + } + } + .request() + } + @Synchronized private fun upPath() { rootDoc?.let { upDocs(it) - } ?: upFiles() + } } - private fun upDocs(rootDoc: DocumentFile) { + private fun upDocs(rootDoc: FileDoc) { binding.tvEmptyMsg.gone() - var path = rootDoc.name.toString() + File.separator + var path = rootDoc.name + File.separator var lastDoc = rootDoc for (doc in subDocs) { lastDoc = doc @@ -206,29 +214,6 @@ class ImportBookActivity : VMBaseActivity false - it.isDir -> true - else -> it.name.matches(bookFileRegex) - } - } - docList.sortWith(compareBy({ !it.isDir }, { it.name })) - withContext(Main) { - adapter.setItems(docList) - } - }.onFailure { - toastOnUi("获取文件列表出错\n${it.localizedMessage}") - } - } - } - /** * 扫描当前文件夹 */ @@ -244,22 +229,6 @@ class ImportBookActivity : VMBaseActivity Unit, finally: (() -> Unit)? = null ) { kotlin.runCatching { - DocumentUtils.listFiles(documentFile.uri).forEach { docItem -> + DocumentUtils.listFiles(fileDoc.uri).forEach { docItem -> if (docItem.isDir) { - DocumentFile.fromSingleUri(context, docItem.uri)?.let { - scanDoc(it, false, find) - } + scanDoc(docItem, false, find) } else if (docItem.name.endsWith(".txt", true) || docItem.name.endsWith(".epub", true) ) { @@ -68,32 +66,4 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application) } } - fun scanFile( - file: File, - isRoot: Boolean, - find: (docItem: FileDoc) -> Unit, - finally: (() -> Unit)? = null - ) { - file.listFiles()?.forEach { - if (it.isDirectory) { - scanFile(it, false, find) - } else if (it.name.endsWith(".txt", true) - || it.name.endsWith(".epub", true) - ) { - find( - FileDoc( - it.name, - it.isDirectory, - it.length(), - Date(it.lastModified()), - Uri.parse(it.absolutePath) - ) - ) - } - } - if (isRoot) { - finally?.invoke() - } - } - } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt b/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt index 0208f8f93..10c3a6496 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentExtensions.kt @@ -179,6 +179,30 @@ data class FileDoc( fun readBytes(): ByteArray { return uri.readBytes(appCtx) } + + companion object { + + fun fromDocumentFile(doc: DocumentFile): FileDoc { + return FileDoc( + name = doc.name ?: "", + isDir = doc.isDirectory, + size = doc.length(), + date = Date(doc.lastModified()), + uri = doc.uri + ) + } + + fun fromFile(file: File): FileDoc { + return FileDoc( + name = file.name, + isDir = file.isDirectory, + size = file.length(), + date = Date(file.lastModified()), + uri = Uri.fromFile(file) + ) + } + + } } @Throws(Exception::class)