Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: search query stays the old one during back presses #4874

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading