feat: implémentation Play Games Services v2 sur Android

This commit is contained in:
herel committed 2026-08-29 08:43:11 +02:00
1 parent d2c5fa0fdb
commit a86b534659
5 files changed
+66 -3

No files matched your search

@@ -5,6 +5,7 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.platform.LocalView
@@ -12,11 +13,20 @@ import androidx.core.view.WindowInsetsControllerCompat
import com.livrets.App
class AppActivity : ComponentActivity() {
private val leaderboardLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
val gamesServices = PlayGamesServices(this) { intent -> leaderboardLauncher.launch(intent) }
setContent {
App(filesDirPath = filesDir.absolutePath, onThemeChanged = { ThemeChanged(it) })
App(
filesDirPath = filesDir.absolutePath,
gamesServices = gamesServices,
onThemeChanged = { ThemeChanged(it) },
)
}
}
}
@@ -0,0 +1,48 @@
package com.livrets.androidApp
import android.app.Activity
import android.content.Intent
import com.google.android.gms.games.PlayGames
import com.livrets.games.GamesServices
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
class PlayGamesServices(
private val activity: Activity,
private val launchIntent: (Intent) -> Unit,
) : GamesServices {
override suspend fun signIn(): Boolean = withContext(Dispatchers.IO) {
runCatching {
PlayGames.getGamesSignInClient(activity).signIn().await().isAuthenticated
}.getOrDefault(false)
}
override suspend fun isSignedIn(): Boolean = withContext(Dispatchers.IO) {
runCatching {
PlayGames.getGamesSignInClient(activity).isAuthenticated().await().isAuthenticated
}.getOrDefault(false)
}
override suspend fun submitScore(leaderboardId: String, score: Long): Boolean =
withContext(Dispatchers.IO) {
runCatching {
PlayGames.getLeaderboardsClient(activity)
.submitScoreImmediate(leaderboardId, score)
.await()
true
}.getOrDefault(false)
}
override suspend fun showLeaderboard(leaderboardId: String) {
val intent = withContext(Dispatchers.IO) {
runCatching {
PlayGames.getLeaderboardsClient(activity)
.getLeaderboardIntent(leaderboardId)
.await()
}.getOrNull()
} ?: return
launchIntent(intent)
}
}