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.
This commit is contained in:
OxygenCobalt 2022-01-08 09:45:14 -07:00
parent 049b279e1f
commit d4904136c0
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
2 changed files with 17 additions and 7 deletions

View file

@ -4,7 +4,7 @@ apply plugin: "kotlin-kapt"
apply plugin: "androidx.navigation.safeargs.kotlin" apply plugin: "androidx.navigation.safeargs.kotlin"
android { android {
compileSdkVersion 32 compileSdkVersion 31
buildToolsVersion "31.0.0" buildToolsVersion "31.0.0"
defaultConfig { defaultConfig {
@ -13,7 +13,7 @@ android {
versionCode 10 versionCode 10
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 32 targetSdkVersion 31
buildFeatures { buildFeatures {
dataBinding true dataBinding true

View file

@ -55,6 +55,9 @@ class SearchFragment : Fragment() {
private val playbackModel: PlaybackViewModel by activityViewModels() private val playbackModel: PlaybackViewModel by activityViewModels()
private val detailModel: DetailViewModel by activityViewModels() private val detailModel: DetailViewModel by activityViewModels()
private var launchedKeyboard = false
private var mustScrollUp = false
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
@ -103,16 +106,21 @@ class SearchFragment : Fragment() {
binding.searchEditText.apply { binding.searchEditText.apply {
addTextChangedListener { text -> addTextChangedListener { text ->
mustScrollUp = true
// Run the search with the updated text as the query // Run the search with the updated text as the query
searchModel.search(text?.toString() ?: "") searchModel.search(text?.toString() ?: "")
} }
if (!launchedKeyboard) {
// Auto-open the keyboard when this view is shown // Auto-open the keyboard when this view is shown
requestFocus() requestFocus()
postDelayed(200) { postDelayed(200) {
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
} }
launchedKeyboard = true
}
} }
binding.searchRecycler.apply { binding.searchRecycler.apply {
@ -127,7 +135,9 @@ class SearchFragment : Fragment() {
searchModel.searchResults.observe(viewLifecycleOwner) { results -> searchModel.searchResults.observe(viewLifecycleOwner) { results ->
searchAdapter.submitList(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) binding.searchRecycler.scrollToPosition(0)
} }