From ee66194c155f6741bbbad616e5d1ac2eb24ab65c Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 30 Jan 2020 21:58:42 +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/importbook/ImportBookActivity.kt | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt index cf8843600..f56429d5f 100644 --- a/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt @@ -14,10 +14,6 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.help.AppConfig import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_import_book.* -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.Dispatchers.Main -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import org.jetbrains.anko.sdk27.listeners.onClick import java.io.File @@ -58,10 +54,7 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity private fun initEvent() { tv_go_back.onClick { - if (subDirs.isNotEmpty()) { - subDirs.removeAt(subDirs.lastIndex) - upPath() - } + goBackDir() } btn_add_book.onClick { @@ -81,29 +74,26 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity } @SuppressLint("SetTextI18n") + @Synchronized private fun upPath() { rootDoc?.let { rootDoc -> - launch(IO) { - var path = rootDoc.name.toString() + File.separator - var doc: DocumentFile? = rootDoc - for (dirName in subDirs) { - doc = doc?.findFile(dirName) - doc?.let { - path = path + it.name + File.separator - } - } - val docList = arrayListOf() - doc?.listFiles()?.forEach { - if (it.isDirectory || it.name?.endsWith(".txt", true) == true) { - docList.add(it) - } + var path = rootDoc.name.toString() + File.separator + var doc: DocumentFile? = rootDoc + for (dirName in subDirs) { + doc = doc?.findFile(dirName) + doc?.let { + path = path + it.name + File.separator } - docList.sortWith(compareBy({ !it.isDirectory }, { it.name })) - withContext(Main) { - tv_path.text = path - importBookAdapter.setItems(docList) + } + val docList = arrayListOf() + doc?.listFiles()?.forEach { + if (it.isDirectory || it.name?.endsWith(".txt", true) == true) { + docList.add(it) } } + docList.sortWith(compareBy({ !it.isDirectory }, { it.name })) + tv_path.text = path + importBookAdapter.setItems(docList) } } @@ -133,16 +123,25 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity } } + @Synchronized override fun findFolder(dirName: String) { subDirs.add(dirName) upPath() } - override fun onBackPressed() { - if (subDirs.isNotEmpty()) { + @Synchronized + private fun goBackDir(): Boolean { + return if (subDirs.isNotEmpty()) { subDirs.removeAt(subDirs.lastIndex) upPath() + true } else { + false + } + } + + override fun onBackPressed() { + if (!goBackDir()) { super.onBackPressed() } }