feat: afficher le nom du joueur connecté sur l'accueil

This commit is contained in:
herel committed 2026-08-31 09:12:49 +02:00
1 parent 2cc73959c0
commit bc89f098f0
5 files changed
+37 -1

No files matched your search

@@ -25,6 +25,12 @@ class PlayGamesServices(
}.getOrDefault(false)
}
override suspend fun playerDisplayName(): String? = withContext(Dispatchers.IO) {
runCatching {
PlayGames.getPlayersClient(activity).currentPlayer.await().displayName
}.getOrNull()
}
override suspend fun submitScore(leaderboardId: String, score: Long): Boolean =
withContext(Dispatchers.IO) {
runCatching {
@@ -49,8 +49,12 @@ fun App(
val scope = rememberCoroutineScope()
val history = remember { HistoryRepository("$filesDirPath/history.json".toPath()) }
var screen by remember { mutableStateOf<Screen>(Screen.Home) }
var playerName by remember { mutableStateOf<String?>(null) }
LaunchedEffect(gamesServices) { gamesServices.signIn() }
LaunchedEffect(gamesServices) {
gamesServices.signIn()
playerName = gamesServices.playerDisplayName()
}
val homePreview: @Composable () -> Unit = {
HomeScreen(onPlay = {}, onHistory = {}, onLeaderboard = {})
@@ -65,9 +69,11 @@ fun App(
Screen.Home -> HomeScreen(
onPlay = { config -> screen = Screen.Game(config) },
onHistory = { screen = Screen.History },
playerName = playerName,
onLeaderboard = { config ->
scope.launch {
if (gamesServices.isSignedIn() || gamesServices.signIn()) {
if (playerName == null) playerName = gamesServices.playerDisplayName()
gamesServices.showLeaderboard(config.leaderboardId())
}
}
@@ -3,6 +3,7 @@ package com.livrets.games
interface GamesServices {
suspend fun signIn(): Boolean
suspend fun isSignedIn(): Boolean
suspend fun playerDisplayName(): String?
suspend fun submitScore(leaderboardId: String, score: Long): Boolean
suspend fun showLeaderboard(leaderboardId: String)
}
@@ -10,6 +11,7 @@ interface GamesServices {
object NoopGamesServices : GamesServices {
override suspend fun signIn(): Boolean = false
override suspend fun isSignedIn(): Boolean = false
override suspend fun playerDisplayName(): String? = null
override suspend fun submitScore(leaderboardId: String, score: Long): Boolean = false
override suspend fun showLeaderboard(leaderboardId: String) {}
}
@@ -33,6 +33,7 @@ fun HomeScreen(
onPlay: (GameConfig) -> Unit,
onHistory: () -> Unit,
onLeaderboard: (GameConfig) -> Unit,
playerName: String? = null,
) {
var questionCount by remember { mutableStateOf(10) }
var secondsPerQuestion by remember { mutableStateOf(5) }
@@ -64,6 +65,14 @@ fun HomeScreen(
TextButton(
onClick = { onLeaderboard(GameConfig(questionCount, secondsPerQuestion, maxTable)) },
) { Text("Classement") }
playerName?.let { name ->
Spacer(Modifier.height(8.dp))
Text(
text = "Connecté : $name",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
}
@@ -0,0 +1,13 @@
package com.livrets.games
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertNull
class GamesServicesTest {
@Test
fun `NoopGamesServices ne renvoie pas de nom de joueur`() = runTest {
assertNull(NoopGamesServices.playerDisplayName())
}
}