android: Scroll shortcut for games list

If you reselect the "Games" menu item in the bottom navigation menu, the list smoothly scrolls to the top.
This commit is contained in:
Charles Lombardo 2023-04-28 19:50:58 -04:00 committed by bunnei
parent 274b2be24f
commit 0d16805445
3 changed files with 34 additions and 1 deletions

View File

@ -26,6 +26,9 @@ class GamesViewModel : ViewModel() {
private val _shouldSwapData = MutableLiveData(false)
val shouldSwapData: LiveData<Boolean> get() = _shouldSwapData
private val _shouldScrollToTop = MutableLiveData(false)
val shouldScrollToTop: LiveData<Boolean> get() = _shouldScrollToTop
init {
reloadGames(false)
}
@ -38,6 +41,10 @@ class GamesViewModel : ViewModel() {
_shouldSwapData.postValue(shouldSwap)
}
fun setShouldScrollToTop(shouldScroll: Boolean) {
_shouldScrollToTop.postValue(shouldScroll)
}
fun reloadGames(directoryChanged: Boolean) {
if (isReloading.value == true)
return

View File

@ -138,6 +138,14 @@ class GamesFragment : Fragment() {
searchHidden()
}
// 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)
}
}
setInsets()
// Make sure the loading indicator appears even if the layout is told to refresh before being fully drawn
@ -191,6 +199,12 @@ class GamesFragment : Fragment() {
}
}
fun scrollToTop() {
if (_binding != null) {
binding.gridGames.smoothScrollToPosition(0)
}
}
private fun setInsets() =
ViewCompat.setOnApplyWindowInsetsListener(binding.gridGames) { view: View, windowInsets: WindowInsetsCompat ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())

View File

@ -25,6 +25,7 @@ import androidx.preference.PreferenceManager
import com.google.android.material.color.MaterialColors
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.elevation.ElevationOverlayProvider
import com.google.android.material.navigation.NavigationBarView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@ -73,6 +74,11 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
setUpNavigation(navHostFragment.navController)
(binding.navigationBar as NavigationBarView).setOnItemReselectedListener {
if (it.itemId == R.id.gamesFragment) {
gamesViewModel.setShouldScrollToTop(true)
}
}
binding.statusBarShade.setBackgroundColor(
MaterialColors.getColor(
@ -243,7 +249,13 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
)
val dstPath = DirectoryInitialization.userDirectory + "/keys/"
if (FileUtil.copyUriToInternalStorage(applicationContext, result, dstPath, "prod.keys")) {
if (FileUtil.copyUriToInternalStorage(
applicationContext,
result,
dstPath,
"prod.keys"
)
) {
if (NativeLibrary.reloadKeys()) {
Toast.makeText(
applicationContext,