From 8675f25668cc6b23916e50c1ee80437b852a7a39 Mon Sep 17 00:00:00 2001 From: Nite Date: Sat, 19 Feb 2022 13:54:25 +0100 Subject: [PATCH] Fixed Search and Edit Server to hide the keyboard when navigating away --- .../ultrasonic/fragment/EditServerFragment.kt | 38 ++++++++++--------- .../ultrasonic/fragment/SearchFragment.kt | 38 ++++++++++--------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/EditServerFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/EditServerFragment.kt index f0128e62..84de4a97 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/EditServerFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/EditServerFragment.kt @@ -110,25 +110,24 @@ class EditServerFragment : Fragment(), OnBackPressedHandler { FragmentTitle.setTitle(this, R.string.server_editor_label) val serverSetting = serverSettingsModel.getServerSetting(index) serverSetting.observe( - viewLifecycleOwner, - { t -> - if (t != null) { - currentServerSetting = t - if (!isInstanceStateSaved) setFields() - // Remove the minimum API version so it can be detected again - if (currentServerSetting?.minimumApiVersion != null) { - currentServerSetting!!.minimumApiVersion = null - serverSettingsModel.updateItem(currentServerSetting) - if ( - activeServerProvider.getActiveServer().id == - currentServerSetting!!.id - ) { - MusicServiceFactory.resetMusicService() - } + viewLifecycleOwner + ) { t -> + if (t != null) { + currentServerSetting = t + if (!isInstanceStateSaved) setFields() + // Remove the minimum API version so it can be detected again + if (currentServerSetting?.minimumApiVersion != null) { + currentServerSetting!!.minimumApiVersion = null + serverSettingsModel.updateItem(currentServerSetting) + if ( + activeServerProvider.getActiveServer().id == + currentServerSetting!!.id + ) { + MusicServiceFactory.resetMusicService() } } } - ) + } saveButton!!.setOnClickListener { if (currentServerSetting != null) { if (getFields()) { @@ -187,6 +186,11 @@ class EditServerFragment : Fragment(), OnBackPressedHandler { } } + override fun onStop() { + Util.hideKeyboard(activity) + super.onStop() + } + private fun updateColor(color: Int?) { val image = ContextCompat.getDrawable(requireContext(), R.drawable.thumb_drawable) currentColor = ServerColor.getBackgroundColor(requireContext(), color) @@ -511,7 +515,6 @@ class EditServerFragment : Fragment(), OnBackPressedHandler { .setMessage(R.string.server_editor_leave_confirmation) .setPositiveButton(R.string.common_ok) { dialog, _ -> dialog.dismiss() - Util.hideKeyboard(activity) findNavController().navigateUp() } .setNegativeButton(R.string.common_cancel) { dialog, _ -> @@ -519,7 +522,6 @@ class EditServerFragment : Fragment(), OnBackPressedHandler { } .show() } else { - Util.hideKeyboard(activity) findNavController().navigateUp() } } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SearchFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SearchFragment.kt index 8acf907d..4be5be3f 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SearchFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/SearchFragment.kt @@ -41,6 +41,7 @@ import org.moire.ultrasonic.util.CancellationToken import org.moire.ultrasonic.util.CommunicationError import org.moire.ultrasonic.util.Constants import org.moire.ultrasonic.util.Settings +import org.moire.ultrasonic.util.Util import org.moire.ultrasonic.util.Util.toast import timber.log.Timber @@ -50,6 +51,7 @@ import timber.log.Timber class SearchFragment : MultiListFragment(), KoinComponent { private var searchResult: SearchResult? = null private var searchRefresh: SwipeRefreshLayout? = null + private var searchView: SearchView? = null private val mediaPlayerController: MediaPlayerController by inject() @@ -69,15 +71,14 @@ class SearchFragment : MultiListFragment(), KoinComponent { setHasOptionsMenu(true) listModel.searchResult.observe( - viewLifecycleOwner, - { - if (it != null) { - // Shorten the display initially - searchResult = it - populateList(listModel.trimResultLength(it)) - } + viewLifecycleOwner + ) { + if (it != null) { + // Shorten the display initially + searchResult = it + populateList(listModel.trimResultLength(it)) } - ) + } searchRefresh = view.findViewById(R.id.swipe_refresh_view) searchRefresh!!.isEnabled = false @@ -143,9 +144,9 @@ class SearchFragment : MultiListFragment(), KoinComponent { val searchManager = activity.getSystemService(Context.SEARCH_SERVICE) as SearchManager inflater.inflate(R.menu.search, menu) val searchItem = menu.findItem(R.id.search_item) - val searchView = searchItem.actionView as SearchView + searchView = searchItem.actionView as SearchView val searchableInfo = searchManager.getSearchableInfo(requireActivity().componentName) - searchView.setSearchableInfo(searchableInfo) + searchView!!.setSearchableInfo(searchableInfo) val arguments = arguments val autoPlay = arguments != null && @@ -154,31 +155,31 @@ class SearchFragment : MultiListFragment(), KoinComponent { // If started with a query, enter it to the searchView if (query != null) { - searchView.setQuery(query, false) - searchView.clearFocus() + searchView!!.setQuery(query, false) + searchView!!.clearFocus() } - searchView.setOnSuggestionListener(object : SearchView.OnSuggestionListener { + searchView!!.setOnSuggestionListener(object : SearchView.OnSuggestionListener { override fun onSuggestionSelect(position: Int): Boolean { return true } override fun onSuggestionClick(position: Int): Boolean { Timber.d("onSuggestionClick: %d", position) - val cursor = searchView.suggestionsAdapter.cursor + val cursor = searchView!!.suggestionsAdapter.cursor cursor.moveToPosition(position) // 2 is the index of col containing suggestion name. val suggestion = cursor.getString(2) - searchView.setQuery(suggestion, true) + searchView!!.setQuery(suggestion, true) return true } }) - searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { + searchView!!.setOnQueryTextListener(object : SearchView.OnQueryTextListener { override fun onQueryTextSubmit(query: String): Boolean { Timber.d("onQueryTextSubmit: %s", query) - searchView.clearFocus() + searchView!!.clearFocus() search(query, autoPlay) return true } @@ -188,11 +189,12 @@ class SearchFragment : MultiListFragment(), KoinComponent { } }) - searchView.setIconifiedByDefault(false) + searchView!!.setIconifiedByDefault(false) searchItem.expandActionView() } override fun onDestroyView() { + Util.hideKeyboard(activity) cancellationToken?.cancel() super.onDestroyView() }