feat: 优化代码

pull/172/head
kunfei 5 years ago
parent 2ae8cea905
commit 01ce97581f
  1. 24
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt
  2. 16
      app/src/main/res/layout/activity_import_book.xml

@ -17,7 +17,8 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.lib.theme.accentColor import io.legado.app.ui.filechooser.FileChooserDialog
import io.legado.app.ui.filechooser.FilePicker
import io.legado.app.ui.widget.SelectActionBar import io.legado.app.ui.widget.SelectActionBar
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_import_book.* import kotlinx.android.synthetic.main.activity_import_book.*
@ -31,6 +32,7 @@ import java.util.*
class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity_import_book), class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity_import_book),
FileChooserDialog.CallBack,
PopupMenu.OnMenuItemClickListener, PopupMenu.OnMenuItemClickListener,
ImportBookAdapter.CallBack { ImportBookAdapter.CallBack {
private val requestCodeSelectFolder = 342 private val requestCodeSelectFolder = 342
@ -59,7 +61,6 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.layoutManager = LinearLayoutManager(this)
adapter = ImportBookAdapter(this, this) adapter = ImportBookAdapter(this, this)
recycler_view.adapter = adapter recycler_view.adapter = adapter
rotate_loading.loadingColor = accentColor
select_action_bar.setMainActionText(R.string.add_to_shelf) select_action_bar.setMainActionText(R.string.add_to_shelf)
select_action_bar.inflateMenu(R.menu.import_book_sel) select_action_bar.inflateMenu(R.menu.import_book_sel)
select_action_bar.setOnMenuItemClickListener(this) select_action_bar.setOnMenuItemClickListener(this)
@ -88,7 +89,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.menu_select_folder -> selectImportFolder() R.id.menu_select_folder -> FilePicker.selectFolder(this, requestCodeSelectFolder)
} }
return super.onCompatOptionsItemSelected(item) return super.onCompatOptionsItemSelected(item)
} }
@ -118,6 +119,8 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
rootDoc = DocumentFile.fromTreeUri(this, rootUri) rootDoc = DocumentFile.fromTreeUri(this, rootUri)
subDocs.clear() subDocs.clear()
} else { } else {
rootDoc = null
subDocs.clear()
path = it path = it
} }
} }
@ -137,7 +140,6 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
tv_path.text = path tv_path.text = path
adapter.selectedUris.clear() adapter.selectedUris.clear()
adapter.clearItems() adapter.clearItems()
rotate_loading.show()
launch(IO) { launch(IO) {
val docList = DocumentUtils.listFiles( val docList = DocumentUtils.listFiles(
this@ImportBookActivity, this@ImportBookActivity,
@ -153,7 +155,6 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} }
docList.sortWith(compareBy({ !it.isDir }, { it.name })) docList.sortWith(compareBy({ !it.isDir }, { it.name }))
withContext(Main) { withContext(Main) {
rotate_loading.hide()
adapter.setData(docList) adapter.setData(docList)
} }
} }
@ -186,13 +187,12 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} }
} }
private fun selectImportFolder() { override fun onFilePicked(requestCode: Int, currentPath: String) {
try { when (requestCode) {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) requestCodeSelectFolder -> {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) AppConfig.importBookPath = currentPath
startActivityForResult(intent, requestCodeSelectFolder) upRootDoc()
} catch (e: Exception) { }
e.printStackTrace()
} }
} }

@ -50,23 +50,11 @@
tools:ignore="UnusedAttribute" /> tools:ignore="UnusedAttribute" />
</LinearLayout> </LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view" android:id="@+id/recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="0dp"
android:layout_weight="1" />
<io.legado.app.ui.widget.anima.RotateLoading
android:id="@+id/rotate_loading"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center" />
</FrameLayout>
<io.legado.app.ui.widget.SelectActionBar <io.legado.app.ui.widget.SelectActionBar
android:id="@+id/select_action_bar" android:id="@+id/select_action_bar"

Loading…
Cancel
Save