diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 4264cd1..53d077b 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -10,7 +10,7 @@ android { compileSdk = 37 defaultConfig { - minSdk = 23 + minSdk = 24 targetSdk = 37 applicationId = "com.livrets.androidApp" @@ -31,4 +31,6 @@ kotlin { dependencies { implementation(project(":sharedUI")) implementation(libs.androidx.activityCompose) + implementation(libs.gms.games.v2) + implementation(libs.kotlinx.coroutines.play.services) } diff --git a/androidApp/src/main/kotlin/com/livrets/androidApp/AppActivity.kt b/androidApp/src/main/kotlin/com/livrets/androidApp/AppActivity.kt index a043f67..bc2da62 100644 --- a/androidApp/src/main/kotlin/com/livrets/androidApp/AppActivity.kt +++ b/androidApp/src/main/kotlin/com/livrets/androidApp/AppActivity.kt @@ -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) }, + ) } } } diff --git a/androidApp/src/main/kotlin/com/livrets/androidApp/PlayGamesServices.kt b/androidApp/src/main/kotlin/com/livrets/androidApp/PlayGamesServices.kt new file mode 100644 index 0000000..85068fb --- /dev/null +++ b/androidApp/src/main/kotlin/com/livrets/androidApp/PlayGamesServices.kt @@ -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) + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 00d7677..5e90e8c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,6 +9,7 @@ kotlinx-datetime = "0.8.0" kotlinx-serialization = "1.11.0" kotlinx-coroutines = "1.11.0" okio = "3.18.1" +gms-games-v2 = "22.0.0" [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" } okio = { module = "com.squareup.okio:okio", 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] diff --git a/sharedUI/build.gradle.kts b/sharedUI/build.gradle.kts index acd695d..c6b58ca 100644 --- a/sharedUI/build.gradle.kts +++ b/sharedUI/build.gradle.kts @@ -14,7 +14,7 @@ kotlin { android { namespace = "com.livrets" compileSdk = 37 - minSdk = 23 + minSdk = 24 androidResources.enable = true compilerOptions { jvmTarget = JvmTarget.JVM_17 } }