diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index e525ddc03f..e270a33441 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -337,6 +337,8 @@ class MainActivity : ComponentActivity() { displayManager = getSystemService(DISPLAY_SERVICE) as DisplayManager refreshPhantomStatus() theme = SessionPrefs.theme(this) + // Last start's game list, so the Steam wall is up on the first frame; refresh() replaces it. + steamGames = com.droiddeck.launcher.frontend.LibraryCache.load(this) backActionsInverted = SessionPrefs.backActionsInverted(this) launcherFullscreen = SessionPrefs.launcherFullscreen(this) storeEnabled = SessionPrefs.storeEnabled(this) @@ -1068,9 +1070,12 @@ class MainActivity : ComponentActivity() { } /** The added games as the settings page lists them; a scan of the folder, on this thread (one level, small). */ + /** The added games for the session settings: a folder walk, so off the main thread. */ private fun refreshAddedGames() { - addedGames = scanAddedGames() - fetchAddedGameArt() + Thread({ + val games = scanAddedGames() + ui.post { addedGames = games; fetchAddedGameArt() } + }, "added-games").start() } /** Walks the added-games folders, which can sit on slow shared storage or an SD card. */ @@ -1195,6 +1200,9 @@ class MainActivity : ComponentActivity() { } else null // The libraries, off the main thread: manifests and a folder scan. Thread({ + // One update with the whole list: the wall places games by their position in it, so a + // partial list first would shuffle every capsule when the rest arrived. LibraryCache + // covers the wait. val games = if (ready) Library.steamGames(this) + com.droiddeck.launcher.frontend.AddedGames.scan(this).map { g -> com.droiddeck.launcher.frontend.AddedGameArt.resolve(this, g).let { art -> Library.SteamGame( @@ -1205,7 +1213,9 @@ class MainActivity : ComponentActivity() { } } else emptyList() val emus = Library.emulators(this) { id -> DesktopCatalog.installed(this, id) != null } - ui.post { steamGames = games.distinctBy { it.gameId }; emulatorList = emus } + val all = games.distinctBy { it.gameId } + if (ready) com.droiddeck.launcher.frontend.LibraryCache.save(this, all) + ui.post { steamGames = all; emulatorList = emus } // Box art for the games that have none, fetched after the list is up; the list is // rebuilt once if any was found. if (!OfflineMode.enabled(this) && CoverArt.fetchMissing(this, emus.flatMap { it.games })) { diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt new file mode 100644 index 0000000000..afe842d7ce --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt @@ -0,0 +1,49 @@ +package com.droiddeck.launcher.frontend + +import android.content.Context +import android.util.Log +import org.json.JSONArray +import org.json.JSONObject +import java.io.File + +/** + * The last game list the front end showed, kept in the app's files so the next start draws the + * Steam wall on its first frame. Building the list reads every manifest and walks the added-games + * folders, which can take a second or more; the fresh list replaces this one when it is ready. + */ +object LibraryCache { + private const val TAG = "LibraryCache" + private fun file(context: Context) = File(context.filesDir, "library-cache.json") + + fun load(context: Context): List = runCatching { + val f = file(context) + if (!f.isFile) return emptyList() + val array = JSONArray(f.readText()) + List(array.length()) { i -> + val o = array.getJSONObject(i) + Library.SteamGame( + appId = o.getInt("appId"), name = o.getString("name"), art = o.file("art"), + library = o.getString("library"), gameId = o.getLong("gameId"), hero = o.file("hero"), + lastPlayed = o.optLong("lastPlayed"), gameFiles = o.file("gameFiles"), protonPrefix = o.file("protonPrefix"), + ) + } + }.getOrElse { e -> Log.w(TAG, "unreadable, ignored: ${e.message}"); emptyList() } + + fun save(context: Context, games: List) { + runCatching { + val array = JSONArray() + for (g in games) array.put( + JSONObject().put("appId", g.appId).put("name", g.name).put("library", g.library).put("gameId", g.gameId) + .put("lastPlayed", g.lastPlayed).putFile("art", g.art).putFile("hero", g.hero) + .putFile("gameFiles", g.gameFiles).putFile("protonPrefix", g.protonPrefix), + ) + val f = file(context) + val tmp = File(f.path + ".tmp") + tmp.writeText(array.toString()) + tmp.renameTo(f) + }.onFailure { e -> Log.w(TAG, "not saved: ${e.message}") } + } + + private fun JSONObject.file(key: String): File? = optString(key).takeIf { it.isNotEmpty() }?.let(::File) + private fun JSONObject.putFile(key: String, f: File?): JSONObject = if (f == null) this else put(key, f.path) +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SteamHome.kt b/app/src/main/java/com/droiddeck/launcher/ui/SteamHome.kt index 0f4f97c339..832ea304d0 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SteamHome.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SteamHome.kt @@ -59,7 +59,9 @@ private const val WALL_MIN_GAMES = 4 */ @Composable internal fun SteamHome(s: FrontEndState, a: FrontEndActions, modifier: Modifier) { - val games = remember(s.steamGames) { s.steamGames.sortedByDescending { it.lastPlayed } } + // A fixed order, ties included: the wall places each game by its position, so an order that + // came out differently from one read of the library to the next moved every capsule. + val games = remember(s.steamGames) { s.steamGames.sortedWith(compareByDescending { it.lastPlayed }.thenBy { it.gameId }) } val narrow = LocalNarrowPane.current val colors = MaterialTheme.colorScheme val pal = LocalPalette.current