feat: connexion et envoi du score Play Jeux dans l'UI partagée
This commit is contained in:
3 files changed
+31
-1
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package com.livrets
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -9,6 +10,10 @@ import androidx.compose.runtime.setValue
|
||||
import com.livrets.data.HistoryRepository
|
||||
import com.livrets.game.GameConfig
|
||||
import com.livrets.game.GameResult
|
||||
import com.livrets.games.GamesServices
|
||||
import com.livrets.games.NoopGamesServices
|
||||
import com.livrets.games.leaderboardId
|
||||
import com.livrets.games.leaderboardScore
|
||||
import com.livrets.theme.AppTheme
|
||||
import com.livrets.ui.GameScreen
|
||||
import com.livrets.ui.HistoryScreen
|
||||
@@ -28,20 +33,30 @@ sealed interface Screen {
|
||||
@Composable
|
||||
fun App(
|
||||
filesDirPath: String,
|
||||
gamesServices: GamesServices = NoopGamesServices,
|
||||
onThemeChanged: @Composable (isDark: Boolean) -> Unit = {},
|
||||
) = AppTheme(onThemeChanged) {
|
||||
val scope = rememberCoroutineScope()
|
||||
val history = remember { HistoryRepository("$filesDirPath/history.json".toPath()) }
|
||||
var screen by remember { mutableStateOf<Screen>(Screen.Home) }
|
||||
|
||||
LaunchedEffect(gamesServices) { gamesServices.signIn() }
|
||||
|
||||
val homePreview: @Composable () -> Unit = {
|
||||
HomeScreen(onPlay = {}, onHistory = {})
|
||||
HomeScreen(onPlay = {}, onHistory = {}, onLeaderboard = {})
|
||||
}
|
||||
|
||||
when (val current = screen) {
|
||||
Screen.Home -> HomeScreen(
|
||||
onPlay = { config -> screen = Screen.Game(config) },
|
||||
onHistory = { screen = Screen.History },
|
||||
onLeaderboard = { config ->
|
||||
scope.launch {
|
||||
if (gamesServices.isSignedIn() || gamesServices.signIn()) {
|
||||
gamesServices.showLeaderboard(config.leaderboardId())
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
is Screen.Game -> GameScreen(
|
||||
@@ -49,6 +64,12 @@ fun App(
|
||||
onFinish = { result ->
|
||||
screen = Screen.Results(result)
|
||||
scope.launch { history.save(result) }
|
||||
scope.launch {
|
||||
val score = result.leaderboardScore()
|
||||
if (score > 0 && gamesServices.isSignedIn()) {
|
||||
gamesServices.submitScore(result.config.leaderboardId(), score)
|
||||
}
|
||||
}
|
||||
},
|
||||
onQuit = { screen = Screen.Home },
|
||||
)
|
||||
|
||||
@@ -32,6 +32,7 @@ import com.livrets.game.GameConfig
|
||||
fun HomeScreen(
|
||||
onPlay: (GameConfig) -> Unit,
|
||||
onHistory: () -> Unit,
|
||||
onLeaderboard: (GameConfig) -> Unit,
|
||||
) {
|
||||
var questionCount by remember { mutableStateOf(10) }
|
||||
var secondsPerQuestion by remember { mutableStateOf(5) }
|
||||
@@ -60,6 +61,9 @@ fun HomeScreen(
|
||||
Text("Jouer", fontSize = 20.sp)
|
||||
}
|
||||
TextButton(onClick = onHistory) { Text("Historique") }
|
||||
TextButton(
|
||||
onClick = { onLeaderboard(GameConfig(questionCount, secondsPerQuestion, maxTable)) },
|
||||
) { Text("Classement") }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.livrets.game.GameResult
|
||||
import com.livrets.games.leaderboardScore
|
||||
|
||||
@Composable
|
||||
fun ResultsScreen(
|
||||
@@ -55,6 +56,10 @@ fun ResultsScreen(
|
||||
text = "Temps moyen de réponse : ${result.averageResponseMs / 1000.0} s",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
)
|
||||
Text(
|
||||
text = "Points classement : ${result.leaderboardScore()}",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
)
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
Text(
|
||||
|
||||
Reference in new issue
Block a user