feat: implémentation Play Games Services v2 sur Android
This commit is contained in:
5 files changed
+66
-3
No files matched your search
@@ -10,7 +10,7 @@ android {
|
|||||||
compileSdk = 37
|
compileSdk = 37
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdk = 23
|
minSdk = 24
|
||||||
targetSdk = 37
|
targetSdk = 37
|
||||||
|
|
||||||
applicationId = "com.livrets.androidApp"
|
applicationId = "com.livrets.androidApp"
|
||||||
@@ -31,4 +31,6 @@ kotlin {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation(project(":sharedUI"))
|
implementation(project(":sharedUI"))
|
||||||
implementation(libs.androidx.activityCompose)
|
implementation(libs.androidx.activityCompose)
|
||||||
|
implementation(libs.gms.games.v2)
|
||||||
|
implementation(libs.kotlinx.coroutines.play.services)
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ import android.os.Bundle
|
|||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.ui.platform.LocalView
|
import androidx.compose.ui.platform.LocalView
|
||||||
@@ -12,11 +13,20 @@ import androidx.core.view.WindowInsetsControllerCompat
|
|||||||
import com.livrets.App
|
import com.livrets.App
|
||||||
|
|
||||||
class AppActivity : ComponentActivity() {
|
class AppActivity : ComponentActivity() {
|
||||||
|
|
||||||
|
private val leaderboardLauncher =
|
||||||
|
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { }
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
|
val gamesServices = PlayGamesServices(this) { intent -> leaderboardLauncher.launch(intent) }
|
||||||
setContent {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ kotlinx-datetime = "0.8.0"
|
|||||||
kotlinx-serialization = "1.11.0"
|
kotlinx-serialization = "1.11.0"
|
||||||
kotlinx-coroutines = "1.11.0"
|
kotlinx-coroutines = "1.11.0"
|
||||||
okio = "3.18.1"
|
okio = "3.18.1"
|
||||||
|
gms-games-v2 = "22.0.0"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
|
|
||||||
@@ -27,6 +28,8 @@ kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-c
|
|||||||
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
|
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
|
||||||
okio = { module = "com.squareup.okio:okio", version.ref = "okio" }
|
okio = { module = "com.squareup.okio:okio", version.ref = "okio" }
|
||||||
okio-fakefilesystem = { module = "com.squareup.okio:okio-fakefilesystem", version.ref = "okio" }
|
okio-fakefilesystem = { module = "com.squareup.okio:okio-fakefilesystem", version.ref = "okio" }
|
||||||
|
gms-games-v2 = { module = "com.google.android.gms:play-services-games-v2", version.ref = "gms-games-v2" }
|
||||||
|
kotlinx-coroutines-play-services = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-play-services", version.ref = "kotlinx-coroutines" }
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ kotlin {
|
|||||||
android {
|
android {
|
||||||
namespace = "com.livrets"
|
namespace = "com.livrets"
|
||||||
compileSdk = 37
|
compileSdk = 37
|
||||||
minSdk = 23
|
minSdk = 24
|
||||||
androidResources.enable = true
|
androidResources.enable = true
|
||||||
compilerOptions { jvmTarget = JvmTarget.JVM_17 }
|
compilerOptions { jvmTarget = JvmTarget.JVM_17 }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user