From d4904136c0f53a48dc0bdf5dcd0ff155d694dc99 Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Sat, 8 Jan 2022 09:45:14 -0700 Subject: [PATCH] search: open kb only when created Make it so that the keyboard is only opened on the search view when it's initially created instead of when it's loaded from the backstack. This is just nice for overall UX. --- app/build.gradle | 4 ++-- .../oxycblt/auxio/search/SearchFragment.kt | 20 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7766e114f..63b9932cc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,7 +4,7 @@ apply plugin: "kotlin-kapt" apply plugin: "androidx.navigation.safeargs.kotlin" android { - compileSdkVersion 32 + compileSdkVersion 31 buildToolsVersion "31.0.0" defaultConfig { @@ -13,7 +13,7 @@ android { versionCode 10 minSdkVersion 21 - targetSdkVersion 32 + targetSdkVersion 31 buildFeatures { dataBinding true diff --git a/app/src/main/java/org/oxycblt/auxio/search/SearchFragment.kt b/app/src/main/java/org/oxycblt/auxio/search/SearchFragment.kt index 67202347c..b39647326 100644 --- a/app/src/main/java/org/oxycblt/auxio/search/SearchFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/search/SearchFragment.kt @@ -55,6 +55,9 @@ class SearchFragment : Fragment() { private val playbackModel: PlaybackViewModel by activityViewModels() private val detailModel: DetailViewModel by activityViewModels() + private var launchedKeyboard = false + private var mustScrollUp = false + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -103,15 +106,20 @@ class SearchFragment : Fragment() { binding.searchEditText.apply { addTextChangedListener { text -> + mustScrollUp = true // Run the search with the updated text as the query searchModel.search(text?.toString() ?: "") } - // Auto-open the keyboard when this view is shown - requestFocus() + if (!launchedKeyboard) { + // Auto-open the keyboard when this view is shown + requestFocus() - postDelayed(200) { - imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) + postDelayed(200) { + imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) + } + + launchedKeyboard = true } } @@ -127,7 +135,9 @@ class SearchFragment : Fragment() { searchModel.searchResults.observe(viewLifecycleOwner) { results -> searchAdapter.submitList(results) { - // We've just scrolled back to the top, reset the lifted state + // I would make it so that the position is only scrolled back to the top when + // the query actually changes instead of one every re-creation event, but sadly + // that doesn't seem possible. binding.searchRecycler.scrollToPosition(0) }