|
|
@ -55,62 +55,65 @@ class GamesFragment : Fragment() {
|
|
|
|
adapter = GameAdapter(requireActivity() as AppCompatActivity)
|
|
|
|
adapter = GameAdapter(requireActivity() as AppCompatActivity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add swipe down to refresh gesture
|
|
|
|
binding.swipeRefresh.apply {
|
|
|
|
binding.swipeRefresh.setOnRefreshListener {
|
|
|
|
// Add swipe down to refresh gesture
|
|
|
|
gamesViewModel.reloadGames(false)
|
|
|
|
setOnRefreshListener {
|
|
|
|
}
|
|
|
|
gamesViewModel.reloadGames(false)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set theme color to the refresh animation's background
|
|
|
|
// Set theme color to the refresh animation's background
|
|
|
|
binding.swipeRefresh.setProgressBackgroundColorSchemeColor(
|
|
|
|
setProgressBackgroundColorSchemeColor(
|
|
|
|
MaterialColors.getColor(
|
|
|
|
MaterialColors.getColor(
|
|
|
|
binding.swipeRefresh,
|
|
|
|
binding.swipeRefresh,
|
|
|
|
com.google.android.material.R.attr.colorPrimary
|
|
|
|
com.google.android.material.R.attr.colorPrimary
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
setColorSchemeColors(
|
|
|
|
binding.swipeRefresh.setColorSchemeColors(
|
|
|
|
MaterialColors.getColor(
|
|
|
|
MaterialColors.getColor(
|
|
|
|
binding.swipeRefresh,
|
|
|
|
binding.swipeRefresh,
|
|
|
|
com.google.android.material.R.attr.colorOnPrimary
|
|
|
|
com.google.android.material.R.attr.colorOnPrimary
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Watch for when we get updates to any of our games lists
|
|
|
|
// Make sure the loading indicator appears even if the layout is told to refresh before being fully drawn
|
|
|
|
gamesViewModel.isReloading.observe(viewLifecycleOwner) { isReloading ->
|
|
|
|
post {
|
|
|
|
binding.swipeRefresh.isRefreshing = isReloading
|
|
|
|
if (_binding == null) {
|
|
|
|
}
|
|
|
|
return@post
|
|
|
|
gamesViewModel.games.observe(viewLifecycleOwner) {
|
|
|
|
}
|
|
|
|
(binding.gridGames.adapter as GameAdapter).submitList(it)
|
|
|
|
binding.swipeRefresh.isRefreshing = gamesViewModel.isReloading.value!!
|
|
|
|
if (it.isEmpty()) {
|
|
|
|
|
|
|
|
binding.noticeText.visibility = View.VISIBLE
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
binding.noticeText.visibility = View.GONE
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gamesViewModel.shouldSwapData.observe(viewLifecycleOwner) { shouldSwapData ->
|
|
|
|
gamesViewModel.apply {
|
|
|
|
if (shouldSwapData) {
|
|
|
|
// Watch for when we get updates to any of our games lists
|
|
|
|
(binding.gridGames.adapter as GameAdapter).submitList(gamesViewModel.games.value!!)
|
|
|
|
isReloading.observe(viewLifecycleOwner) { isReloading ->
|
|
|
|
gamesViewModel.setShouldSwapData(false)
|
|
|
|
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
|
|
|
|
// 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) {
|
|
|
|
if (shouldScroll) {
|
|
|
|
scrollToTop()
|
|
|
|
scrollToTop()
|
|
|
|
gamesViewModel.setShouldScrollToTop(false)
|
|
|
|
gamesViewModel.setShouldScrollToTop(false)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setInsets()
|
|
|
|
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() {
|
|
|
|
override fun onDestroyView() {
|
|
|
|