android: Move game deserialization to another thread

Deserializing games from the cache in shared preferences was done on the main thread and could cause a stutter on startup.
This commit is contained in:
Charles Lombardo 2023-10-29 14:15:37 -04:00
parent 911d2216be
commit 2581590023
1 changed files with 23 additions and 16 deletions

View File

@ -49,6 +49,9 @@ class GamesViewModel : ViewModel() {
// Retrieve list of cached games // Retrieve list of cached games
val storedGames = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) val storedGames = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext)
.getStringSet(GameHelper.KEY_GAMES, emptySet()) .getStringSet(GameHelper.KEY_GAMES, emptySet())
viewModelScope.launch {
withContext(Dispatchers.IO) {
if (storedGames!!.isNotEmpty()) { if (storedGames!!.isNotEmpty()) {
val deserializedGames = mutableSetOf<Game>() val deserializedGames = mutableSetOf<Game>()
storedGames.forEach { storedGames.forEach {
@ -60,8 +63,10 @@ class GamesViewModel : ViewModel() {
} }
val gameExists = val gameExists =
DocumentFile.fromSingleUri(YuzuApplication.appContext, Uri.parse(game.path)) DocumentFile.fromSingleUri(
?.exists() YuzuApplication.appContext,
Uri.parse(game.path)
)?.exists()
if (gameExists == true) { if (gameExists == true) {
deserializedGames.add(game) deserializedGames.add(game)
} }
@ -70,6 +75,8 @@ class GamesViewModel : ViewModel() {
} }
reloadGames(false) reloadGames(false)
} }
}
}
fun setGames(games: List<Game>) { fun setGames(games: List<Game>) {
val sortedList = games.sortedWith( val sortedList = games.sortedWith(