|
|
@ -7,11 +7,16 @@ import androidx.lifecycle.LiveData |
|
|
import androidx.lifecycle.MutableLiveData |
|
|
import androidx.lifecycle.MutableLiveData |
|
|
import androidx.lifecycle.ViewModel |
|
|
import androidx.lifecycle.ViewModel |
|
|
import androidx.lifecycle.viewModelScope |
|
|
import androidx.lifecycle.viewModelScope |
|
|
|
|
|
import androidx.preference.PreferenceManager |
|
|
import kotlinx.coroutines.Dispatchers |
|
|
import kotlinx.coroutines.Dispatchers |
|
|
import kotlinx.coroutines.launch |
|
|
import kotlinx.coroutines.launch |
|
|
import kotlinx.coroutines.withContext |
|
|
import kotlinx.coroutines.withContext |
|
|
|
|
|
import kotlinx.serialization.decodeFromString |
|
|
|
|
|
import kotlinx.serialization.json.Json |
|
|
import org.yuzu.yuzu_emu.NativeLibrary |
|
|
import org.yuzu.yuzu_emu.NativeLibrary |
|
|
|
|
|
import org.yuzu.yuzu_emu.YuzuApplication |
|
|
import org.yuzu.yuzu_emu.utils.GameHelper |
|
|
import org.yuzu.yuzu_emu.utils.GameHelper |
|
|
|
|
|
import java.util.Locale |
|
|
|
|
|
|
|
|
class GamesViewModel : ViewModel() { |
|
|
class GamesViewModel : ViewModel() { |
|
|
private val _games = MutableLiveData<List<Game>>(emptyList()) |
|
|
private val _games = MutableLiveData<List<Game>>(emptyList()) |
|
|
@ -33,9 +38,30 @@ class GamesViewModel : ViewModel() { |
|
|
val searchFocused: LiveData<Boolean> get() = _searchFocused |
|
|
val searchFocused: LiveData<Boolean> get() = _searchFocused |
|
|
|
|
|
|
|
|
init { |
|
|
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) |
|
|
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>) { |
|
|
fun setSearchedGames(games: List<Game>) { |
|
|
_searchedGames.postValue(games) |
|
|
_searchedGames.postValue(games) |
|
|
} |
|
|
} |
|
|
@ -60,7 +86,7 @@ class GamesViewModel : ViewModel() { |
|
|
viewModelScope.launch { |
|
|
viewModelScope.launch { |
|
|
withContext(Dispatchers.IO) { |
|
|
withContext(Dispatchers.IO) { |
|
|
NativeLibrary.resetRomMetadata() |
|
|
NativeLibrary.resetRomMetadata() |
|
|
_games.postValue(GameHelper.getGames()) |
|
|
|
|
|
|
|
|
setGames(GameHelper.getGames()) |
|
|
_isReloading.postValue(false) |
|
|
_isReloading.postValue(false) |
|
|
|
|
|
|
|
|
if (directoryChanged) { |
|
|
if (directoryChanged) { |
|
|
|