VERY experimental Nintendo Switch 2 emulator
C++
android: Game data cache
Charles Lombardo710e19a4bbfc
diff --git a/src/android/app/build.gradle.kts b/src/android/app/build.gradle.ktsindex ea13b6d0ec..a9790b2004 100644--- a/src/android/app/build.gradle.kts+++ b/src/android/app/build.gradle.kts@@ -2,6 +2,7 @@ plugins {id("com.android.application")id("org.jetbrains.kotlin.android")id("kotlin-parcelize")+ kotlin("plugin.serialization") version "1.8.21"}/**@@ -164,6 +165,7 @@ dependencies {implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")implementation("androidx.navigation:navigation-ui-ktx:2.5.3")implementation("info.debatty:java-string-similarity:2.0.0")+ implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")}fun getVersion(): String {diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.ktindex b9f975e2b5..a9653475f2 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt@@ -100,7 +100,6 @@ class GameAdapter(private val activity: AppCompatActivity) :return oldItem.gameId == newItem.gameId}- @SuppressLint("DiffUtilEquals")override fun areContentsTheSame(oldItem: Game, newItem: Game): Boolean {return oldItem == newItem}diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Game.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Game.ktindex c5cde9d059..2a17653b2f 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Game.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/Game.kt@@ -5,9 +5,11 @@ package org.yuzu.yuzu_emu.modelimport android.os.Parcelableimport kotlinx.parcelize.Parcelize+import kotlinx.serialization.Serializableimport java.util.HashSet@Parcelize+@Serializableclass Game(val title: String,val description: String,@@ -19,6 +21,18 @@ class Game(val keyAddedToLibraryTime get() = "${gameId}_AddedToLibraryTime"val keyLastPlayedTime get() = "${gameId}_LastPlayed"+ override fun equals(other: Any?): Boolean {+ if (other !is Game)+ return false++ return title == other.title+ && description == other.description+ && regions == other.regions+ && path == other.path+ && gameId == other.gameId+ && company == other.company+ }+companion object {val extensions: Set<String> = HashSet(listOf(".xci", ".nsp", ".nca", ".nro")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.ktindex 1d0846b080..5a35b14c98 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.LiveDataimport androidx.lifecycle.MutableLiveDataimport androidx.lifecycle.ViewModelimport androidx.lifecycle.viewModelScope+import androidx.preference.PreferenceManagerimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.launchimport kotlinx.coroutines.withContext+import kotlinx.serialization.decodeFromString+import kotlinx.serialization.json.Jsonimport org.yuzu.yuzu_emu.NativeLibrary+import org.yuzu.yuzu_emu.YuzuApplicationimport org.yuzu.yuzu_emu.utils.GameHelper+import java.util.Localeclass GamesViewModel : ViewModel() {private val _games = MutableLiveData<List<Game>>(emptyList())@@ -33,9 +38,30 @@ class GamesViewModel : ViewModel() {val searchFocused: LiveData<Boolean> get() = _searchFocusedinit {+ // 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) {diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/GamesFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/GamesFragment.ktindex 07d0cd3d8d..afabfb2b92 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/GamesFragment.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/GamesFragment.kt@@ -20,10 +20,8 @@ import org.yuzu.yuzu_emu.Rimport org.yuzu.yuzu_emu.adapters.GameAdapterimport org.yuzu.yuzu_emu.databinding.FragmentGamesBindingimport org.yuzu.yuzu_emu.layout.AutofitGridLayoutManager-import org.yuzu.yuzu_emu.model.Gameimport org.yuzu.yuzu_emu.model.GamesViewModelimport org.yuzu.yuzu_emu.model.HomeViewModel-import java.util.Localeclass GamesFragment : Fragment() {private var _binding: FragmentGamesBinding? = null@@ -81,7 +79,7 @@ class GamesFragment : Fragment() {binding.swipeRefresh.isRefreshing = isReloading}gamesViewModel.games.observe(viewLifecycleOwner) {- submitGamesList(it)+ (binding.gridGames.adapter as GameAdapter).submitList(it)if (it.isEmpty()) {binding.noticeText.visibility = View.VISIBLE} else {@@ -91,7 +89,7 @@ class GamesFragment : Fragment() {gamesViewModel.shouldSwapData.observe(viewLifecycleOwner) { shouldSwapData ->if (shouldSwapData) {- submitGamesList(gamesViewModel.games.value!!)+ (binding.gridGames.adapter as GameAdapter).submitList(gamesViewModel.games.value!!)gamesViewModel.setShouldSwapData(false)}}@@ -115,11 +113,6 @@ class GamesFragment : Fragment() {}}- private fun submitGamesList(gameList: List<Game>) {- val sortedList = gameList.sortedBy { it.title.lowercase(Locale.getDefault()) }- (binding.gridGames.adapter as GameAdapter).submitList(sortedList)- }-override fun onDestroyView() {super.onDestroyView()_binding = nulldiff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.ktindex b2499168e0..f8f275b41e 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt@@ -25,7 +25,6 @@ import androidx.navigation.ui.setupWithNavControllerimport androidx.preference.PreferenceManagerimport com.google.android.material.color.MaterialColorsimport com.google.android.material.dialog.MaterialAlertDialogBuilder-import com.google.android.material.elevation.ElevationOverlayProviderimport com.google.android.material.navigation.NavigationBarViewimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.launchdiff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameHelper.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameHelper.ktindex 9dd43343f6..ba6b5783ec 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameHelper.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameHelper.kt@@ -6,19 +6,22 @@ package org.yuzu.yuzu_emu.utilsimport android.content.SharedPreferencesimport android.net.Uriimport androidx.preference.PreferenceManager+import kotlinx.serialization.decodeFromString+import kotlinx.serialization.encodeToString+import kotlinx.serialization.json.Jsonimport org.yuzu.yuzu_emu.NativeLibraryimport org.yuzu.yuzu_emu.YuzuApplicationimport org.yuzu.yuzu_emu.model.Gameimport java.util.*-import kotlin.collections.ArrayListobject GameHelper {const val KEY_GAME_PATH = "game_path"+ const val KEY_GAMES = "Games"private lateinit var preferences: SharedPreferences- fun getGames(): ArrayList<Game> {- val games = ArrayList<Game>()+ fun getGames(): List<Game> {+ val games = mutableListOf<Game>()val context = YuzuApplication.appContextval gamesDir =PreferenceManager.getDefaultSharedPreferences(context).getString(KEY_GAME_PATH, "")@@ -44,7 +47,17 @@ object GameHelper {}}- return games+ // Cache list of games found on disk+ val serializedGames = mutableSetOf<String>()+ games.forEach {+ serializedGames.add(Json.encodeToString(it))+ }+ preferences.edit()+ .remove(KEY_GAMES)+ .putStringSet(KEY_GAMES, serializedGames)+ .apply()++ return games.toList()}private fun getGame(filePath: String): Game {diff --git a/src/android/app/src/main/res/layout/fragment_games.xml b/src/android/app/src/main/res/layout/fragment_games.xmlindex 8b6d0b3b63..a0568668a2 100644--- a/src/android/app/src/main/res/layout/fragment_games.xml+++ b/src/android/app/src/main/res/layout/fragment_games.xml@@ -20,7 +20,7 @@android:gravity="center"android:padding="@dimen/spacing_large"android:text="@string/empty_gamelist"- tools:visibility="gone" />+ android:visibility="gone" /><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/grid_games"