android: Simplify setup in search and games fragments
This commit is contained in:
parent
aa8a48e94c
commit
412c95e0b0
2 changed files with 62 additions and 57 deletions
|
@ -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,8 +80,16 @@ class SearchFragment : Fragment() {
|
|||
filterAndSearch()
|
||||
}
|
||||
|
||||
gamesViewModel.games.observe(viewLifecycleOwner) { filterAndSearch() }
|
||||
gamesViewModel.searchedGames.observe(viewLifecycleOwner) {
|
||||
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
|
||||
|
@ -96,6 +97,7 @@ class SearchFragment : Fragment() {
|
|||
binding.noResultsView.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
binding.clearButton.setOnClickListener { binding.searchText.setText("") }
|
||||
|
||||
|
|
|
@ -55,30 +55,41 @@ class GamesFragment : Fragment() {
|
|||
adapter = GameAdapter(requireActivity() as AppCompatActivity)
|
||||
}
|
||||
|
||||
binding.swipeRefresh.apply {
|
||||
// Add swipe down to refresh gesture
|
||||
binding.swipeRefresh.setOnRefreshListener {
|
||||
setOnRefreshListener {
|
||||
gamesViewModel.reloadGames(false)
|
||||
}
|
||||
|
||||
// Set theme color to the refresh animation's background
|
||||
binding.swipeRefresh.setProgressBackgroundColorSchemeColor(
|
||||
setProgressBackgroundColorSchemeColor(
|
||||
MaterialColors.getColor(
|
||||
binding.swipeRefresh,
|
||||
com.google.android.material.R.attr.colorPrimary
|
||||
)
|
||||
)
|
||||
binding.swipeRefresh.setColorSchemeColors(
|
||||
setColorSchemeColors(
|
||||
MaterialColors.getColor(
|
||||
binding.swipeRefresh,
|
||||
com.google.android.material.R.attr.colorOnPrimary
|
||||
)
|
||||
)
|
||||
|
||||
// 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.apply {
|
||||
// Watch for when we get updates to any of our games lists
|
||||
gamesViewModel.isReloading.observe(viewLifecycleOwner) { isReloading ->
|
||||
isReloading.observe(viewLifecycleOwner) { isReloading ->
|
||||
binding.swipeRefresh.isRefreshing = isReloading
|
||||
}
|
||||
gamesViewModel.games.observe(viewLifecycleOwner) {
|
||||
games.observe(viewLifecycleOwner) {
|
||||
(binding.gridGames.adapter as GameAdapter).submitList(it)
|
||||
if (it.isEmpty()) {
|
||||
binding.noticeText.visibility = View.VISIBLE
|
||||
|
@ -86,8 +97,7 @@ class GamesFragment : Fragment() {
|
|||
binding.noticeText.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
gamesViewModel.shouldSwapData.observe(viewLifecycleOwner) { shouldSwapData ->
|
||||
shouldSwapData.observe(viewLifecycleOwner) { shouldSwapData ->
|
||||
if (shouldSwapData) {
|
||||
(binding.gridGames.adapter as GameAdapter).submitList(gamesViewModel.games.value!!)
|
||||
gamesViewModel.setShouldSwapData(false)
|
||||
|
@ -95,22 +105,15 @@ class GamesFragment : Fragment() {
|
|||
}
|
||||
|
||||
// Check if the user reselected the games menu item and then scroll to top of the list
|
||||
gamesViewModel.shouldScrollToTop.observe(viewLifecycleOwner) { shouldScroll ->
|
||||
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() {
|
||||
|
|
Loading…
Reference in a new issue