Skip to content

Commit

Permalink
Feature/kotlinx coroutines play services (#122)
Browse files Browse the repository at this point in the history
Co-authored-by: Jordon de Hoog <[email protected]>
  • Loading branch information
cristan and jordond authored Dec 23, 2024
1 parent d32bf87 commit fd7eda2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion compass-geolocation-mobile/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<LocationResult> {
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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" }
Expand Down

0 comments on commit fd7eda2

Please sign in to comment.