Modern Apps git

← Modern-Apps.git

hub: fix profile name and avatar display (#696, #697)

Commit
a6e61f087198c2035b93888a5b91bc2309c21557
Author
vayun-mathur <[email protected]>
When
2026-09-12 23:52 UTC (2 months ago)
Parents
1c82e1c

Diff

games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.ktindex 41d9865445..639a98c48f 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/MainActivity.kt@@ -14,9 +14,10 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier+import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.games.hub.ui.components.AvatarIcon import com.vayunmathur.library.ui.IconDashboard import com.vayunmathur.library.ui.IconEmojiEvents-import com.vayunmathur.library.ui.IconPerson import com.vayunmathur.library.ui.IconSportsEsports import com.vayunmathur.library.ui.PagerTab import com.vayunmathur.library.ui.TabStyle@@ -134,6 +135,7 @@ private fun HubTabs( ) {     val pagerState = rememberPagerState(pageCount = { 4 })     val scope = rememberCoroutineScope()+    val profile by viewModel.profileFlow.collectAsStateWithLifecycle()     // Home and Games are adjacent pages showing the same games (recentlyPlayed is a subset of the     // full list), and the pager composes both while a swipe is in flight - so "hub-game-<id>" would     // have two live origins. settledPage rather than currentPage: currentPage flips at the halfway@@ -161,7 +163,7 @@ private fun HubTabs(         PagerTab("Achievements", { IconEmojiEvents() }) {             AchievementsScreen(viewModel = viewModel)         },-        PagerTab("Profile", { IconPerson() }) {+        PagerTab("Profile", { AvatarIcon(symbol = profile?.avatarSymbol) }) {             ProfilePage(viewModel = viewModel)         },     )
games/hub/src/main/java/com/vayunmathur/games/hub/ui/components/AvatarBadge.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/components/AvatarBadge.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/components/AvatarBadge.ktnew file mode 100644index 0000000000..d1529de4ef--- /dev/null+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/components/AvatarBadge.kt@@ -0,0 +1,31 @@+package com.vayunmathur.games.hub.ui.components++import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.shape.CircleShape+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.MaterialTheme++/**+ * Circular avatar badge for profile headers. Same sizing and surface as+ * [LevelBadge] so the two are interchangeable in header rows; the level+ * itself stays visible in the surrounding text.+ */+@Composable+fun AvatarBadge(symbol: String?, modifier: Modifier = Modifier, large: Boolean = false) {+    val size = if (large) 56.dp else 36.dp+    Box(+        modifier = modifier+            .size(size)+            .clip(CircleShape)+            .background(MaterialTheme.colorScheme.primary),+        contentAlignment = Alignment.Center+    ) {+        AvatarIcon(symbol = symbol, tint = MaterialTheme.colorScheme.onPrimary)+    }+}
games/hub/src/main/java/com/vayunmathur/games/hub/ui/components/AvatarIcon.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/ui/components/AvatarIcon.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/components/AvatarIcon.ktnew file mode 100644index 0000000000..75c035f800--- /dev/null+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/ui/components/AvatarIcon.kt@@ -0,0 +1,50 @@+package com.vayunmathur.games.hub.ui.components++import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color+import com.vayunmathur.library.ui.IconEmoji+import com.vayunmathur.library.ui.IconEmojiEvents+import com.vayunmathur.library.ui.IconFire+import com.vayunmathur.library.ui.IconFlashOn+import com.vayunmathur.library.ui.IconFlight+import com.vayunmathur.library.ui.IconHotel+import com.vayunmathur.library.ui.IconLightbulb+import com.vayunmathur.library.ui.IconMenuBook+import com.vayunmathur.library.ui.IconPerson+import com.vayunmathur.library.ui.IconShapeDiamondFill+import com.vayunmathur.library.ui.IconShield+import com.vayunmathur.library.ui.IconSportsEsports+import com.vayunmathur.library.ui.IconStar+import com.vayunmathur.library.ui.IconVerify+import com.vayunmathur.library.ui.LocalContentColor++/**+ * Renders the player's chosen avatar [symbol] using only the shared `IconXxx`+ * wrappers — app modules must never call `Icon()` or import+ * `androidx.compose.material.icons.*` directly. Unknown or null symbols fall+ * back to [IconPerson] so a stale saved value can never render blank.+ */+@Composable+fun AvatarIcon(+    symbol: String?,+    modifier: Modifier = Modifier,+    tint: Color = LocalContentColor.current,+) {+    when (symbol) {+        "stadia_controller", "sports_esports" -> IconSportsEsports(modifier, tint)+        "emoji_events" -> IconEmojiEvents(modifier, tint)+        "military_tech" -> IconShield(modifier, tint)+        "star" -> IconStar(modifier, tint)+        "bolt" -> IconFlashOn(modifier, tint)+        "local_fire_department" -> IconFire(modifier, tint)+        "rocket" -> IconFlight(modifier, tint)+        "diamond" -> IconShapeDiamondFill(modifier, tint)+        "psychology" -> IconEmoji(modifier, tint)+        "lightbulb" -> IconLightbulb(modifier, tint)+        "school" -> IconMenuBook(modifier, tint)+        "workspace_premium" -> IconVerify(modifier, tint)+        "king_bed" -> IconHotel(modifier, tint)+        else -> IconPerson(modifier, tint)+    }+}
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.ktindex 2f6a6090bb..cc25125de6 100644--- 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.kt@@ -62,6 +62,7 @@ fun DashboardPage(     DashboardScreen(         state = DashboardUiState(             playerName = profile?.displayName,+            avatarSymbol = profile?.avatarSymbol,             level = level,             title = title,             totalXp = xp,
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 d7446cb73c..bf3da55386 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@@ -21,8 +21,8 @@ import androidx.compose.ui.unit.dp 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.AvatarBadge import com.vayunmathur.games.hub.ui.components.GameCard-import com.vayunmathur.games.hub.ui.components.LevelBadge import com.vayunmathur.games.hub.ui.components.StatCard import com.vayunmathur.games.hub.ui.components.StreakCard import com.vayunmathur.games.hub.ui.components.XpProgressBar@@ -71,7 +71,7 @@ fun DashboardScreen(             item {                 Card(onClick = actions::openProfile, modifier = Modifier.fillMaxWidth()) {                     Row(modifier = Modifier.padding(16.dp), horizontalArrangement = Arrangement.spacedBy(12.dp)) {-                        LevelBadge(level = state.level, large = true)+                        AvatarBadge(symbol = state.avatarSymbol, large = true)                         Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) {                             Text(text = state.playerName ?: stringResource(R.string.player), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold)                             Text(text = state.title, style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.primary)
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 f109bdb7e1..ed4aaf4554 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@@ -23,7 +23,8 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import com.vayunmathur.games.hub.R-import com.vayunmathur.games.hub.ui.components.LevelBadge+import com.vayunmathur.games.hub.ui.components.AvatarBadge+import com.vayunmathur.games.hub.ui.components.AvatarIcon import com.vayunmathur.games.hub.ui.components.StatCard import com.vayunmathur.games.hub.ui.components.XpProgressBar import com.vayunmathur.games.hub.util.ProfileActions@@ -61,10 +62,9 @@ fun ProfileScreen(     var editName by remember { mutableStateOf("") }     var selectedAvatar by remember { mutableStateOf<String?>(null) } -    if (showEditDialog && editName.isEmpty() && state.playerName != null) {-        editName = state.playerName-        selectedAvatar = state.avatarSymbol-    }+    // Initialised from saved state when the dialog is opened (see the edit+    // IconButton below); while open, editName is purely local so clearing the+    // field stays empty until save, which validates with isNotBlank().      AppScaffold(         title = {},@@ -81,7 +81,7 @@ fun ProfileScreen(             item {                 Card(modifier = Modifier.fillMaxWidth()) {                     Column(modifier = Modifier.padding(24.dp).fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(12.dp), horizontalAlignment = Alignment.CenterHorizontally) {-                        LevelBadge(level = state.level, large = true)+                        AvatarBadge(symbol = state.avatarSymbol, large = true)                         Text(text = state.playerName ?: stringResource(R.string.player), style = MaterialTheme.typography.headlineSmall, fontWeight = FontWeight.Bold)                         Text(text = state.title, style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.primary)                         Text(stringResource(R.string.level_1, state.level), style = MaterialTheme.typography.labelLarge)@@ -135,7 +135,7 @@ fun ProfileScreen(                         Text(stringResource(R.string.avatar), style = MaterialTheme.typography.labelMedium)                         LazyRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) {                             items(avatarOptions) { sym ->-                                FilterChip(selected = selectedAvatar == sym, onClick = { selectedAvatar = if (selectedAvatar == sym) null else sym }, label = { Text(sym) })+                                FilterChip(selected = selectedAvatar == sym, onClick = { selectedAvatar = if (selectedAvatar == sym) null else sym }, label = { Text(sym) }, leadingIcon = { AvatarIcon(symbol = sym) })                             }                         }                     }
games/hub/src/main/java/com/vayunmathur/games/hub/util/HubUiContract.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/util/HubUiContract.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/util/HubUiContract.ktindex 927c61baa9..aaabbbb35f 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/util/HubUiContract.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/util/HubUiContract.kt@@ -22,6 +22,7 @@ import com.vayunmathur.games.hub.viewmodel.CrossGameStats /** What the dashboard draws. `playerName` is null until the profile row loads. */ data class DashboardUiState(     val playerName: String? = null,+    val avatarSymbol: String? = null,     val level: Int = 1,     val title: String = "Beginner",     val totalXp: Int = 0,