summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt')
-rw-r--r--src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt
index 1d0846b08..5a35b14c9 100644
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GamesViewModel.kt
@@ -7,11 +7,16 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
+import androidx.preference.PreferenceManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
+import kotlinx.serialization.decodeFromString
+import kotlinx.serialization.json.Json
import org.yuzu.yuzu_emu.NativeLibrary
+import org.yuzu.yuzu_emu.YuzuApplication
import org.yuzu.yuzu_emu.utils.GameHelper
+import java.util.Locale
class GamesViewModel : ViewModel() {
private val _games = MutableLiveData<List<Game>>(emptyList())
@@ -33,9 +38,30 @@ class GamesViewModel : ViewModel() {
val searchFocused: LiveData<Boolean> get() = _searchFocused
init {
+ // Retrieve list of cached games
+ val storedGames = PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext)
+ .getStringSet(GameHelper.KEY_GAMES, emptySet())
+ if (storedGames!!.isNotEmpty()) {
+ val deserializedGames = mutableSetOf<Game>()
+ storedGames.forEach {
+ deserializedGames.add(Json.decodeFromString(it))
+ }
+ setGames(deserializedGames.toList())
+ }
reloadGames(false)
}
+ fun setGames(games: List<Game>) {
+ val sortedList = games.sortedWith(
+ compareBy(
+ { it.title.lowercase(Locale.getDefault()) },
+ { it.path }
+ )
+ )
+
+ _games.postValue(sortedList)
+ }
+
fun setSearchedGames(games: List<Game>) {
_searchedGames.postValue(games)
}
@@ -60,7 +86,7 @@ class GamesViewModel : ViewModel() {
viewModelScope.launch {
withContext(Dispatchers.IO) {
NativeLibrary.resetRomMetadata()
- _games.postValue(GameHelper.getGames())
+ setGames(GameHelper.getGames())
_isReloading.postValue(false)
if (directoryChanged) {