Skip to content

Instantly share code, notes, and snippets.

class IOSGPSService: GPSService {
private let asyncLocationManager = AsyncLocationManager(desiredAccuracy: .threeKilometersAccuracy)
func userGPSCoordinates() async throws -> GPSCoordinates? {
switch asyncLocationManager.getAuthorizationStatus() {
case .authorizedAlways, .authorizedWhenInUse:
let locationUpdate = try? await asyncLocationManager.requestLocation()
switch locationUpdate {
case .didUpdateLocations(let locations):
@sdetilly
sdetilly / AndroidGPSService.kt
Created July 28, 2023 01:57
AndroidGPSService
class AndroidGPSService(
private val context: Context
) : GPSService {
// withContext is necessary here because location queries need to be done in the UI Thread
override suspend fun userGPSCoordinates() = withContext(Dispatchers.Main.immediate) {
suspendCancellableCoroutine { continuation ->
// PermissionUtils checks permission using ContextCompat.checkSelfPermission(...) and returns true if granted
if (PermissionUtils.hasPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)) {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
@sdetilly
sdetilly / GPSService.kt
Created July 25, 2023 18:16
Common code GPSService
interface GPSService {
suspend fun userGPSCoordinates(): GPSCoordinates?
}
data class GPSCoordinates(
val latitude: Double,
val longitude: Double
)