Explorar el Código

本地导入添加排序

kunfei hace 4 años
padre
commit
a3c50fffb8

+ 41 - 1
app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt

@@ -70,11 +70,30 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
         return super.onCompatCreateOptionsMenu(menu)
     }
 
+    override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
+        menu.findItem(R.id.menu_sort_name)?.isChecked = viewModel.sort == 0
+        menu.findItem(R.id.menu_sort_size)?.isChecked = viewModel.sort == 1
+        menu.findItem(R.id.menu_sort_time)?.isChecked = viewModel.sort == 2
+        return super.onMenuOpened(featureId, menu)
+    }
+
     override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
         when (item.itemId) {
             R.id.menu_select_folder -> selectFolder.launch()
             R.id.menu_scan_folder -> scanFolder()
             R.id.menu_import_file_name -> alertImportFileName()
+            R.id.menu_sort_name -> {
+                viewModel.sort = 0
+                upPath()
+            }
+            R.id.menu_sort_size -> {
+                viewModel.sort = 1
+                upPath()
+            }
+            R.id.menu_sort_time -> {
+                viewModel.sort = 2
+                upPath()
+            }
         }
         return super.onCompatOptionsItemSelected(item)
     }
@@ -205,7 +224,28 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
                         else -> item.name.matches(bookFileRegex)
                     }
                 }
-                docList.sortWith(compareBy({ !it.isDir }, { it.name }))
+                when (viewModel.sort) {
+                    2 -> docList.sortWith(
+                        compareBy(
+                            { !it.isDir },
+                            { it.date },
+                            { it.name }
+                        )
+                    )
+                    1 -> docList.sortWith(
+                        compareBy(
+                            { !it.isDir },
+                            { it.size },
+                            { it.name }
+                        )
+                    )
+                    else -> docList.sortWith(
+                        compareBy(
+                            { !it.isDir },
+                            { it.name }
+                        )
+                    )
+                }
                 withContext(Main) {
                     adapter.setItems(docList)
                 }

+ 2 - 0
app/src/main/java/io/legado/app/ui/book/local/ImportBookViewModel.kt

@@ -14,6 +14,8 @@ import java.io.File
 
 class ImportBookViewModel(application: Application) : BaseViewModel(application) {
 
+    var sort = 0
+
     fun addToBookshelf(uriList: HashSet<String>, finally: () -> Unit) {
         execute {
             uriList.forEach {