pull/2037/head
kunfei 2 years ago
parent 98f53dd9a0
commit 6dbfcf24fe
  1. 76
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookActivity.kt
  2. 14
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt
  3. 5
      app/src/main/java/io/legado/app/ui/book/remote/Sort.kt
  4. 7
      app/src/main/res/menu/book_remote.xml

@ -13,19 +13,14 @@ import io.legado.app.databinding.ActivityImportBookBinding
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.about.AppLogDialog
import io.legado.app.ui.book.remote.manager.RemoteBookWebDav 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.SelectActionBar
import io.legado.app.ui.widget.dialog.WaitDialog 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.showDialogFragment
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.File import java.io.File
import java.util.ArrayList
/** /**
* 展示远程书籍 * 展示远程书籍
@ -40,8 +35,6 @@ class RemoteBookActivity : VMBaseActivity<ActivityImportBookBinding, RemoteBookV
private val adapter by lazy { RemoteBookAdapter(this, this) } private val adapter by lazy { RemoteBookAdapter(this, this) }
private val waitDialog by lazy { WaitDialog(this) } private val waitDialog by lazy { WaitDialog(this) }
private var groupMenu: SubMenu? = null private var groupMenu: SubMenu? = null
private var sortKey = Sort.Default
private var sortAscending = false
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.titleBar.setTitle(R.string.remote_book) binding.titleBar.setTitle(R.string.remote_book)
initView() initView()
@ -58,57 +51,18 @@ class RemoteBookActivity : VMBaseActivity<ActivityImportBookBinding, RemoteBookV
} }
private fun sortCheck(sortKey: Sort) { private fun sortCheck(sortKey: Sort) {
if (this.sortKey == sortKey) { if (viewModel.sortKey == sortKey) {
sortAscending = !sortAscending viewModel.sortAscending = !viewModel.sortAscending
} else { } else {
sortAscending = true viewModel.sortAscending = true
this.sortKey = sortKey viewModel.sortKey = sortKey
} }
} }
private fun initData() { private fun initData() {
binding.refreshProgressBar.isAutoLoading = true binding.refreshProgressBar.isAutoLoading = true
launch { launch {
viewModel.dataFlow.conflate().map { remoteBooks -> viewModel.dataFlow.conflate().collect { sortedRemoteBooks ->
val dirList = ArrayList<RemoteBook>()
val bookList = ArrayList<RemoteBook>()
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 ->
binding.refreshProgressBar.isAutoLoading = false binding.refreshProgressBar.isAutoLoading = false
binding.tvEmptyMsg.isGone = sortedRemoteBooks.isNotEmpty() binding.tvEmptyMsg.isGone = sortedRemoteBooks.isNotEmpty()
adapter.setItems(sortedRemoteBooks) adapter.setItems(sortedRemoteBooks)
@ -120,7 +74,7 @@ class RemoteBookActivity : VMBaseActivity<ActivityImportBookBinding, RemoteBookV
private fun initEvent() { private fun initEvent() {
binding.tvGoBack.setOnClickListener { binding.tvGoBack.setOnClickListener {
goBackDir() goBackDir()
} }
} }
@ -133,22 +87,14 @@ class RemoteBookActivity : VMBaseActivity<ActivityImportBookBinding, RemoteBookV
when (item.itemId) { when (item.itemId) {
R.id.menu_refresh -> upPath() R.id.menu_refresh -> upPath()
R.id.menu_log -> showDialogFragment<AppLogDialog>() R.id.menu_log -> showDialogFragment<AppLogDialog>()
R.id.menu_sort_auto -> {
item.isChecked = true
toastOnUi("menu_sort_auto")
sortCheck(Sort.Default)
upPath()
}
R.id.menu_sort_name -> { R.id.menu_sort_name -> {
item.isChecked = true item.isChecked = true
toastOnUi("menu_sort_name")
sortCheck(Sort.Name) sortCheck(Sort.Name)
upPath() upPath()
} }
R.id.menu_sort_time -> { R.id.menu_sort_time -> {
item.isChecked = true item.isChecked = true
toastOnUi("menu_sort_time") sortCheck(Sort.Default)
sortCheck(Sort.UpdateTime)
upPath() upPath()
} }
} }
@ -158,8 +104,11 @@ class RemoteBookActivity : VMBaseActivity<ActivityImportBookBinding, RemoteBookV
override fun onPrepareOptionsMenu(menu: Menu): Boolean { override fun onPrepareOptionsMenu(menu: Menu): Boolean {
groupMenu = menu.findItem(R.id.menu_sort)?.subMenu groupMenu = menu.findItem(R.id.menu_sort)?.subMenu
groupMenu?.setGroupCheckable(R.id.menu_group_sort, true, true) groupMenu?.setGroupCheckable(R.id.menu_group_sort, true, true)
groupMenu?.findItem(R.id.menu_sort_name)?.isChecked = viewModel.sortKey == Sort.Name
groupMenu?.findItem(R.id.menu_sort_time)?.isChecked = viewModel.sortKey == Sort.Default
return super.onPrepareOptionsMenu(menu) return super.onPrepareOptionsMenu(menu)
} }
override fun revertSelection() { override fun revertSelection() {
adapter.revertSelection() adapter.revertSelection()
} }
@ -221,7 +170,4 @@ class RemoteBookActivity : VMBaseActivity<ActivityImportBookBinding, RemoteBookV
binding.selectActionBar.upCountView(adapter.selected.size, adapter.checkableCount) binding.selectActionBar.upCountView(adapter.selected.size, adapter.checkableCount)
} }
enum class Sort {
Default, Name, UpdateTime
}
} }

@ -10,10 +10,12 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import java.util.* import java.util.*
class RemoteBookViewModel(application: Application): BaseViewModel(application){ class RemoteBookViewModel(application: Application) : BaseViewModel(application) {
var sortKey = Sort.Default
var sortAscending = false
val dirList = arrayListOf<RemoteBook>() val dirList = arrayListOf<RemoteBook>()
var dataCallback: DataCallback? = null var dataCallback: DataCallback? = null
@ -44,6 +46,14 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){
awaitClose { awaitClose {
dataCallback = null 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) }.flowOn(Dispatchers.IO)
init { init {

@ -0,0 +1,5 @@
package io.legado.app.ui.book.remote
enum class Sort {
Default, Name
}

@ -20,12 +20,6 @@
android:id="@+id/menu_group_sort" android:id="@+id/menu_group_sort"
android:checkableBehavior="single"> android:checkableBehavior="single">
<item
android:id="@+id/menu_sort_default"
android:checkable="true"
android:checked="true"
android:title="@string/sort_default" />
<item <item
android:id="@+id/menu_sort_name" android:id="@+id/menu_sort_name"
android:checkable="true" android:checkable="true"
@ -36,7 +30,6 @@
android:checkable="true" android:checkable="true"
android:title="@string/sort_by_lastUpdateTime" /> android:title="@string/sort_by_lastUpdateTime" />
</group> </group>
</menu> </menu>

Loading…
Cancel
Save