优化本地导入

pull/457/head
gedoor 4 years ago
parent 3cccff03d4
commit c08215c196
  1. 121
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt

@ -1,6 +1,5 @@
package io.legado.app.ui.book.local package io.legado.app.ui.book.local
import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
@ -33,7 +32,9 @@ import org.jetbrains.anko.sdk27.listeners.onClick
import java.io.File import java.io.File
import java.util.* import java.util.*
/**
* 导入本地书籍界面
*/
class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity_import_book), class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity_import_book),
FilePickerDialog.CallBack, FilePickerDialog.CallBack,
PopupMenu.OnMenuItemClickListener, PopupMenu.OnMenuItemClickListener,
@ -53,7 +54,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
initView() initView()
initEvent() initEvent()
initData() initData()
upRootDoc() initRootDoc()
} }
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
@ -118,7 +119,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
}) })
} }
private fun upRootDoc() { private fun initRootDoc() {
val lastPath = AppConfig.importBookPath val lastPath = AppConfig.importBookPath
when { when {
lastPath.isNullOrEmpty() -> { lastPath.isNullOrEmpty() -> {
@ -156,79 +157,83 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} }
} }
@SuppressLint("SetTextI18n")
@Synchronized @Synchronized
private fun upPath() { private fun upPath() {
rootDoc?.let { rootDoc -> rootDoc?.let {
tv_empty_msg.gone() upDocs(it)
var path = rootDoc.name.toString() + File.separator } ?: upFiles()
var lastDoc = rootDoc }
for (doc in subDocs) {
lastDoc = doc private fun upDocs(rootDoc: DocumentFile) {
path = path + doc.name + File.separator tv_empty_msg.gone()
} var path = rootDoc.name.toString() + File.separator
tv_path.text = path var lastDoc = rootDoc
adapter.selectedUris.clear() for (doc in subDocs) {
adapter.clearItems() lastDoc = doc
launch(IO) { path = path + doc.name + File.separator
val docList = DocumentUtils.listFiles(this@ImportBookActivity, lastDoc.uri) }
for (i in docList.lastIndex downTo 0) { tv_path.text = path
val item = docList[i] adapter.selectedUris.clear()
if (item.name.startsWith(".")) { adapter.clearItems()
docList.removeAt(i) launch(IO) {
} else if (!item.isDir val docList = DocumentUtils.listFiles(this@ImportBookActivity, lastDoc.uri)
&& !item.name.endsWith(".txt", true) for (i in docList.lastIndex downTo 0) {
&& !item.name.endsWith(".epub", true) val item = docList[i]
) { if (item.name.startsWith(".")) {
docList.removeAt(i) docList.removeAt(i)
} } else if (!item.isDir
} && !item.name.endsWith(".txt", true)
docList.sortWith(compareBy({ !it.isDir }, { it.name })) && !item.name.endsWith(".epub", true)
withContext(Main) { ) {
adapter.setData(docList) docList.removeAt(i)
} }
} }
} ?: let { docList.sortWith(compareBy({ !it.isDir }, { it.name }))
if (path.isBlank()) return withContext(Main) {
tv_empty_msg.gone() adapter.setData(docList)
tv_path.text = path.replace(sdPath, "SD") }
val docList = arrayListOf<DocItem>() }
File(path).listFiles()?.forEach { }
if (it.isDirectory) {
if (!it.name.startsWith(".")) private fun upFiles() {
docList.add( tv_empty_msg.gone()
DocItem( tv_path.text = path.replace(sdPath, "SD")
it.name, val docList = arrayListOf<DocItem>()
DocumentsContract.Document.MIME_TYPE_DIR, File(path).listFiles()?.forEach {
it.length(), if (it.isDirectory) {
Date(it.lastModified()), if (!it.name.startsWith("."))
Uri.parse(it.absolutePath)
)
)
} else if (it.name.endsWith(".txt", true)
|| it.name.endsWith(".epub", true)
) {
docList.add( docList.add(
DocItem( DocItem(
it.name, it.name,
it.extension, DocumentsContract.Document.MIME_TYPE_DIR,
it.length(), it.length(),
Date(it.lastModified()), Date(it.lastModified()),
Uri.parse(it.absolutePath) Uri.parse(it.absolutePath)
) )
) )
} } else if (it.name.endsWith(".txt", true)
|| it.name.endsWith(".epub", true)
) {
docList.add(
DocItem(
it.name,
it.extension,
it.length(),
Date(it.lastModified()),
Uri.parse(it.absolutePath)
)
)
} }
docList.sortWith(compareBy({ !it.isDir }, { it.name }))
adapter.setData(docList)
} }
docList.sortWith(compareBy({ !it.isDir }, { it.name }))
adapter.setData(docList)
} }
override fun onFilePicked(requestCode: Int, currentPath: String) { override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) { when (requestCode) {
requestCodeSelectFolder -> { requestCodeSelectFolder -> {
AppConfig.importBookPath = currentPath AppConfig.importBookPath = currentPath
upRootDoc() initRootDoc()
} }
} }
} }
@ -243,7 +248,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
AppConfig.importBookPath = uri.toString() AppConfig.importBookPath = uri.toString()
upRootDoc() initRootDoc()
} }
} }
} }

Loading…
Cancel
Save