书架管理添加筛选

pull/2424/head
kunfei 2 years ago
parent 3aa346c764
commit 08ad0f1314
  1. 76
      app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt
  2. 3
      app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt
  3. 13
      app/src/main/res/menu/bookshelf_manage.xml

@ -6,6 +6,7 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
@ -34,6 +35,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -55,23 +57,26 @@ class BookshelfManageActivity :
private val adapter by lazy { BookAdapter(this, this) } private val adapter by lazy { BookAdapter(this, this) }
private var booksFlowJob: Job? = null private var booksFlowJob: Job? = null
private var menu: Menu? = null private var menu: Menu? = null
private var groupId: Long = -1 private var searchView: SearchView? = null
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
groupId = intent.getLongExtra("groupId", -1) viewModel.groupId = intent.getLongExtra("groupId", -1)
launch { launch {
binding.titleBar.subtitle = withContext(IO) { viewModel.groupName = withContext(IO) {
appDb.bookGroupDao.getByID(groupId)?.groupName appDb.bookGroupDao.getByID(viewModel.groupId)?.groupName
?: getString(R.string.no_group) ?: getString(R.string.no_group)
} }
binding.titleBar.subtitle = viewModel.groupName
} }
initView() initRecyclerView()
initOtherView()
initGroupData() initGroupData()
initBookData() upBookData()
} }
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.bookshelf_manage, menu) menuInflater.inflate(R.menu.bookshelf_manage, menu)
initSearchView(menu)
return super.onCompatCreateOptionsMenu(menu) return super.onCompatCreateOptionsMenu(menu)
} }
@ -93,7 +98,37 @@ class BookshelfManageActivity :
selectGroup(groupRequestCode, 0) selectGroup(groupRequestCode, 0)
} }
private fun initView() { private fun showTitle() {
binding.titleBar.title = getString(R.string.bookshelf_management)
binding.titleBar.subtitle = viewModel.groupName
}
private fun initSearchView(menu: Menu) {
searchView = menu.findItem(R.id.menu_screen).actionView as SearchView
searchView?.run {
setOnCloseListener {
showTitle()
false
}
setOnSearchClickListener {
binding.titleBar.title = ""
binding.titleBar.subtitle = ""
}
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
upBookData(newText)
return false
}
})
}
}
private fun initRecyclerView() {
binding.recyclerView.setEdgeEffectColor(primaryColor) binding.recyclerView.setEdgeEffectColor(primaryColor)
binding.recyclerView.layoutManager = LinearLayoutManager(this) binding.recyclerView.layoutManager = LinearLayoutManager(this)
binding.recyclerView.addItemDecoration(VerticalDivider(this)) binding.recyclerView.addItemDecoration(VerticalDivider(this))
@ -107,6 +142,9 @@ class BookshelfManageActivity :
dragSelectTouchHelper.activeSlideSelect() dragSelectTouchHelper.activeSlideSelect()
// Note: need judge selection first, so add ItemTouchHelper after it. // Note: need judge selection first, so add ItemTouchHelper after it.
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recyclerView) ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recyclerView)
}
private fun initOtherView() {
binding.selectActionBar.setMainActionText(R.string.move_to_group) binding.selectActionBar.setMainActionText(R.string.move_to_group)
binding.selectActionBar.inflateMenu(R.menu.bookshelf_menage_sel) binding.selectActionBar.inflateMenu(R.menu.bookshelf_menage_sel)
binding.selectActionBar.setOnMenuItemClickListener(this) binding.selectActionBar.setOnMenuItemClickListener(this)
@ -136,18 +174,25 @@ class BookshelfManageActivity :
} }
} }
private fun initBookData() { private fun upBookData(searchKey: String? = null) {
booksFlowJob?.cancel() booksFlowJob?.cancel()
booksFlowJob = launch { booksFlowJob = launch {
when (groupId) { when (viewModel.groupId) {
AppConst.rootGroupId -> appDb.bookDao.flowNetNoGroup() AppConst.rootGroupId -> appDb.bookDao.flowNetNoGroup()
AppConst.bookGroupAllId -> appDb.bookDao.flowAll() AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal() AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio() AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
AppConst.bookGroupNetNoneId -> appDb.bookDao.flowNetNoGroup() AppConst.bookGroupNetNoneId -> appDb.bookDao.flowNetNoGroup()
AppConst.bookGroupLocalNoneId -> appDb.bookDao.flowLocalNoGroup() AppConst.bookGroupLocalNoneId -> appDb.bookDao.flowLocalNoGroup()
else -> appDb.bookDao.flowByGroup(groupId) else -> appDb.bookDao.flowByGroup(viewModel.groupId)
}.conflate().map { books -> }.conflate().map { list ->
val books = if (searchKey.isNullOrBlank()) {
list
} else {
list.filter {
it.name.contains(searchKey) || it.author.contains(searchKey)
}
}
when (getPrefInt(PreferKey.bookshelfSort)) { when (getPrefInt(PreferKey.bookshelfSort)) {
1 -> books.sortedByDescending { 1 -> books.sortedByDescending {
it.latestChapterTime it.latestChapterTime
@ -162,7 +207,8 @@ class BookshelfManageActivity :
it.durChapterTime it.durChapterTime
} }
} }
}.conflate().collect { books -> }.flowOn(IO)
.conflate().collect { books ->
adapter.setItems(books) adapter.setItems(books)
} }
} }
@ -172,9 +218,11 @@ class BookshelfManageActivity :
when (item.itemId) { when (item.itemId) {
R.id.menu_group_manage -> showDialogFragment<GroupManageDialog>() R.id.menu_group_manage -> showDialogFragment<GroupManageDialog>()
else -> if (item.groupId == R.id.menu_group) { else -> if (item.groupId == R.id.menu_group) {
viewModel.groupName = item.title.toString()
binding.titleBar.subtitle = item.title binding.titleBar.subtitle = item.title
groupId = appDb.bookGroupDao.getByName(item.title.toString())?.groupId ?: 0 viewModel.groupId =
initBookData() appDb.bookGroupDao.getByName(item.title.toString())?.groupId ?: 0
upBookData()
} }
} }
return super.onCompatOptionsItemSelected(item) return super.onCompatOptionsItemSelected(item)

@ -13,7 +13,8 @@ import io.legado.app.utils.toastOnUi
class BookshelfManageViewModel(application: Application) : BaseViewModel(application) { class BookshelfManageViewModel(application: Application) : BaseViewModel(application) {
var groupId: Long = -1L
var groupName: String? = null
val batchChangeSourceState = mutableStateOf(false) val batchChangeSourceState = mutableStateOf(false)
val batchChangeSourceSize = mutableStateOf(0) val batchChangeSourceSize = mutableStateOf(0)
val batchChangeSourcePosition = mutableStateOf(0) val batchChangeSourcePosition = mutableStateOf(0)

@ -1,11 +1,20 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="AlwaysShowAction">
<item
android:id="@+id/menu_screen"
android:icon="@drawable/ic_screen"
android:title="@string/screen"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
<item <item
android:id="@+id/menu_book_group" android:id="@+id/menu_book_group"
android:title="@string/group"
android:icon="@drawable/ic_groups" android:icon="@drawable/ic_groups"
android:title="@string/group"
app:showAsAction="always"> app:showAsAction="always">
<menu> <menu>

Loading…
Cancel
Save