为搜索二级界面添加菜单功能

This commit is contained in:
HapeLee
2025-10-20 01:16:05 +08:00
parent 407b20d891
commit e67481fcf6
6 changed files with 139 additions and 54 deletions
@@ -60,7 +60,6 @@ import io.legato.kazusa.lib.dialogs.alert
import io.legato.kazusa.lib.dialogs.selector
import io.legato.kazusa.model.ReadAloud
import io.legato.kazusa.model.ReadBook
import io.legato.kazusa.model.ReadManga
import io.legato.kazusa.model.analyzeRule.AnalyzeRule
import io.legato.kazusa.model.analyzeRule.AnalyzeRule.Companion.setChapter
import io.legato.kazusa.model.analyzeRule.AnalyzeRule.Companion.setCoroutineContext
@@ -9,6 +9,7 @@ import android.view.MenuItem
import android.view.View.VISIBLE
import android.view.inputmethod.EditorInfo
import androidx.activity.viewModels
import androidx.appcompat.widget.PopupMenu
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
@@ -88,13 +89,12 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setSupportActionBar(binding.searchBar)
initRecyclerView()
initMaterialSearch()
initOtherView()
initData()
updateSelectedGroup()
receiptIntent(intent)
}
@@ -112,79 +112,84 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
}
override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
menu.transaction {
menu.removeGroup(R.id.menu_group_1)
menu.removeGroup(R.id.menu_group_2)
var hasChecked = false
val searchScopeNames = viewModel.searchScope.displayNames
if (viewModel.searchScope.isSource()) {
menu.add(R.id.menu_group_1, Menu.NONE, Menu.NONE, searchScopeNames.first()).apply {
isChecked = true
hasChecked = true
}
}
val allSourceMenu =
menu.add(R.id.menu_group_2, R.id.menu_1, Menu.NONE, getString(R.string.all_source))
.apply {
if (searchScopeNames.isEmpty()) {
isChecked = true
hasChecked = true
}
}
groups?.forEach {
if (searchScopeNames.contains(it)) {
menu.add(R.id.menu_group_1, Menu.NONE, Menu.NONE, it).apply {
isChecked = true
hasChecked = true
}
} else {
menu.add(R.id.menu_group_2, Menu.NONE, Menu.NONE, it)
}
}
if (!hasChecked) {
viewModel.searchScope.update("")
allSourceMenu.isChecked = true
}
menu.setGroupCheckable(R.id.menu_group_1, true, false)
menu.setGroupCheckable(R.id.menu_group_2, true, true)
}
buildSearchScopeMenu(menu)
return super.onMenuOpened(featureId, menu)
}
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_precision_search -> {
putPrefBoolean(
PreferKey.precisionSearch,
!getPrefBoolean(PreferKey.precisionSearch)
)
precisionSearchMenuItem?.isChecked = getPrefBoolean(PreferKey.precisionSearch)
searchView.text.toString().trim().let {
searchView.setText(it)
val newValue = !getPrefBoolean(PreferKey.precisionSearch)
putPrefBoolean(PreferKey.precisionSearch, newValue)
precisionSearchMenuItem?.isChecked = newValue
binding.chipPrecisionSearch.isChecked = newValue
binding.searchView.text.toString().trim().let {
binding.searchView.setText(it)
}
}
R.id.menu_search_scope -> alertSearchScope()
R.id.menu_source_manage -> startActivity<BookSourceActivity>()
R.id.menu_log -> showDialogFragment(AppLogDialog())
R.id.menu_1 -> viewModel.searchScope.update("")
R.id.menu_1 -> {
viewModel.searchScope.update("")
updateSelectedGroup()
}
else -> {
if (item.groupId == R.id.menu_group_1) {
viewModel.searchScope.remove(item.title.toString())
} else if (item.groupId == R.id.menu_group_2) {
viewModel.searchScope.update(item.title.toString())
}
updateSelectedGroup()
}
}
return super.onCompatOptionsItemSelected(item)
}
private fun initMaterialSearch() {
binding.chipPrecisionSearch.isChecked = getPrefBoolean(PreferKey.precisionSearch)
binding.chipPrecisionSearch.setOnCheckedChangeListener { chip, isChecked ->
putPrefBoolean(PreferKey.precisionSearch, isChecked)
precisionSearchMenuItem?.isChecked = isChecked
binding.searchView.text.toString().trim().let {
binding.searchView.setText(it)
}
}
binding.chipSearchScope.setOnClickListener {
alertSearchScope()
}
binding.chipGroup.setOnClickListener { view ->
val popup = PopupMenu(this, view)
val menu = popup.menu
buildSearchScopeMenu(menu)
popup.setOnMenuItemClickListener { item ->
when (item.groupId) {
R.id.menu_group_1 -> item.title.toString()
R.id.menu_group_2 -> {
if (item.itemId == R.id.menu_1) {
""
} else {
item.title.toString()
}
}
else -> item.title.toString()
}
if (item.groupId == R.id.menu_group_1) {
viewModel.searchScope.remove(item.title.toString())
} else {
if (item.itemId == R.id.menu_1) {
viewModel.searchScope.update("")
} else {
viewModel.searchScope.update(item.title.toString())
}
}
updateSelectedGroup()
true
}
popup.show()
}
searchBar.setOnMenuItemClickListener { item ->
onCompatOptionsItemSelected(item)
}
@@ -236,6 +241,60 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
visibleInputHelp()
}
private fun updateSelectedGroup() {
val chip = binding.chipGroup
val displayNames = viewModel.searchScope.displayNames
val groupName = displayNames.firstOrNull().orEmpty()
chip.text = groupName.ifEmpty { getString(R.string.search_select_group) }
chip.isCheckable = true
chip.isChecked = groupName.isNotEmpty()
}
private fun buildSearchScopeMenu(menu: Menu) {
menu.transaction {
menu.removeGroup(R.id.menu_group_1)
menu.removeGroup(R.id.menu_group_2)
var hasChecked = false
val searchScopeNames = viewModel.searchScope.displayNames
if (viewModel.searchScope.isSource()) {
menu.add(R.id.menu_group_1, Menu.NONE, Menu.NONE, searchScopeNames.first()).apply {
isChecked = true
hasChecked = true
}
}
val allSourceMenu =
menu.add(R.id.menu_group_2, R.id.menu_1, Menu.NONE, getString(R.string.all_source))
.apply {
if (searchScopeNames.isEmpty()) {
isChecked = true
hasChecked = true
}
}
groups?.forEach { groupName ->
if (searchScopeNames.contains(groupName)) {
menu.add(R.id.menu_group_1, Menu.NONE, Menu.NONE, groupName).apply {
isChecked = true
hasChecked = true
}
} else {
menu.add(R.id.menu_group_2, Menu.NONE, Menu.NONE, groupName)
}
}
if (!hasChecked) {
viewModel.searchScope.update("")
allSourceMenu.isChecked = true
}
menu.setGroupCheckable(R.id.menu_group_1, true, false)
menu.setGroupCheckable(R.id.menu_group_2, true, true)
}
}
private fun initRecyclerView() {
binding.rvBookshelfSearch.layoutManager = FlexboxLayoutManager(this)
binding.rvBookshelfSearch.adapter = bookAdapter
@@ -109,6 +109,7 @@
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="@menu/book_search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
@@ -123,6 +124,31 @@
android:orientation="vertical"
android:visibility="visible">
<com.google.android.material.chip.ChipGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginHorizontal="16dp">
<com.google.android.material.chip.Chip
android:id="@+id/chip_precision_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkable="true"
android:text="@string/precision_search"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_search_scope"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/search_scope"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/search_select_group"/>
</com.google.android.material.chip.ChipGroup>
<TextView
android:id="@+id/tv_book_show"
android:layout_width="match_parent"
@@ -144,8 +170,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingHorizontal="16dp"
android:paddingTop="4dp">
android:paddingHorizontal="16dp">
<TextView
android:id="@+id/tv_history"
+1 -1
View File
@@ -16,7 +16,7 @@
<item
android:id="@+id/menu_search_scope"
android:title="@string/groups_or_source"
android:title="@string/search_scope"
app:showAsAction="never" />
<item
+1
View File
@@ -1299,4 +1299,5 @@
<string name="tip_custom_mode">切换至自定义主题或调整自定义设置时,部分选项需要重启。</string>
<string name="restart_required_message">此更改需要重启应用才能生效,是否立即重启?</string>
<string name="restart_later_message">更改将在下次启动应用时生效</string>
<string name="search_select_group">选择书源分组</string>
</resources>
+1
View File
@@ -1300,4 +1300,5 @@
<string name="tip_custom_mode">切换至自定义主题或调整自定义设置时,部分选项需要重启。</string>
<string name="restart_required_message">修改该选项需要重启</string>
<string name="restart_later_message">稍后重启</string>
<string name="search_select_group">选择书源分组</string>
</resources>