From bc89f098f0d33ece9d206a2575d01cb2a5bf61f6 Mon Sep 17 00:00:00 2001 From: Rene Luria Date: Mon, 31 Aug 2026 09:12:49 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20afficher=20le=20nom=20du=20joueur=20con?= =?UTF-8?q?nect=C3=A9=20sur=20l'accueil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/livrets/androidApp/PlayGamesServices.kt | 6 ++++++ sharedUI/src/commonMain/kotlin/com/livrets/App.kt | 8 +++++++- .../kotlin/com/livrets/games/GamesServices.kt | 2 ++ .../commonMain/kotlin/com/livrets/ui/HomeScreen.kt | 9 +++++++++ .../kotlin/com/livrets/games/GamesServicesTest.kt | 13 +++++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 sharedUI/src/commonTest/kotlin/com/livrets/games/GamesServicesTest.kt diff --git a/androidApp/src/main/kotlin/com/livrets/androidApp/PlayGamesServices.kt b/androidApp/src/main/kotlin/com/livrets/androidApp/PlayGamesServices.kt index 85068fb..52f37ca 100644 --- a/androidApp/src/main/kotlin/com/livrets/androidApp/PlayGamesServices.kt +++ b/androidApp/src/main/kotlin/com/livrets/androidApp/PlayGamesServices.kt @@ -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 { diff --git a/sharedUI/src/commonMain/kotlin/com/livrets/App.kt b/sharedUI/src/commonMain/kotlin/com/livrets/App.kt index 380a9bc..da3d21c 100644 --- a/sharedUI/src/commonMain/kotlin/com/livrets/App.kt +++ b/sharedUI/src/commonMain/kotlin/com/livrets/App.kt @@ -49,8 +49,12 @@ fun App( val scope = rememberCoroutineScope() val history = remember { HistoryRepository("$filesDirPath/history.json".toPath()) } var screen by remember { mutableStateOf(Screen.Home) } + var playerName by remember { mutableStateOf(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()) } } diff --git a/sharedUI/src/commonMain/kotlin/com/livrets/games/GamesServices.kt b/sharedUI/src/commonMain/kotlin/com/livrets/games/GamesServices.kt index 4949036..fcb51dd 100644 --- a/sharedUI/src/commonMain/kotlin/com/livrets/games/GamesServices.kt +++ b/sharedUI/src/commonMain/kotlin/com/livrets/games/GamesServices.kt @@ -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) {} } diff --git a/sharedUI/src/commonMain/kotlin/com/livrets/ui/HomeScreen.kt b/sharedUI/src/commonMain/kotlin/com/livrets/ui/HomeScreen.kt index dfa9564..6496a58 100644 --- a/sharedUI/src/commonMain/kotlin/com/livrets/ui/HomeScreen.kt +++ b/sharedUI/src/commonMain/kotlin/com/livrets/ui/HomeScreen.kt @@ -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, + ) + } } } diff --git a/sharedUI/src/commonTest/kotlin/com/livrets/games/GamesServicesTest.kt b/sharedUI/src/commonTest/kotlin/com/livrets/games/GamesServicesTest.kt new file mode 100644 index 0000000..d2e403e --- /dev/null +++ b/sharedUI/src/commonTest/kotlin/com/livrets/games/GamesServicesTest.kt @@ -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()) + } +}