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"
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

View file

@ -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)
}