view_toolbar_search: Switch to native toolbar for search

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-12-12 18:53:29 +07:00
parent 3ec134279f
commit fd134096c8
12 changed files with 222 additions and 332 deletions

View File

@@ -23,13 +23,8 @@ import android.net.Uri
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.ContextThemeWrapper
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.PopupMenu
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.content.res.AppCompatResources
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
@@ -83,71 +78,44 @@ class AppsGamesFragment : BaseFragment<FragmentGenericWithSearchBinding>() {
}
// Toolbar
binding.layoutToolbarNative.apply {
imgActionPrimary.visibility = View.VISIBLE
imgActionSecondary.visibility = View.VISIBLE
imgActionPrimary.setOnClickListener { findNavController().navigateUp() }
imgActionSecondary.apply {
setImageDrawable(
AppCompatResources.getDrawable(requireContext(), R.drawable.ic_menu)
)
setOnClickListener {
showMenu(it)
}
}
inputSearch.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s.isNullOrEmpty()) {
updateController(viewModel.apps.value)
} else {
val filteredPackages = viewModel.apps.value?.filter {
it.displayName.contains(s, true) || it.packageName.contains(s, true)
}
updateController(filteredPackages)
binding.toolbar.apply {
inflateMenu(R.menu.menu_import_export)
setNavigationOnClickListener { findNavController().navigateUp() }
setOnMenuItemClickListener {
when (it.itemId) {
R.id.action_export -> {
startForDocumentExport.launch(
"aurora_store_apps_${Calendar.getInstance().time.time}.json"
)
true
}
else -> false
}
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
})
}
}
private fun showMenu(anchor: View) {
val popupMenu = PopupMenu(
ContextThemeWrapper(
requireContext(),
R.style.AppTheme_PopupMenu
), anchor
)
val inflater: MenuInflater = popupMenu.menuInflater
inflater.inflate(R.menu.menu_import_export, popupMenu.menu)
popupMenu.menu.removeItem(R.id.action_import)
popupMenu.setOnMenuItemClickListener { menuItem: MenuItem ->
when (menuItem.itemId) {
R.id.action_export -> {
startForDocumentExport.launch(
"aurora_store_apps_${Calendar.getInstance().time.time}.json"
)
true
}
else -> false
}
}
popupMenu.show()
binding.searchBar.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s.isNullOrEmpty()) {
updateController(viewModel.apps.value)
} else {
val filteredPackages = viewModel.apps.value?.filter {
it.displayName.contains(s, true) || it.packageName.contains(s, true)
}
updateController(filteredPackages)
}
}
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
})
}
private fun updateController(packages: List<App>?) {

View File

@@ -24,13 +24,8 @@ import android.net.Uri
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.ContextThemeWrapper
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.PopupMenu
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.content.res.AppCompatResources
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
@@ -71,46 +66,62 @@ class BlacklistFragment : BaseFragment<FragmentGenericWithSearchBinding>() {
}
// Toolbar
binding.layoutToolbarNative.apply {
imgActionPrimary.visibility = View.VISIBLE
imgActionSecondary.apply {
visibility = View.VISIBLE
setImageDrawable(
AppCompatResources.getDrawable(requireContext(), R.drawable.ic_menu)
)
setOnClickListener {
showMenu(it)
}
}
imgActionPrimary.setOnClickListener {
binding.toolbar.apply {
inflateMenu(R.menu.menu_blacklist)
setNavigationOnClickListener {
viewModel.blacklistProvider.blacklist = viewModel.selected
findNavController().navigateUp()
}
setOnMenuItemClickListener {
when (it.itemId) {
R.id.action_import -> {
startForDocumentImport.launch(arrayOf(Constants.JSON_MIME_TYPE))
}
inputSearch.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s.isNullOrEmpty()) {
updateController(viewModel.packages.value)
} else {
val filteredPackages = viewModel.packages.value?.filter {
it.applicationInfo!!.loadLabel(requireContext().packageManager)
.contains(s, true) || it.packageName.contains(s, true)
}
updateController(filteredPackages)
R.id.action_export -> {
startForDocumentExport.launch(
"aurora_store_apps_${Calendar.getInstance().time.time}.json"
)
}
R.id.action_select_all -> {
viewModel.selectAll()
binding.recycler.requestModelBuild()
true
}
R.id.action_remove_all -> {
viewModel.removeAll()
binding.recycler.requestModelBuild()
true
}
}
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
})
true
}
}
binding.searchBar.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s.isNullOrEmpty()) {
updateController(viewModel.packages.value)
} else {
val filteredPackages = viewModel.packages.value?.filter {
it.applicationInfo!!.loadLabel(requireContext().packageManager)
.contains(s, true) || it.packageName.contains(s, true)
}
updateController(filteredPackages)
}
}
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
})
}
override fun onPause() {
@@ -118,49 +129,6 @@ class BlacklistFragment : BaseFragment<FragmentGenericWithSearchBinding>() {
viewModel.blacklistProvider.blacklist = viewModel.selected
}
private fun showMenu(anchor: View) {
val popupMenu = PopupMenu(
ContextThemeWrapper(
requireContext(),
R.style.AppTheme_PopupMenu
), anchor
)
val inflater: MenuInflater = popupMenu.menuInflater
inflater.inflate(R.menu.menu_blacklist, popupMenu.menu)
popupMenu.setOnMenuItemClickListener { menuItem: MenuItem ->
when (menuItem.itemId) {
R.id.action_import -> {
startForDocumentImport.launch(arrayOf(Constants.JSON_MIME_TYPE))
true
}
R.id.action_export -> {
startForDocumentExport.launch(
"aurora_store_blacklist_${Calendar.getInstance().time.time}.json"
)
true
}
R.id.action_select_all -> {
viewModel.selectAll()
binding.recycler.requestModelBuild()
true
}
R.id.action_remove_all -> {
viewModel.removeAll()
binding.recycler.requestModelBuild()
true
}
else -> false
}
}
popupMenu.show()
}
private fun updateController(packages: List<PackageInfo>?) {
binding.recycler.withModels {
setFilterDuplicates(true)

View File

@@ -50,7 +50,6 @@ import com.aurora.store.view.epoxy.views.app.NoAppViewModel_
import com.aurora.store.view.epoxy.views.shimmer.AppListViewShimmerModel_
import com.aurora.store.view.ui.commons.BaseFragment
import com.aurora.store.viewmodel.search.SearchResultViewModel
import com.google.android.material.textfield.TextInputEditText
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
@@ -59,8 +58,6 @@ class SearchResultsFragment : BaseFragment<FragmentSearchResultBinding>(),
private val viewModel: SearchResultViewModel by viewModels()
private lateinit var searchView: TextInputEditText
private lateinit var sharedPreferences: SharedPreferences
private var query: String? = null
@@ -84,20 +81,20 @@ class SearchResultsFragment : BaseFragment<FragmentSearchResultBinding>(),
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
// Toolbar
binding.layoutViewToolbar.apply {
searchView = inputSearch
imgActionPrimary.setOnClickListener {
binding.toolbar.apply {
setNavigationOnClickListener {
binding.searchBar.hideKeyboard()
findNavController().navigateUp()
}
imgActionSecondary.setOnClickListener {
findNavController().navigate(R.id.downloadFragment)
}
clearButton.apply {
visibility = if (query.isNullOrBlank()) View.GONE else View.VISIBLE
setOnClickListener {
searchView.text?.clear()
searchView.showKeyboard()
setOnMenuItemClickListener {
when (it.itemId) {
R.id.action_clear -> {
binding.searchBar.text?.clear()
binding.searchBar.showKeyboard()
}
R.id.action_download -> findNavController().navigate(R.id.downloadFragment)
}
true
}
}
@@ -186,30 +183,29 @@ class SearchResultsFragment : BaseFragment<FragmentSearchResultBinding>(),
}
}
} else {
binding.recycler
.withModels {
setFilterDuplicates(true)
binding.recycler.withModels {
setFilterDuplicates(true)
filteredAppList.forEach { app ->
add(
AppListViewModel_()
.id(app.id)
.app(app)
.click(View.OnClickListener {
searchView.hideKeyboard()
openDetailsFragment(app.packageName, app)
})
)
}
if (searchBundle.subBundles.isNotEmpty()) {
add(
AppProgressViewModel_()
.id("progress")
)
}
filteredAppList.forEach { app ->
add(
AppListViewModel_()
.id(app.id)
.app(app)
.click(View.OnClickListener {
binding.searchBar.hideKeyboard()
openDetailsFragment(app.packageName, app)
})
)
}
if (searchBundle.subBundles.isNotEmpty()) {
add(
AppProgressViewModel_()
.id("progress")
)
}
}
binding.recycler.adapter?.let {
if (it.itemCount < 10) {
viewModel.next(searchBundle.subBundles)
@@ -219,28 +215,22 @@ class SearchResultsFragment : BaseFragment<FragmentSearchResultBinding>(),
}
private fun attachSearch() {
searchView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
binding.searchBar.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (s.isNotEmpty()) {
binding.layoutViewToolbar.clearButton.visibility = View.VISIBLE
} else {
binding.layoutViewToolbar.clearButton.visibility = View.GONE
}
binding.toolbar.menu.findItem(R.id.action_clear)?.isVisible = s.isNotBlank()
}
override fun afterTextChanged(s: Editable) {}
})
searchView.setOnEditorActionListener { _: TextView?, actionId: Int, _: KeyEvent? ->
binding.searchBar.setOnEditorActionListener { _: TextView?, actionId: Int, _: KeyEvent? ->
if (actionId == EditorInfo.IME_ACTION_SEARCH
|| actionId == KeyEvent.ACTION_DOWN
|| actionId == KeyEvent.KEYCODE_ENTER
) {
query = searchView.text.toString()
query = binding.searchBar.text.toString()
query?.let {
requireArguments().putString("query", it)
queryViewModel(it)
@@ -252,8 +242,8 @@ class SearchResultsFragment : BaseFragment<FragmentSearchResultBinding>(),
}
private fun updateQuery(query: String) {
searchView.text = Editable.Factory.getInstance().newEditable(query)
searchView.setSelection(query.length)
binding.searchBar.text = Editable.Factory.getInstance().newEditable(query)
binding.searchBar.setSelection(query.length)
queryViewModel(query)
}
@@ -277,7 +267,6 @@ class SearchResultsFragment : BaseFragment<FragmentSearchResultBinding>(),
.toList()
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == PREFERENCE_FILTER) query?.let { queryViewModel(it) }
}

View File

@@ -37,7 +37,6 @@ import com.aurora.store.databinding.FragmentSearchSuggestionBinding
import com.aurora.store.view.epoxy.views.SearchSuggestionViewModel_
import com.aurora.store.view.ui.commons.BaseFragment
import com.aurora.store.viewmodel.search.SearchSuggestionViewModel
import com.google.android.material.textfield.TextInputEditText
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
@@ -47,26 +46,21 @@ class SearchSuggestionFragment : BaseFragment<FragmentSearchSuggestionBinding>()
private val viewModel: SearchSuggestionViewModel by viewModels()
private lateinit var searchView: TextInputEditText
private var query: String = String()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Toolbar
binding.layoutToolbarSearch.apply {
searchView = inputSearch
imgActionPrimary.setOnClickListener {
searchView.hideKeyboard()
binding.toolbar.apply {
setNavigationOnClickListener {
binding.searchBar.hideKeyboard()
findNavController().navigateUp()
}
imgActionSecondary.setOnClickListener {
findNavController().navigate(R.id.downloadFragment)
}
clearButton.apply {
visibility = if (query.isBlank()) View.GONE else View.VISIBLE
setOnClickListener { searchView.text?.clear() }
setOnMenuItemClickListener {
when (it.itemId) {
R.id.action_clear -> binding.searchBar.text?.clear()
R.id.action_download -> findNavController().navigate(R.id.downloadFragment)
}
true
}
}
@@ -81,9 +75,7 @@ class SearchSuggestionFragment : BaseFragment<FragmentSearchSuggestionBinding>()
override fun onResume() {
super.onResume()
if (::searchView.isInitialized) {
searchView.showKeyboard()
}
binding.searchBar.showKeyboard()
}
private fun updateController(searchSuggestions: List<SearchSuggestEntry>) {
@@ -98,7 +90,7 @@ class SearchSuggestionFragment : BaseFragment<FragmentSearchSuggestionBinding>()
updateQuery(it.title)
}
.click { _ ->
searchView.hideKeyboard()
binding.searchBar.hideKeyboard()
search(it.title)
}
)
@@ -107,32 +99,30 @@ class SearchSuggestionFragment : BaseFragment<FragmentSearchSuggestionBinding>()
}
private fun setupSearch() {
searchView.addTextChangedListener(object : TextWatcher {
binding.searchBar.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (s.isNotEmpty()) {
query = s.toString()
val query = s.toString()
if (query.isNotEmpty()) {
viewModel.observeStreamBundles(query)
}
binding.layoutToolbarSearch.clearButton.visibility = View.VISIBLE
} else {
binding.layoutToolbarSearch.clearButton.visibility = View.GONE
}
binding.toolbar.menu.findItem(R.id.action_clear)?.isVisible = s.isNotBlank()
}
override fun afterTextChanged(s: Editable) {}
})
searchView.setOnEditorActionListener { _: TextView?, actionId: Int, _: KeyEvent? ->
binding.searchBar.setOnEditorActionListener { _: TextView?, actionId: Int, _: KeyEvent? ->
if (actionId == EditorInfo.IME_ACTION_SEARCH
|| actionId == KeyEvent.ACTION_DOWN
|| actionId == KeyEvent.KEYCODE_ENTER
) {
query = searchView.text.toString()
val query = binding.searchBar.text.toString()
if (query.isNotEmpty()) {
searchView.hideKeyboard()
binding.searchBar.hideKeyboard()
search(query)
return@setOnEditorActionListener true
}
@@ -142,8 +132,8 @@ class SearchSuggestionFragment : BaseFragment<FragmentSearchSuggestionBinding>()
}
private fun updateQuery(query: String) {
searchView.text = Editable.Factory.getInstance().newEditable(query)
searchView.setSelection(query.length)
binding.searchBar.text = Editable.Factory.getInstance().newEditable(query)
binding.searchBar.setSelection(query.length)
}
private fun search(query: String) {