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
This commit is contained in:
FX1922 authored and GitHub committed 2026-11-14 10:08:27 -05:00
1 parent 76c4de5b5d
commit 50fbcf8b2a
5 files changed
+134 -8

No files matched your search

@@ -277,6 +277,12 @@ object SessionPrefs {
prefs(context).edit().putBoolean("libraryShowUninstalled", show).apply()
}
fun libraryFavorites(context: Context): Set<String> = prefs(context).getStringSet("libraryFavorites", null)?.toSet().orEmpty()
fun setLibraryFavorites(context: Context, keys: Set<String>) {
prefs(context).edit().putStringSet("libraryFavorites", keys.toSet()).apply()
}
fun storesTabOrder(context: Context): List<String> = idList(prefs(context).getString("storesTabOrder", null))
fun setStoresTabOrder(context: Context, order: List<String>) {
@@ -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
@@ -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<Set<String>>(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<String>, key: String): Set<String> = if (key in keys) keys - key else keys + key
internal fun inLibrarySource(source: String, entry: LibraryEntry, favorites: Set<String>): Boolean = when (source) {
LIBRARY_ALL -> true
LIBRARY_FAVORITES -> entry.favoriteKey in favorites
else -> entry.source == source
}
@@ -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<LibraryEntry>, items: List<CatalogItem>
internal fun installedFirst(entries: List<LibraryEntry>): List<LibraryEntry> = entries.partition { it.installed }.let { (on, off) -> on + off }
internal const val LIBRARY_ALL = "all"
internal fun librarySources(entries: List<LibraryEntry>, order: List<String>, always: Set<String> = emptySet()): List<String> =
(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<LibraryEntry>, order: List<String>, always: Set<String> = emptySet(), favorites: Set<String> = emptySet()): List<String> =
(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<List<SoundtrackIndex.Hit>>(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<String>, 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) }
}
}
}
@@ -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<String>(), 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")))
}
}