Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to ignore Location services available check #172

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compass-geolocation/api/android/compass-geolocation.api
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public final class dev/jordond/compass/geolocation/ExtensionsKt {
public abstract interface class dev/jordond/compass/geolocation/Geolocator {
public static final field Companion Ldev/jordond/compass/geolocation/Geolocator$Companion;
public abstract fun current (Ldev/jordond/compass/Priority;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun current (Ldev/jordond/compass/geolocation/LocationRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getLocationUpdates ()Lkotlinx/coroutines/flow/Flow;
public abstract fun getTrackingStatus ()Lkotlinx/coroutines/flow/Flow;
public abstract fun isAvailable (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -122,9 +123,10 @@ public final class dev/jordond/compass/geolocation/GeolocatorResult$Success : de

public final class dev/jordond/compass/geolocation/LocationRequest {
public fun <init> ()V
public fun <init> (Ldev/jordond/compass/Priority;JJ)V
public synthetic fun <init> (Ldev/jordond/compass/Priority;JJILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ldev/jordond/compass/Priority;JJZ)V
public synthetic fun <init> (Ldev/jordond/compass/Priority;JJZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getIgnoreAvailableCheck ()Z
public final fun getInterval ()J
public final fun getMaximumAge ()J
public final fun getPriority ()Ldev/jordond/compass/Priority;
Expand Down
6 changes: 4 additions & 2 deletions compass-geolocation/api/jvm/compass-geolocation.api
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public final class dev/jordond/compass/geolocation/ExtensionsKt {
public abstract interface class dev/jordond/compass/geolocation/Geolocator {
public static final field Companion Ldev/jordond/compass/geolocation/Geolocator$Companion;
public abstract fun current (Ldev/jordond/compass/Priority;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun current (Ldev/jordond/compass/geolocation/LocationRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getLocationUpdates ()Lkotlinx/coroutines/flow/Flow;
public abstract fun getTrackingStatus ()Lkotlinx/coroutines/flow/Flow;
public abstract fun isAvailable (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -122,9 +123,10 @@ public final class dev/jordond/compass/geolocation/GeolocatorResult$Success : de

public final class dev/jordond/compass/geolocation/LocationRequest {
public fun <init> ()V
public fun <init> (Ldev/jordond/compass/Priority;JJ)V
public synthetic fun <init> (Ldev/jordond/compass/Priority;JJILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ldev/jordond/compass/Priority;JJZ)V
public synthetic fun <init> (Ldev/jordond/compass/Priority;JJZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getIgnoreAvailableCheck ()Z
public final fun getInterval ()J
public final fun getMaximumAge ()J
public final fun getPriority ()Ldev/jordond/compass/Priority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public interface Geolocator {
*/
public suspend fun current(priority: Priority = Priority.Balanced): GeolocatorResult

/**
* Get the current location.
*
* @param request The location request details.
* @return A [GeolocatorResult] with the success or error state.
*/
public suspend fun current(request: LocationRequest): GeolocatorResult

/**
* Start tracking the location.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import dev.jordond.compass.Priority
* @param interval The interval at which to receive location updates, in milliseconds.
* @param maximumAge The maximum age of a cached location that can be used before a new location is
* requested, in milliseconds.
* @param ignoreAvailableCheck Whether to ignore the availability of location services when
* requesting a location. If `true`, the request will be made regardless of whether location
* services are available, potentially resulting in an error if they are not.
*/
@Poko
public class LocationRequest(
public val priority: Priority = Priority.Balanced,
public val interval: Long = 5000L,
public val maximumAge: Long = 0L,
public val ignoreAvailableCheck: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,18 @@ internal class DefaultGeolocator(

override suspend fun isAvailable(): Boolean = locator.isAvailable()

override suspend fun current(priority: Priority): GeolocatorResult {
return handleResult { locator.current(priority) }
override suspend fun current(request: LocationRequest): GeolocatorResult {
return handleResult(request) { locator.current(request.priority) }
}

override suspend fun current(priority: Priority): GeolocatorResult =
current(LocationRequest(priority))

override fun track(request: LocationRequest): Flow<TrackingStatus> = status.also {
if (trackingJob?.isActive == true) return@also

trackingJob = coroutineScope.launch {
if (!isAvailable()) {
if (!request.ignoreAvailableCheck && !isAvailable()) {
status.update { TrackingStatus.Error(GeolocatorResult.NotSupported) }
} else {
status.update { TrackingStatus.Tracking }
Expand Down Expand Up @@ -86,9 +89,12 @@ internal class DefaultGeolocator(
else -> GeolocatorResult.GeolocationFailed(this.message ?: "Unknown error")
}

private suspend fun handleResult(block: suspend () -> Location?): GeolocatorResult {
private suspend fun handleResult(
request: LocationRequest,
block: suspend () -> Location?,
): GeolocatorResult {
try {
if (!isAvailable()) {
if (!request.ignoreAvailableCheck && !isAvailable()) {
return GeolocatorResult.NotSupported
}
val result = withContext(dispatcher) { block() }
Expand Down
Loading