Modern Apps git

← Modern-Apps.git

games/hub: split ui/screens/ to one public @Composable per file (DashboardPage, GamesListPage, ProfilePage)

Commit
27375a4e97cd78c78c3f13353f78dfd2bb430db4
Author
vayun-mathur <[email protected]>
When
2026-08-15 17:56 UTC (3 months ago)
Parents
4129ac9

Diff

games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/DashboardPage.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/DashboardPage.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/DashboardPage.ktnew file mode 100644index 0000000000..392e95637d--- /dev/null+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/DashboardPage.kt@@ -0,0 +1,83 @@+package com.vayunmathur.games.hub.ui.screens++import android.graphics.drawable.Drawable+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.remember+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.LocalContext+import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.games.hub.data.entities.HubGameEntity+import com.vayunmathur.games.hub.util.DashboardActions+import com.vayunmathur.games.hub.util.DashboardUiState+import com.vayunmathur.games.hub.util.GameIconResolver+import com.vayunmathur.games.hub.viewmodel.GameHubViewModel+import com.vayunmathur.library.ui.BackupButtons++/** Binds [GameHubViewModel] to the stateless [DashboardScreen]. */+@Composable+fun DashboardPage(+    viewModel: GameHubViewModel,+    onGameClick: (String) -> Unit,+    onProfileClick: () -> Unit,+    onActivityClick: () -> Unit,+    onGamesClick: () -> Unit,+    dbConfigs: List<Pair<String, String>>,+    datastoreNames: List<String>,+    modifier: Modifier = Modifier+) {+    val games by viewModel.gamesFlow.collectAsStateWithLifecycle()+    val crossStats by viewModel.statsFlow.collectAsStateWithLifecycle()+    val xp by viewModel.totalXpFlow.collectAsStateWithLifecycle()+    val level by viewModel.levelFlow.collectAsStateWithLifecycle()+    val title by viewModel.titleFlow.collectAsStateWithLifecycle()+    val profile by viewModel.profileFlow.collectAsStateWithLifecycle()+    val recentActivity by viewModel.recentActivityFlow.collectAsStateWithLifecycle()+    val allAchievements by viewModel.allAchievementsFlow.collectAsStateWithLifecycle()+    val sessions by viewModel.sessionsFlow.collectAsStateWithLifecycle()+    val context = LocalContext.current++    val iconCache = remember { mutableMapOf<String, Drawable?>() }++    val installedGameIds = remember(games) {+        games.filter { g ->+            try { context.packageManager.getPackageInfo(g.packageName, 0); true } catch (_: Exception) { false }+        }.mapTo(mutableSetOf()) { it.gameId }+    }++    val recentlyPlayedGames = remember(sessions, games) {+        val cutoff = System.currentTimeMillis() - 7L * 24 * 60 * 60 * 1000+        sessions.filter { it.startTime >= cutoff }+            .distinctBy { it.gameId }+            .mapNotNull { session -> games.find { g -> g.gameId == session.gameId } }+            .take(10)+    }++    val achievementProgressByGame = remember(allAchievements) {+        allAchievements.groupBy { it.gameId }.mapValues { (_, list) -> list.count { it.isUnlocked } to list.size }+    }++    DashboardScreen(+        state = DashboardUiState(+            playerName = profile?.displayName,+            level = level,+            title = title,+            totalXp = xp,+            stats = crossStats,+            recentlyPlayed = recentlyPlayedGames,+            recentActivity = recentActivity,+            achievementProgressByGame = achievementProgressByGame,+            installedGameIds = installedGameIds,+        ),+        actions = object : DashboardActions {+            override fun openGame(gameId: String) = onGameClick(gameId)+            override fun openProfile() = onProfileClick()+            override fun openActivity() = onActivityClick()+            override fun openGamesList() = onGamesClick()+            override fun playGame(game: HubGameEntity) = launchGame(context, game)+        },+        iconFor = { game -> iconCache.getOrPut(game.packageName) { GameIconResolver.resolveAppIcon(context, game.packageName) } },+        topBarActions = { BackupButtons(dbConfigs = dbConfigs, datastoreNames = datastoreNames) },+        modifier = modifier,+    )+}
games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/DashboardScreen.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/DashboardScreen.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/DashboardScreen.ktindex d7860515ca..fddd43b282 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/DashboardScreen.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/DashboardScreen.kt@@ -14,14 +14,11 @@ import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.items import androidx.compose.runtime.Composable-import androidx.compose.runtime.getValue-import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier-import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp-import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.games.hub.R import com.vayunmathur.games.hub.data.entities.HubGameEntity import com.vayunmathur.games.hub.ui.components.ActivityItemCard import com.vayunmathur.games.hub.ui.components.GameCard@@ -31,10 +28,7 @@ import com.vayunmathur.games.hub.ui.components.StreakCard import com.vayunmathur.games.hub.ui.components.XpProgressBar import com.vayunmathur.games.hub.util.DashboardActions import com.vayunmathur.games.hub.util.DashboardUiState-import com.vayunmathur.games.hub.util.GameIconResolver import com.vayunmathur.games.hub.util.formatPlaytime-import com.vayunmathur.games.hub.viewmodel.GameHubViewModel-import com.vayunmathur.library.ui.BackupButtons import com.vayunmathur.library.ui.Card import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Scaffold@@ -42,75 +36,6 @@ import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton import com.vayunmathur.library.ui.TopAppBar import androidx.compose.ui.res.stringResource-import com.vayunmathur.games.hub.R--/** Binds [GameHubViewModel] to the stateless [DashboardScreen]. */-@Composable-fun DashboardPage(-    viewModel: GameHubViewModel,-    onGameClick: (String) -> Unit,-    onProfileClick: () -> Unit,-    onActivityClick: () -> Unit,-    onGamesClick: () -> Unit,-    dbConfigs: List<Pair<String, String>>,-    datastoreNames: List<String>,-    modifier: Modifier = Modifier-) {-    val games by viewModel.gamesFlow.collectAsStateWithLifecycle()-    val crossStats by viewModel.statsFlow.collectAsStateWithLifecycle()-    val xp by viewModel.totalXpFlow.collectAsStateWithLifecycle()-    val level by viewModel.levelFlow.collectAsStateWithLifecycle()-    val title by viewModel.titleFlow.collectAsStateWithLifecycle()-    val profile by viewModel.profileFlow.collectAsStateWithLifecycle()-    val recentActivity by viewModel.recentActivityFlow.collectAsStateWithLifecycle()-    val allAchievements by viewModel.allAchievementsFlow.collectAsStateWithLifecycle()-    val sessions by viewModel.sessionsFlow.collectAsStateWithLifecycle()-    val context = LocalContext.current--    val iconCache = remember { mutableMapOf<String, Drawable?>() }--    val installedGameIds = remember(games) {-        games.filter { g ->-            try { context.packageManager.getPackageInfo(g.packageName, 0); true } catch (_: Exception) { false }-        }.mapTo(mutableSetOf()) { it.gameId }-    }--    val recentlyPlayedGames = remember(sessions, games) {-        val cutoff = System.currentTimeMillis() - 7L * 24 * 60 * 60 * 1000-        sessions.filter { it.startTime >= cutoff }-            .distinctBy { it.gameId }-            .mapNotNull { session -> games.find { g -> g.gameId == session.gameId } }-            .take(10)-    }--    val achievementProgressByGame = remember(allAchievements) {-        allAchievements.groupBy { it.gameId }.mapValues { (_, list) -> list.count { it.isUnlocked } to list.size }-    }--    DashboardScreen(-        state = DashboardUiState(-            playerName = profile?.displayName,-            level = level,-            title = title,-            totalXp = xp,-            stats = crossStats,-            recentlyPlayed = recentlyPlayedGames,-            recentActivity = recentActivity,-            achievementProgressByGame = achievementProgressByGame,-            installedGameIds = installedGameIds,-        ),-        actions = object : DashboardActions {-            override fun openGame(gameId: String) = onGameClick(gameId)-            override fun openProfile() = onProfileClick()-            override fun openActivity() = onActivityClick()-            override fun openGamesList() = onGamesClick()-            override fun playGame(game: HubGameEntity) = launchGame(context, game)-        },-        iconFor = { game -> iconCache.getOrPut(game.packageName) { GameIconResolver.resolveAppIcon(context, game.packageName) } },-        topBarActions = { BackupButtons(dbConfigs = dbConfigs, datastoreNames = datastoreNames) },-        modifier = modifier,-    )-}  /**  * The dashboard, with no dependency on the ViewModel so it can be rendered from a
games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/GamesListPage.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/GamesListPage.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/GamesListPage.ktnew file mode 100644index 0000000000..ffa2dd17fc--- /dev/null+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/GamesListPage.kt@@ -0,0 +1,52 @@+package com.vayunmathur.games.hub.ui.screens++import android.graphics.drawable.Drawable+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.remember+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.LocalContext+import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.games.hub.data.entities.HubGameEntity+import com.vayunmathur.games.hub.util.GameIconResolver+import com.vayunmathur.games.hub.util.GamesListActions+import com.vayunmathur.games.hub.util.GamesListUiState+import com.vayunmathur.games.hub.viewmodel.GameHubViewModel++/** Binds [GameHubViewModel] to the stateless [GamesListScreen]. */+@Composable+fun GamesListPage(+    viewModel: GameHubViewModel,+    onGameClick: (String) -> Unit,+    modifier: Modifier = Modifier+) {+    val games by viewModel.gamesFlow.collectAsStateWithLifecycle()+    val allAchievements by viewModel.allAchievementsFlow.collectAsStateWithLifecycle()+    val context = LocalContext.current++    val iconCache = remember { mutableMapOf<String, Drawable?>() }++    val installedGameIds = remember(games) {+        games.filter { g ->+            try { context.packageManager.getPackageInfo(g.packageName, 0); true } catch (_: Exception) { false }+        }.mapTo(mutableSetOf()) { it.gameId }+    }++    val achievementProgressByGame = remember(allAchievements) {+        allAchievements.groupBy { it.gameId }.mapValues { (_, list) -> list.count { it.isUnlocked } to list.size }+    }++    GamesListScreen(+        state = GamesListUiState(+            games = games,+            achievementProgressByGame = achievementProgressByGame,+            installedGameIds = installedGameIds,+        ),+        actions = object : GamesListActions {+            override fun openGame(gameId: String) = onGameClick(gameId)+            override fun playGame(game: HubGameEntity) = launchGame(context, game)+        },+        iconFor = { game -> iconCache.getOrPut(game.packageName) { GameIconResolver.resolveAppIcon(context, game.packageName) } },+        modifier = modifier,+    )+}
games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/GamesListScreen.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/GamesListScreen.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/GamesListScreen.ktindex ef50a579d2..925a55dafc 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/GamesListScreen.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/GamesListScreen.kt@@ -15,15 +15,12 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier-import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp-import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.games.hub.R import com.vayunmathur.games.hub.data.entities.HubGameEntity import com.vayunmathur.games.hub.ui.components.GameCard-import com.vayunmathur.games.hub.util.GameIconResolver import com.vayunmathur.games.hub.util.GamesListActions import com.vayunmathur.games.hub.util.GamesListUiState-import com.vayunmathur.games.hub.viewmodel.GameHubViewModel import com.vayunmathur.library.ui.CommonSearchBar import com.vayunmathur.library.ui.IconButton import com.vayunmathur.library.ui.IconMoreVert@@ -32,48 +29,9 @@ import com.vayunmathur.library.ui.Scaffold import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TopAppBar import androidx.compose.ui.res.stringResource-import com.vayunmathur.games.hub.R  enum class GameSort { LAST_PLAYED, MOST_PLAYED, NAME, COMPLETION } -/** Binds [GameHubViewModel] to the stateless [GamesListScreen]. */-@Composable-fun GamesListPage(-    viewModel: GameHubViewModel,-    onGameClick: (String) -> Unit,-    modifier: Modifier = Modifier-) {-    val games by viewModel.gamesFlow.collectAsStateWithLifecycle()-    val allAchievements by viewModel.allAchievementsFlow.collectAsStateWithLifecycle()-    val context = LocalContext.current--    val iconCache = remember { mutableMapOf<String, Drawable?>() }--    val installedGameIds = remember(games) {-        games.filter { g ->-            try { context.packageManager.getPackageInfo(g.packageName, 0); true } catch (_: Exception) { false }-        }.mapTo(mutableSetOf()) { it.gameId }-    }--    val achievementProgressByGame = remember(allAchievements) {-        allAchievements.groupBy { it.gameId }.mapValues { (_, list) -> list.count { it.isUnlocked } to list.size }-    }--    GamesListScreen(-        state = GamesListUiState(-            games = games,-            achievementProgressByGame = achievementProgressByGame,-            installedGameIds = installedGameIds,-        ),-        actions = object : GamesListActions {-            override fun openGame(gameId: String) = onGameClick(gameId)-            override fun playGame(game: HubGameEntity) = launchGame(context, game)-        },-        iconFor = { game -> iconCache.getOrPut(game.packageName) { GameIconResolver.resolveAppIcon(context, game.packageName) } },-        modifier = modifier,-    )-}- /**  * The games list, with no dependency on the ViewModel so it can be rendered from a  * `@Preview`. Search and sort stay here: they are the screen's own state, and filtering the
games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/ProfilePage.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/ProfilePage.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/ProfilePage.ktnew file mode 100644index 0000000000..390b488d86--- /dev/null+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/ProfilePage.kt@@ -0,0 +1,31 @@+package com.vayunmathur.games.hub.ui.screens++import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.ui.Modifier+import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.games.hub.util.ProfileUiState+import com.vayunmathur.games.hub.viewmodel.GameHubViewModel++/** Binds [GameHubViewModel] to the stateless [ProfileScreen]. */+@Composable+fun ProfilePage(viewModel: GameHubViewModel, modifier: Modifier = Modifier) {+    val profile by viewModel.profileFlow.collectAsStateWithLifecycle()+    val xp by viewModel.totalXpFlow.collectAsStateWithLifecycle()+    val level by viewModel.levelFlow.collectAsStateWithLifecycle()+    val title by viewModel.titleFlow.collectAsStateWithLifecycle()+    val crossStats by viewModel.statsFlow.collectAsStateWithLifecycle()++    ProfileScreen(+        state = ProfileUiState(+            playerName = profile?.displayName,+            avatarSymbol = profile?.avatarSymbol,+            level = level,+            title = title,+            totalXp = xp,+            stats = crossStats,+        ),+        actions = viewModel,+        modifier = modifier,+    )+}
games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/ProfileScreen.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/ProfileScreen.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/ProfileScreen.ktindex 4dfbd61e12..80970df0c2 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/ProfileScreen.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/screens/ProfileScreen.kt@@ -22,7 +22,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp-import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.games.hub.R import com.vayunmathur.games.hub.ui.components.LevelBadge import com.vayunmathur.games.hub.ui.components.StatCard import com.vayunmathur.games.hub.ui.components.XpProgressBar@@ -30,7 +30,6 @@ import com.vayunmathur.games.hub.util.ProfileActions import com.vayunmathur.games.hub.util.ProfileUiState import com.vayunmathur.games.hub.util.XpLevelCalculator import com.vayunmathur.games.hub.util.formatPlaytime-import com.vayunmathur.games.hub.viewmodel.GameHubViewModel import com.vayunmathur.library.ui.AlertDialog import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Card@@ -41,7 +40,6 @@ import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton import com.vayunmathur.library.ui.TopAppBar import androidx.compose.ui.res.stringResource-import com.vayunmathur.games.hub.R  private val avatarOptions = listOf(     "person", "stadia_controller", "sports_esports", "emoji_events", "military_tech",@@ -49,29 +47,6 @@ private val avatarOptions = listOf(     "psychology", "lightbulb", "school", "workspace_premium", "king_bed" ) -/** Binds [GameHubViewModel] to the stateless [ProfileScreen]. */-@Composable-fun ProfilePage(viewModel: GameHubViewModel, modifier: Modifier = Modifier) {-    val profile by viewModel.profileFlow.collectAsStateWithLifecycle()-    val xp by viewModel.totalXpFlow.collectAsStateWithLifecycle()-    val level by viewModel.levelFlow.collectAsStateWithLifecycle()-    val title by viewModel.titleFlow.collectAsStateWithLifecycle()-    val crossStats by viewModel.statsFlow.collectAsStateWithLifecycle()--    ProfileScreen(-        state = ProfileUiState(-            playerName = profile?.displayName,-            avatarSymbol = profile?.avatarSymbol,-            level = level,-            title = title,-            totalXp = xp,-            stats = crossStats,-        ),-        actions = viewModel,-        modifier = modifier,-    )-}- /**  * The profile screen, with no dependency on the ViewModel so it can be rendered from a  * `@Preview`.