优化本地导入

pull/457/head
gedoor 4 years ago
parent 3cccff03d4
commit c08215c196
  1. 27
      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,10 +157,14 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} }
} }
@SuppressLint("SetTextI18n")
@Synchronized @Synchronized
private fun upPath() { private fun upPath() {
rootDoc?.let { rootDoc -> rootDoc?.let {
upDocs(it)
} ?: upFiles()
}
private fun upDocs(rootDoc: DocumentFile) {
tv_empty_msg.gone() tv_empty_msg.gone()
var path = rootDoc.name.toString() + File.separator var path = rootDoc.name.toString() + File.separator
var lastDoc = rootDoc var lastDoc = rootDoc
@ -188,8 +193,9 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
adapter.setData(docList) adapter.setData(docList)
} }
} }
} ?: let { }
if (path.isBlank()) return
private fun upFiles() {
tv_empty_msg.gone() tv_empty_msg.gone()
tv_path.text = path.replace(sdPath, "SD") tv_path.text = path.replace(sdPath, "SD")
val docList = arrayListOf<DocItem>() val docList = arrayListOf<DocItem>()
@ -222,13 +228,12 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
docList.sortWith(compareBy({ !it.isDir }, { it.name })) docList.sortWith(compareBy({ !it.isDir }, { it.name }))
adapter.setData(docList) 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