Skip to content

Commit

Permalink
fix: search query stays the old one during back presses
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Sep 30, 2023
1 parent ac5bf43 commit acc43b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class MainActivity : BaseActivity() {
private val subscriptionsViewModel: SubscriptionsViewModel by viewModels()

private var savedSearchQuery: String? = null
private var shouldOpenSuggestions = true

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -283,6 +284,8 @@ class MainActivity : BaseActivity() {
}

override fun onQueryTextChange(newText: String?): Boolean {
if (!shouldOpenSuggestions) return true

// Prevent navigation when search view is collapsed
if (searchView.isIconified ||
binding.bottomNav.menu.children.any {
Expand Down Expand Up @@ -356,6 +359,15 @@ class MainActivity : BaseActivity() {
return super.onCreateOptionsMenu(menu)
}

/**
* Update the query text in the search bar without opening the search suggestions
*/
fun setQuerySilent(query: String) {
shouldOpenSuggestions = false
searchView.setQuery(query, false)
shouldOpenSuggestions = true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.adapters.SearchAdapter
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.util.TextUtils
Expand Down Expand Up @@ -60,6 +61,10 @@ class SearchResultFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// fixes a bug that the search query will stay the old one when searching for multiple
// different queries in a row and navigating to the previous ones through back presses
(context as MainActivity).setQuerySilent(query)

binding.searchRecycler.layoutManager = LinearLayoutManager(requireContext())

// add the query to the history
Expand Down

0 comments on commit acc43b8

Please sign in to comment.