优化搜索页面的按钮文字显示 #149

This commit is contained in:
HapeLee
2025-10-27 01:45:30 +08:00
parent 251322a5d8
commit 4688da7c04
3 changed files with 45 additions and 20 deletions
@@ -114,6 +114,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
buildSearchScopeMenu(menu)
updateSearchScopeChip()
return super.onMenuOpened(featureId, menu)
}
@@ -161,36 +162,43 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
alertSearchScope()
}
binding.chipGroup.setOnClickListener { view ->
binding.chipGroup.isChecked = true
val popup = PopupMenu(this, view)
val menu = popup.menu
buildSearchScopeMenu(menu)
updateSearchScopeChip()
var fromGroup1 = false
popup.setOnMenuItemClickListener { item ->
when (item.groupId) {
R.id.menu_group_1 -> item.title.toString()
R.id.menu_group_1 -> {
viewModel.searchScope.remove(item.title.toString())
fromGroup1 = true
}
R.id.menu_group_2 -> {
if (item.itemId == R.id.menu_1) {
""
viewModel.searchScope.update("")
fromGroup1 = true
} else {
item.title.toString()
viewModel.searchScope.update(item.title.toString())
fromGroup1 = false
}
}
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 {
else -> {
viewModel.searchScope.update(item.title.toString())
fromGroup1 = false
}
}
updateSelectedGroup()
true
}
popup.setOnDismissListener {
binding.chipGroup.isChecked = !fromGroup1
updateSearchScopeChip()
}
popup.show()
}
searchBar.setOnMenuItemClickListener { item ->
onCompatOptionsItemSelected(item)
}
@@ -257,12 +265,10 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
}
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()
binding.chipGroup.text = groupName.ifEmpty { getString(R.string.search_select_group) }
binding.chipGroup.isChecked = groupName.isNotEmpty()
}
private fun buildSearchScopeMenu(menu: Menu) {
@@ -609,6 +615,24 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onSearchScopeOk(searchScope: SearchScope) {
viewModel.searchScope.update(searchScope.toString())
updateSearchScopeChip()
}
private fun updateSearchScopeChip() {
binding.chipSearchScope.isCheckable = true
val scopeNames = viewModel.searchScope.displayNames
if (scopeNames.isEmpty() || viewModel.searchScope.isAll()) {
binding.chipSearchScope.apply {
isChecked = false
text = getString(R.string.all_source)
}
} else {
binding.chipSearchScope.apply {
isChecked = true
text = scopeNames.joinToString(", ")
}
}
binding.chipSearchScope.isCheckable = false
}
private fun alertSearchScope() {
@@ -127,6 +127,7 @@
<com.google.android.material.chip.ChipGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:chipSpacingVertical="0dp"
android:layout_marginTop="8dp"
android:layout_marginHorizontal="16dp">
<com.google.android.material.chip.Chip
@@ -139,13 +140,13 @@
android:id="@+id/chip_search_scope"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/search_scope"/>
android:text="@string/all_source"/>
<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:visibility="gone"
android:checkable="true"
android:text="@string/search_select_group"/>
</com.google.android.material.chip.ChipGroup>
+1 -1
View File
@@ -1,4 +1,4 @@
VERSION_MAJOR=3
VERSION_MINOR=25
VERSION_PATCH=12
VERSION_PATCH=13
VERSION_SUFFIX=