From fd7eda2aa656df3ebd62f677ec759ba2dc230631 Mon Sep 17 00:00:00 2001 From: Cristan Meijer Date: Mon, 23 Dec 2024 21:15:30 +0100 Subject: [PATCH] Feature/kotlinx coroutines play services (#122) Co-authored-by: Jordon de Hoog --- compass-geolocation-mobile/build.gradle.kts | 2 +- .../mobile/internal/LocationManager.kt | 31 ++++++------------- gradle/libs.versions.toml | 3 ++ 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/compass-geolocation-mobile/build.gradle.kts b/compass-geolocation-mobile/build.gradle.kts index f1561f1a..b09420d0 100644 --- a/compass-geolocation-mobile/build.gradle.kts +++ b/compass-geolocation-mobile/build.gradle.kts @@ -27,10 +27,10 @@ kotlin { androidMain.dependencies { implementation(projects.compassToolsAndroid) api(libs.play.services.location) + implementation(libs.play.services.coroutines) implementation(libs.androidx.activity) implementation(libs.androidx.fragment) implementation(libs.androidx.startup) - } iosMain.dependencies { diff --git a/compass-geolocation-mobile/src/androidMain/kotlin/dev/jordond/compass/geolocation/mobile/internal/LocationManager.kt b/compass-geolocation-mobile/src/androidMain/kotlin/dev/jordond/compass/geolocation/mobile/internal/LocationManager.kt index ce890ba3..4e0492fa 100644 --- a/compass-geolocation-mobile/src/androidMain/kotlin/dev/jordond/compass/geolocation/mobile/internal/LocationManager.kt +++ b/compass-geolocation-mobile/src/androidMain/kotlin/dev/jordond/compass/geolocation/mobile/internal/LocationManager.kt @@ -10,12 +10,11 @@ import com.google.android.gms.location.LocationResult import com.google.android.gms.location.LocationServices import com.google.android.gms.tasks.CancellationTokenSource import dev.jordond.compass.exception.NotFoundException +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.suspendCancellableCoroutine -import kotlin.coroutines.resume -import kotlin.coroutines.resumeWithException +import kotlinx.coroutines.tasks.await internal class LocationManager( private val context: Context, @@ -41,28 +40,18 @@ internal class LocationManager( || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) } + @OptIn(ExperimentalCoroutinesApi::class) suspend fun currentLocation(priority: Int): Location { - return suspendCancellableCoroutine { continuation -> - val cancellation = CancellationTokenSource() + val cancellation = CancellationTokenSource() - fusedLocationClient - .getCurrentLocation(priority, cancellation.token) - .addOnSuccessListener { location -> - // Can actually be null. This most often happens when requesting a coarse location - // and no other app recently successfully retrieved a location. - // See https://developer.android.com/develop/sensors-and-location/location/retrieve-current#last-known - if (location == null) { - continuation.resumeWithException(NotFoundException()) - } else { - continuation.resume(location) - } - } - .addOnFailureListener { exception -> continuation.resumeWithException(exception) } + val location: Location? = fusedLocationClient + .getCurrentLocation(priority, cancellation.token) + .await(cancellation) - continuation.invokeOnCancellation { - cancellation.cancel() - } + if (location == null) { + throw NotFoundException() } + return location } fun startTracking(request: LocationRequest): Flow { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3b379b31..14bc2c56 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,6 +4,7 @@ sdk-target = "34" sdk-compile = "34" sdk-min = "21" jvmTarget = "17" + agp = "8.7.3" kotlin = "2.1.0" kotlinx-coroutines = "1.9.0" @@ -16,6 +17,7 @@ androidx-fragment = "1.8.5" androidx-startup = "1.2.0" androidx-activityCompose = "1.9.3" playServicesLocation = "21.3.0" +playServicesCoroutines = "1.9.0" kermit = "2.0.5" binaryCompatibility = "0.17.0" dokka = "2.0.0" @@ -49,6 +51,7 @@ compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" } kermit = { module = "co.touchlab:kermit", version.ref = "kermit" } play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "playServicesLocation" } +play-services-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-play-services", version.ref = "playServicesCoroutines" } stateHolder = { module = "dev.stateholder:core", version.ref = "stateHolder" } stateHolder-compose = { module = "dev.stateholder:extensions-compose", version.ref = "stateHolder" } stateHolder-voyager = { module = "dev.stateholder:extensions-voyager", version.ref = "stateHolder" }