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

This commit is contained in:
HapeLee committed 2025-10-27 01:45:30 +08:00
1 parent 251322a5d8
commit 4688da7c04
3 files changed
+45 -20

No files matched your search

@@ -114,6 +114,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onMenuOpened(featureId: Int, menu: Menu): Boolean { override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
buildSearchScopeMenu(menu) buildSearchScopeMenu(menu)
updateSearchScopeChip()
return super.onMenuOpened(featureId, menu) return super.onMenuOpened(featureId, menu)
} }
@@ -161,36 +162,43 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
alertSearchScope() alertSearchScope()
} }
binding.chipGroup.setOnClickListener { view -> binding.chipGroup.setOnClickListener { view ->
binding.chipGroup.isChecked = true
val popup = PopupMenu(this, view) val popup = PopupMenu(this, view)
val menu = popup.menu val menu = popup.menu
buildSearchScopeMenu(menu) buildSearchScopeMenu(menu)
updateSearchScopeChip()
var fromGroup1 = false
popup.setOnMenuItemClickListener { item -> popup.setOnMenuItemClickListener { item ->
when (item.groupId) { 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 -> { R.id.menu_group_2 -> {
if (item.itemId == R.id.menu_1) { if (item.itemId == R.id.menu_1) {
"" viewModel.searchScope.update("")
fromGroup1 = true
} else { } else {
item.title.toString() viewModel.searchScope.update(item.title.toString())
fromGroup1 = false
} }
} }
else -> item.title.toString() else -> {
}
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()) viewModel.searchScope.update(item.title.toString())
fromGroup1 = false
} }
} }
updateSelectedGroup() updateSelectedGroup()
true true
} }
popup.setOnDismissListener {
binding.chipGroup.isChecked = !fromGroup1
updateSearchScopeChip()
}
popup.show() popup.show()
} }
searchBar.setOnMenuItemClickListener { item -> searchBar.setOnMenuItemClickListener { item ->
onCompatOptionsItemSelected(item) onCompatOptionsItemSelected(item)
} }
@@ -257,12 +265,10 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} }
private fun updateSelectedGroup() { private fun updateSelectedGroup() {
val chip = binding.chipGroup
val displayNames = viewModel.searchScope.displayNames val displayNames = viewModel.searchScope.displayNames
val groupName = displayNames.firstOrNull().orEmpty() val groupName = displayNames.firstOrNull().orEmpty()
chip.text = groupName.ifEmpty { getString(R.string.search_select_group) } binding.chipGroup.text = groupName.ifEmpty { getString(R.string.search_select_group) }
chip.isCheckable = true binding.chipGroup.isChecked = groupName.isNotEmpty()
chip.isChecked = groupName.isNotEmpty()
} }
private fun buildSearchScopeMenu(menu: Menu) { private fun buildSearchScopeMenu(menu: Menu) {
@@ -609,6 +615,24 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
override fun onSearchScopeOk(searchScope: SearchScope) { override fun onSearchScopeOk(searchScope: SearchScope) {
viewModel.searchScope.update(searchScope.toString()) 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() { private fun alertSearchScope() {
@@ -127,6 +127,7 @@
<com.google.android.material.chip.ChipGroup <com.google.android.material.chip.ChipGroup
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:chipSpacingVertical="0dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_marginHorizontal="16dp"> android:layout_marginHorizontal="16dp">
<com.google.android.material.chip.Chip <com.google.android.material.chip.Chip
@@ -139,13 +140,13 @@
android:id="@+id/chip_search_scope" android:id="@+id/chip_search_scope"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:clickable="true" android:text="@string/all_source"/>
android:text="@string/search_scope"/>
<com.google.android.material.chip.Chip <com.google.android.material.chip.Chip
android:id="@+id/chip_group" android:id="@+id/chip_group"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:clickable="true" android:visibility="gone"
android:checkable="true"
android:text="@string/search_select_group"/> android:text="@string/search_select_group"/>
</com.google.android.material.chip.ChipGroup> </com.google.android.material.chip.ChipGroup>
+1 -1
View File
@@ -1,4 +1,4 @@
VERSION_MAJOR=3 VERSION_MAJOR=3
VERSION_MINOR=25 VERSION_MINOR=25
VERSION_PATCH=12 VERSION_PATCH=13
VERSION_SUFFIX= VERSION_SUFFIX=