From 50fbcf8b2a34f82b126448d573a35a8f77f0b795 Mon Sep 17 00:00:00 2001 From: FX1922 <120828496+FX192@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:08:27 +0300 Subject: [PATCH] feat(library): add a Favorites tab and a heart button on the game page (#2613) * Add a Favorites tab to the Library with a heart on the game page * Fix favorites test helper call --- .../launcher/session/SessionPrefs.kt | 6 +++ .../com/droiddeck/launcher/ui/GameDetail.kt | 20 ++++++- .../droiddeck/launcher/ui/LibraryFavorites.kt | 35 ++++++++++++ .../com/droiddeck/launcher/ui/LibraryPage.kt | 28 +++++++--- .../launcher/ui/LibraryFavoritesTest.kt | 53 +++++++++++++++++++ 5 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/LibraryFavorites.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/LibraryFavoritesTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index ae64425b77..e6a8befb90 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -277,6 +277,12 @@ object SessionPrefs { prefs(context).edit().putBoolean("libraryShowUninstalled", show).apply() } + fun libraryFavorites(context: Context): Set = prefs(context).getStringSet("libraryFavorites", null)?.toSet().orEmpty() + + fun setLibraryFavorites(context: Context, keys: Set) { + prefs(context).edit().putStringSet("libraryFavorites", keys.toSet()).apply() + } + fun storesTabOrder(context: Context): List = idList(prefs(context).getString("storesTabOrder", null)) fun setStoresTabOrder(context: Context, order: List) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt index 7aba7feb45..d692af4f2e 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt @@ -35,6 +35,8 @@ import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Favorite +import androidx.compose.material.icons.filled.FavoriteBorder import androidx.compose.material.icons.filled.PlayArrow import androidx.compose.material.icons.outlined.Delete import androidx.compose.material.icons.outlined.Edit @@ -165,6 +167,7 @@ internal fun GameDetail( if (nothing) return val source = game?.source ?: store?.id ?: Library.SOURCE_STEAM val title = game?.name ?: item?.title ?: installed?.sidecar?.title ?: id.orEmpty() + val favoriteKey = if (store != null && id != null) "${store.id}:$id" else game?.let { GameRef.key(it) } ?: key val scores = if (source != Library.ADDED) rememberGameScores(title.takeIf { it.isNotBlank() && it != id }) else null val achievements = remember(store, id, item?.accessible, installed) { when { @@ -225,6 +228,7 @@ internal fun GameDetail( Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { if (installed != null && installed.sidecar.store == Store.ITCH) ItchCheckButton(installed) if (installed != null && installed.sidecar.store == Store.GAMEJOLT) GameJoltCheckButton(installed) + FavoriteButton(favoriteKey) RemoveButton(title, game, installed, a) } } @@ -632,7 +636,7 @@ private fun GameMoreMenu(g: Library.SteamGame, a: FrontEndActions) { } @Composable -internal fun IconAction(icon: androidx.compose.ui.graphics.vector.ImageVector, label: String, id: String, danger: Boolean = false, enabled: Boolean = true, frosted: Boolean = false, onClick: () -> Unit) { +internal fun IconAction(icon: androidx.compose.ui.graphics.vector.ImageVector, label: String, id: String, danger: Boolean = false, enabled: Boolean = true, frosted: Boolean = false, tint: Color? = null, onClick: () -> Unit) { val colors = MaterialTheme.colorScheme val pill = LocalPillButtons.current val shape = if (pill) CircleShape else Shape12 @@ -657,12 +661,24 @@ internal fun IconAction(icon: androidx.compose.ui.graphics.vector.ImageVector, l .controllerConfirm(enabled = enabled, onClick = onClick) .semantics { contentDescription = label }, ) { - Icon(icon, contentDescription = null, tint = if (danger && hot) colors.error else if (danger && !pill) Color.White else colors.onBackground, modifier = Modifier.size(18.dp)) + Icon(icon, contentDescription = null, tint = tint ?: if (danger && hot) colors.error else if (danger && !pill) Color.White else colors.onBackground, modifier = Modifier.size(18.dp)) } } if (frosted) Box(Modifier.glass(CircleShape)) { button() } else button() } +@Composable +internal fun FavoriteButton(key: String) { + val ctx = LocalContext.current + remember { LibraryFavorites.load(ctx) } + val on = key in LibraryFavorites.keys + IconAction( + if (on) Icons.Filled.Favorite else Icons.Filled.FavoriteBorder, + stringResource(if (on) R.string.fm_favorite_remove else R.string.fm_favorite_add), + "game:favorite", frosted = true, tint = if (on) FavoritesColours.dot else null, + ) { LibraryFavorites.toggle(ctx, key) } +} + @Composable internal fun RemoveButton(title: String, game: Library.SteamGame?, installed: InstalledStoreGame?, a: FrontEndActions) { val ctx = LocalContext.current diff --git a/app/src/main/java/com/droiddeck/launcher/ui/LibraryFavorites.kt b/app/src/main/java/com/droiddeck/launcher/ui/LibraryFavorites.kt new file mode 100644 index 0000000000..2aab3b4b9f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/LibraryFavorites.kt @@ -0,0 +1,35 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import com.droiddeck.launcher.session.SessionPrefs + +internal const val LIBRARY_FAVORITES = "favorites" + +internal object LibraryFavorites { + var keys by mutableStateOf>(emptySet()) + private set + private var loaded = false + + fun load(context: Context) { + if (loaded) return + loaded = true + keys = SessionPrefs.libraryFavorites(context) + } + + fun toggle(context: Context, key: String) { + load(context) + keys = toggled(keys, key) + SessionPrefs.setLibraryFavorites(context, keys) + } +} + +internal fun toggled(keys: Set, key: String): Set = if (key in keys) keys - key else keys + key + +internal fun inLibrarySource(source: String, entry: LibraryEntry, favorites: Set): Boolean = when (source) { + LIBRARY_ALL -> true + LIBRARY_FAVORITES -> entry.favoriteKey in favorites + else -> entry.source == source +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt index e3d76dcd1f..d4210f326b 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt @@ -126,6 +126,7 @@ internal class LibraryEntry( val game: Library.SteamGame?, val item: CatalogItem?, val installed: Boolean, ) { val storeKey: String? get() = item?.key ?: game?.storeId?.let { "${game.source}:$it" } + val favoriteKey: String get() = storeKey ?: key } internal fun libraryEntries( @@ -162,9 +163,12 @@ internal fun collectionEntries(all: List, items: List internal fun installedFirst(entries: List): List = entries.partition { it.installed }.let { (on, off) -> on + off } internal const val LIBRARY_ALL = "all" -internal fun librarySources(entries: List, order: List, always: Set = emptySet()): List = - (listOf(Library.SOURCE_STEAM) + arrangeStoreTabs(order, emptySet()).map { it.store.id } + Library.ADDED + Library.SOURCE_COLLECTION) - .filter { source -> source in always || entries.any { it.source == source } } +internal fun librarySources(entries: List, order: List, always: Set = emptySet(), favorites: Set = emptySet()): List = + (listOf(LIBRARY_FAVORITES, Library.SOURCE_STEAM) + arrangeStoreTabs(order, emptySet()).map { it.store.id } + Library.ADDED + Library.SOURCE_COLLECTION) + .filter { source -> + if (source == LIBRARY_FAVORITES) entries.any { it.favoriteKey in favorites } + else source in always || entries.any { it.source == source } + } internal object LibraryDisplay { val scales = listOf(0.75f, 0.875f, 1f, 1.2f, 1.45f) @@ -222,7 +226,9 @@ internal fun LibraryPage(s: FrontEndState, a: FrontEndActions, selected: String, val inputMode = androidx.compose.ui.platform.LocalInputModeManager.current LaunchedEffect(Unit) { StoresState.refresh(ctx) } remember { LibraryDisplay.load(ctx) } + remember { LibraryFavorites.load(ctx) } val soundMode = selected == "soundtracks" || selected.startsWith("sound:") + val favorites = if (soundMode) emptySet() else LibraryFavorites.keys val stores = s.gameStoresEnabled && !s.shortcutPicker && !soundMode val signedIn = Store.entries.filter { StoresState.isSignedIn(it) } LaunchedEffect(stores, signedIn) { if (stores) signedIn.forEach { StoresState.loadLibrary(ctx, it) } } @@ -260,6 +266,7 @@ internal fun LibraryPage(s: FrontEndState, a: FrontEndActions, selected: String, val sources = librarySources( all, SessionPrefs.storesTabOrder(ctx), listOfNotNull(Store.ITCH.id.takeIf { itchSignedIn && !soundMode }, Store.GAMEJOLT.id.takeIf { gameJoltSignedIn && !soundMode }, Library.SOURCE_COLLECTION.takeIf { soundMode }).toSet(), + favorites, ) val source = LibraryView.source.takeIf { it == LIBRARY_ALL || it in sources } ?: LIBRARY_ALL val q = LibraryView.query.trim().lowercase() @@ -282,7 +289,7 @@ internal fun LibraryPage(s: FrontEndState, a: FrontEndActions, selected: String, val collectionItems = collectionId?.let { if (source == Store.GAMEJOLT.id) GameJoltCollections.gamesOf(it) else ItchCollections.gamesOf(it) } val pool = if (collectionId == null) all else collectionEntries(all, collectionItems.orEmpty(), Store.byId(source) ?: Store.ITCH) val matching = pool.filter { - (source == LIBRARY_ALL || it.source == source) && (q.isEmpty() || it.title.lowercase().contains(q)) && keepsXbox(xboxFilter, it) + inLibrarySource(source, it, favorites) && (q.isEmpty() || it.title.lowercase().contains(q)) && keepsXbox(xboxFilter, it) } val listUninstalled = if (collectionId == null) showUninstalled else collectionShows?.takeIf { it.first == collectionId }?.second ?: true val trackHits by produceState>(emptyList(), soundMode, q, source, s.soundtracks) { @@ -522,7 +529,7 @@ private fun LibraryGrid( } val picker = s.shortcutPicker && e.game != null val choose = if (picker) ({ a.onSteamGame(e.game!!) }) else null - if (listMode) LibraryRow(e, first, navigation, choose, onOpen) else LibraryTile(e, capsules, first, navigation, choose, onOpen, showSource = source == LIBRARY_ALL) + if (listMode) LibraryRow(e, first, navigation, choose, onOpen) else LibraryTile(e, capsules, first, navigation, choose, onOpen, showSource = source == LIBRARY_ALL || source == LIBRARY_FAVORITES) } LazyVerticalGrid( columns = if (listMode) GridCells.Fixed(1) else GridCells.Adaptive(tileWidth), state = grid, @@ -672,10 +679,19 @@ private fun LibraryFilters(sources: List, source: String, onSource: (Str val store = Store.byId(src) val dot = when { store != null -> sourceColours(src).dot.copy(alpha = if (StoresState.isSignedIn(store) && StoresState.expired[store] != true) 1f else 0.35f) + src == LIBRARY_FAVORITES -> FavoritesColours.dot src == Library.SOURCE_STEAM || src == Library.ADDED || src == Library.SOURCE_COLLECTION -> sourceColours(src).dot else -> null } - SourceFilterChip(src, if (src == Library.ADDED) stringResource(R.string.library_filter_added) else store?.shortLabel ?: sourceLabel(src), source == src, dot) { onSource(src) } + SourceFilterChip( + src, + when (src) { + Library.ADDED -> stringResource(R.string.library_filter_added) + LIBRARY_FAVORITES -> stringResource(R.string.soundtracks_favorites) + else -> store?.shortLabel ?: sourceLabel(src) + }, + source == src, dot, + ) { onSource(src) } } } } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/LibraryFavoritesTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/LibraryFavoritesTest.kt new file mode 100644 index 0000000000..699ca03720 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/LibraryFavoritesTest.kt @@ -0,0 +1,53 @@ +package com.droiddeck.launcher.ui + +import com.droiddeck.launcher.frontend.Library +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class LibraryFavoritesTest { + private fun steam(id: Int, name: String) = LibraryEntry("app:$id", Library.SOURCE_STEAM, name, Library.SteamGame(id, name, null, "Steam library"), null, true) + + private fun owned(id: String, name: String): LibraryEntry { + val item = CatalogItem(Store.GOG, id, name, imageUrl = null, owned = true) + return LibraryEntry("app:gog:$id", Store.GOG.id, name, null, item, false) + } + + @Test fun togglingAddsThenRemovesAKey() { + val on = toggled(emptySet(), "app:440") + assertEquals(setOf("app:440"), on) + assertEquals(emptySet(), toggled(on, "app:440")) + } + + @Test fun aStoreGameIsKeyedByItsStoreProductSoInstallingItKeepsTheHeart() { + val entry = owned("123", "Witcher") + assertEquals("gog:123", entry.favoriteKey) + val installed = LibraryEntry("app:-5", "gog", "Game", Library.SteamGame(-5, "Game", null, Library.ADDED, source = "gog", storeId = "123"), null, true) + assertEquals(entry.favoriteKey, installed.favoriteKey) + } + + @Test fun aGameWithoutAStoreIsKeyedByItsLibraryKey() { + assertEquals("app:440", steam(440, "Team Fortress 2").favoriteKey) + } + + @Test fun theFavoritesSourceKeepsOnlyHeartedGamesFromEveryStore() { + val tf2 = steam(440, "Team Fortress 2") + val witcher = owned("123", "Witcher") + val other = steam(10, "Counter-Strike") + val hearts = setOf(tf2.favoriteKey, witcher.favoriteKey) + val shown = listOf(tf2, witcher, other).filter { inLibrarySource(LIBRARY_FAVORITES, it, hearts) } + assertEquals(listOf(tf2, witcher), shown) + assertTrue(inLibrarySource(LIBRARY_ALL, other, hearts)) + assertFalse(inLibrarySource(Store.GOG.id, tf2, hearts)) + } + + @Test fun theFavoritesTabComesFirstAndOnlyAppearsOnceSomethingIsHearted() { + val entries = listOf(steam(440, "Team Fortress 2"), owned("123", "Witcher")) + assertEquals(listOf("steam", "gog"), librarySources(entries, listOf("gog"))) + assertEquals(listOf(LIBRARY_FAVORITES, "steam", "gog"), librarySources(entries, listOf("gog"), favorites = setOf("gog:123"))) + assertEquals(listOf("steam", "gog"), librarySources(entries, listOf("gog"), favorites = setOf("gog:999"))) + } +}