From 6dbfcf24fe56cf468a8272a5d13e531e8671e567 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Jul 2022 21:28:34 +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/book/remote/RemoteBookActivity.kt | 76 +++---------------- .../app/ui/book/remote/RemoteBookViewModel.kt | 14 +++- .../java/io/legado/app/ui/book/remote/Sort.kt | 5 ++ app/src/main/res/menu/book_remote.xml | 7 -- 4 files changed, 28 insertions(+), 74 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/remote/Sort.kt diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt index 0dd6ac2ee..2f7b9296b 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt @@ -13,19 +13,14 @@ import io.legado.app.databinding.ActivityImportBookBinding import io.legado.app.lib.theme.backgroundColor import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.book.remote.manager.RemoteBookWebDav -import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.widget.SelectActionBar import io.legado.app.ui.widget.dialog.WaitDialog -import io.legado.app.utils.cnCompare import io.legado.app.utils.showDialogFragment -import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.delay import kotlinx.coroutines.flow.conflate -import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import java.io.File -import java.util.ArrayList /** * 展示远程书籍 @@ -40,8 +35,6 @@ class RemoteBookActivity : VMBaseActivity - - val dirList = ArrayList() - val bookList = ArrayList() - - remoteBooks.forEach { - if (it.isDir) - dirList.add(it) - else - bookList.add(it) - } - //默认情况下,为按修改时间倒序显示 - if (sortAscending) when (sortKey) { - Sort.Name -> { - dirList.sortedBy { it.filename } + - bookList.sortedBy { it.filename } - } - Sort.UpdateTime -> { - dirList.sortedBy { it.lastModify } + - bookList.sortedBy { it.lastModify } - } - else -> dirList + bookList - } - else when (sortKey) { - - Sort.Name -> { - dirList.sortedByDescending { it.filename } + - bookList.sortedByDescending { it.filename } - } - Sort.UpdateTime -> { - dirList.sortedByDescending { it.lastModify } + - bookList.sortedByDescending { it.lastModify } - } - //按修改时间倒序显示 - else -> { - dirList.sortedByDescending { it.lastModify } + - bookList.sortedByDescending { it.lastModify } - } - } - - }.conflate().collect { sortedRemoteBooks -> + viewModel.dataFlow.conflate().collect { sortedRemoteBooks -> binding.refreshProgressBar.isAutoLoading = false binding.tvEmptyMsg.isGone = sortedRemoteBooks.isNotEmpty() adapter.setItems(sortedRemoteBooks) @@ -120,7 +74,7 @@ class RemoteBookActivity : VMBaseActivity upPath() R.id.menu_log -> showDialogFragment() - R.id.menu_sort_auto -> { - item.isChecked = true - toastOnUi("menu_sort_auto") - sortCheck(Sort.Default) - upPath() - } R.id.menu_sort_name -> { item.isChecked = true - toastOnUi("menu_sort_name") sortCheck(Sort.Name) upPath() } R.id.menu_sort_time -> { item.isChecked = true - toastOnUi("menu_sort_time") - sortCheck(Sort.UpdateTime) + sortCheck(Sort.Default) upPath() } } @@ -158,8 +104,11 @@ class RemoteBookActivity : VMBaseActivity() var dataCallback: DataCallback? = null @@ -44,6 +46,14 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){ awaitClose { dataCallback = null } + }.map { list -> + if (sortAscending) when (sortKey) { + Sort.Name -> list.sortedWith(compareBy({ !it.isDir }, { it.filename })) + else -> list.sortedWith(compareBy({ !it.isDir }, { it.lastModify })) + } else when (sortKey) { + Sort.Name -> list.sortedWith(compareBy({ it.isDir }, { it.filename })).reversed() + else -> list.sortedWith(compareBy({ it.isDir }, { it.lastModify })).reversed() + } }.flowOn(Dispatchers.IO) init { diff --git a/app/src/main/java/io/legado/app/ui/book/remote/Sort.kt b/app/src/main/java/io/legado/app/ui/book/remote/Sort.kt new file mode 100644 index 000000000..363d484a0 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/remote/Sort.kt @@ -0,0 +1,5 @@ +package io.legado.app.ui.book.remote + +enum class Sort { + Default, Name +} \ No newline at end of file diff --git a/app/src/main/res/menu/book_remote.xml b/app/src/main/res/menu/book_remote.xml index d291d3f23..046a75372 100644 --- a/app/src/main/res/menu/book_remote.xml +++ b/app/src/main/res/menu/book_remote.xml @@ -20,12 +20,6 @@ android:id="@+id/menu_group_sort" android:checkableBehavior="single"> - - -