From 412c95e0b0515aa0211c7e7612ce237c23a7938e Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Mon, 22 May 2023 16:20:51 -0400 Subject: [PATCH] android: Simplify setup in search and games fragments --- .../yuzu/yuzu_emu/fragments/SearchFragment.kt | 30 ++++--- .../org/yuzu/yuzu_emu/ui/GamesFragment.kt | 89 ++++++++++--------- 2 files changed, 62 insertions(+), 57 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SearchFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SearchFragment.kt index f6aa370f07..eb4f513d1c 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SearchFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SearchFragment.kt @@ -61,13 +61,6 @@ class SearchFragment : Fragment() { binding.searchText.setText(savedInstanceState.getString(SEARCH_TEXT)) } - gamesViewModel.searchFocused.observe(viewLifecycleOwner) { searchFocused -> - if (searchFocused) { - focusSearch() - gamesViewModel.setSearchFocused(false) - } - } - binding.gridGamesSearch.apply { layoutManager = AutofitGridLayoutManager( requireContext(), @@ -87,13 +80,22 @@ class SearchFragment : Fragment() { filterAndSearch() } - gamesViewModel.games.observe(viewLifecycleOwner) { filterAndSearch() } - gamesViewModel.searchedGames.observe(viewLifecycleOwner) { - (binding.gridGamesSearch.adapter as GameAdapter).submitList(it) - if (it.isEmpty()) { - binding.noResultsView.visibility = View.VISIBLE - } else { - binding.noResultsView.visibility = View.GONE + gamesViewModel.apply { + searchFocused.observe(viewLifecycleOwner) { searchFocused -> + if (searchFocused) { + focusSearch() + gamesViewModel.setSearchFocused(false) + } + } + + games.observe(viewLifecycleOwner) { filterAndSearch() } + searchedGames.observe(viewLifecycleOwner) { + (binding.gridGamesSearch.adapter as GameAdapter).submitList(it) + if (it.isEmpty()) { + binding.noResultsView.visibility = View.VISIBLE + } else { + binding.noResultsView.visibility = View.GONE + } } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/GamesFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/GamesFragment.kt index afabfb2b92..97eef40d20 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/GamesFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/GamesFragment.kt @@ -55,62 +55,65 @@ class GamesFragment : Fragment() { adapter = GameAdapter(requireActivity() as AppCompatActivity) } - // Add swipe down to refresh gesture - binding.swipeRefresh.setOnRefreshListener { - gamesViewModel.reloadGames(false) - } + binding.swipeRefresh.apply { + // Add swipe down to refresh gesture + setOnRefreshListener { + gamesViewModel.reloadGames(false) + } - // Set theme color to the refresh animation's background - binding.swipeRefresh.setProgressBackgroundColorSchemeColor( - MaterialColors.getColor( - binding.swipeRefresh, - com.google.android.material.R.attr.colorPrimary + // Set theme color to the refresh animation's background + setProgressBackgroundColorSchemeColor( + MaterialColors.getColor( + binding.swipeRefresh, + com.google.android.material.R.attr.colorPrimary + ) ) - ) - binding.swipeRefresh.setColorSchemeColors( - MaterialColors.getColor( - binding.swipeRefresh, - com.google.android.material.R.attr.colorOnPrimary + setColorSchemeColors( + MaterialColors.getColor( + binding.swipeRefresh, + com.google.android.material.R.attr.colorOnPrimary + ) ) - ) - // Watch for when we get updates to any of our games lists - gamesViewModel.isReloading.observe(viewLifecycleOwner) { isReloading -> - binding.swipeRefresh.isRefreshing = isReloading - } - gamesViewModel.games.observe(viewLifecycleOwner) { - (binding.gridGames.adapter as GameAdapter).submitList(it) - if (it.isEmpty()) { - binding.noticeText.visibility = View.VISIBLE - } else { - binding.noticeText.visibility = View.GONE + // Make sure the loading indicator appears even if the layout is told to refresh before being fully drawn + post { + if (_binding == null) { + return@post + } + binding.swipeRefresh.isRefreshing = gamesViewModel.isReloading.value!! } } - gamesViewModel.shouldSwapData.observe(viewLifecycleOwner) { shouldSwapData -> - if (shouldSwapData) { - (binding.gridGames.adapter as GameAdapter).submitList(gamesViewModel.games.value!!) - gamesViewModel.setShouldSwapData(false) + gamesViewModel.apply { + // Watch for when we get updates to any of our games lists + isReloading.observe(viewLifecycleOwner) { isReloading -> + binding.swipeRefresh.isRefreshing = isReloading + } + games.observe(viewLifecycleOwner) { + (binding.gridGames.adapter as GameAdapter).submitList(it) + if (it.isEmpty()) { + binding.noticeText.visibility = View.VISIBLE + } else { + binding.noticeText.visibility = View.GONE + } + } + shouldSwapData.observe(viewLifecycleOwner) { shouldSwapData -> + if (shouldSwapData) { + (binding.gridGames.adapter as GameAdapter).submitList(gamesViewModel.games.value!!) + gamesViewModel.setShouldSwapData(false) + } } - } - // Check if the user reselected the games menu item and then scroll to top of the list - gamesViewModel.shouldScrollToTop.observe(viewLifecycleOwner) { shouldScroll -> - if (shouldScroll) { - scrollToTop() - gamesViewModel.setShouldScrollToTop(false) + // Check if the user reselected the games menu item and then scroll to top of the list + shouldScrollToTop.observe(viewLifecycleOwner) { shouldScroll -> + if (shouldScroll) { + scrollToTop() + gamesViewModel.setShouldScrollToTop(false) + } } } setInsets() - - // Make sure the loading indicator appears even if the layout is told to refresh before being fully drawn - binding.swipeRefresh.post { - if (_binding == null) { - return@post - } - binding.swipeRefresh.isRefreshing = gamesViewModel.isReloading.value!! - } } override fun onDestroyView() {