pull/83/head
kunfei 5 years ago
parent e460cf1718
commit 5ccf4b8e2c
  1. 24
      app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt
  2. 18
      app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt

@ -25,7 +25,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
ImportBookAdapter.CallBack { ImportBookAdapter.CallBack {
private val requestCodeSelectFolder = 342 private val requestCodeSelectFolder = 342
private var rootDoc: DocumentFile? = null private var rootDoc: DocumentFile? = null
private val subDirs = arrayListOf<String>() private val subDocs = arrayListOf<DocumentFile>()
private lateinit var importBookAdapter: ImportBookAdapter private lateinit var importBookAdapter: ImportBookAdapter
private var localUriLiveData: LiveData<List<String>>? = null private var localUriLiveData: LiveData<List<String>>? = null
@ -81,7 +81,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
AppConfig.importBookPath?.let { AppConfig.importBookPath?.let {
val rootUri = Uri.parse(it) val rootUri = Uri.parse(it)
rootDoc = DocumentFile.fromTreeUri(this, rootUri) rootDoc = DocumentFile.fromTreeUri(this, rootUri)
subDirs.clear() subDocs.clear()
upPath() upPath()
} }
} }
@ -91,15 +91,13 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
private fun upPath() { private fun upPath() {
rootDoc?.let { rootDoc -> rootDoc?.let { rootDoc ->
var path = rootDoc.name.toString() + File.separator var path = rootDoc.name.toString() + File.separator
var doc: DocumentFile? = rootDoc var lastDoc = rootDoc
for (dirName in subDirs) { for (doc in subDocs) {
doc = doc?.findFile(dirName) lastDoc = doc
doc?.let { path = path + doc.name + File.separator
path = path + it.name + File.separator
}
} }
val docList = arrayListOf<DocumentFile>() val docList = arrayListOf<DocumentFile>()
doc?.listFiles()?.forEach { lastDoc.listFiles().forEach {
if (it.isDirectory && it.name?.startsWith(".") == false) { if (it.isDirectory && it.name?.startsWith(".") == false) {
docList.add(it) docList.add(it)
} else if (it.name?.endsWith(".txt", true) == true) { } else if (it.name?.endsWith(".txt", true) == true) {
@ -140,15 +138,15 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} }
@Synchronized @Synchronized
override fun findFolder(dirName: String) { override fun nextDoc(doc: DocumentFile) {
subDirs.add(dirName) subDocs.add(doc)
upPath() upPath()
} }
@Synchronized @Synchronized
private fun goBackDir(): Boolean { private fun goBackDir(): Boolean {
return if (subDirs.isNotEmpty()) { return if (subDocs.isNotEmpty()) {
subDirs.removeAt(subDirs.lastIndex) subDocs.removeAt(subDocs.lastIndex)
upPath() upPath()
true true
} else { } else {

@ -50,16 +50,14 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
tv_name.text = item.name tv_name.text = item.name
cb_select.isChecked = selectedUris.contains(item.uri.toString()) cb_select.isChecked = selectedUris.contains(item.uri.toString())
onClick { onClick {
item.name?.let { name -> if (item.isDirectory) {
if (item.isDirectory) { callBack.nextDoc(item)
callBack.findFolder(name) } else {
cb_select.isChecked = !cb_select.isChecked
if (cb_select.isChecked) {
selectedUris.add(item.uri.toString())
} else { } else {
cb_select.isChecked = !cb_select.isChecked selectedUris.remove(item.uri.toString())
if (cb_select.isChecked) {
selectedUris.add(item.uri.toString())
} else {
selectedUris.remove(item.uri.toString())
}
} }
} }
} }
@ -67,7 +65,7 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
} }
interface CallBack { interface CallBack {
fun findFolder(dirName: String) fun nextDoc(doc: DocumentFile)
fun upCountView() fun upCountView()
} }

Loading…
Cancel
Save