Fix an issue with mixed iOS and Kotlin supertypes in ios LocationServices
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
@file:OptIn(ExperimentalForeignApi::class)
|
||||
|
||||
package app.klottr.location
|
||||
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
@@ -12,7 +14,7 @@ import platform.darwin.NSObject
|
||||
|
||||
private class IOSLocationService(
|
||||
private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
) : NSObject(), LocationService, CLLocationManagerDelegateProtocol {
|
||||
) : LocationService {
|
||||
|
||||
private val manager = CLLocationManager()
|
||||
private val _location = MutableStateFlow<GeoPoint?>(null)
|
||||
@@ -21,9 +23,25 @@ private class IOSLocationService(
|
||||
private var started = false
|
||||
private var highAccuracy = true
|
||||
|
||||
init {
|
||||
manager.delegate = this
|
||||
// Dedicated Obj-C delegate object; keeps Kotlin type separate from Obj-C supertypes
|
||||
private val delegate = LocationDelegate(
|
||||
onUpdate = { last ->
|
||||
val ts = last.timestamp.timeIntervalSince1970() * 1000.0
|
||||
last.coordinate.useContents {
|
||||
val lat = latitude
|
||||
val lon = longitude
|
||||
val p = GeoPoint(
|
||||
lat = lat,
|
||||
lon = lon,
|
||||
accuracyMeters = last.horizontalAccuracy,
|
||||
timeMillis = ts.toLong()
|
||||
)
|
||||
_location.value = p
|
||||
LastLocationStore.update(p)
|
||||
}
|
||||
},
|
||||
onError = { /* ignore */ }
|
||||
).also { manager.delegate = it }
|
||||
|
||||
override fun start(updates: LocationUpdates) {
|
||||
if (started) return
|
||||
@@ -61,28 +79,24 @@ private class IOSLocationService(
|
||||
return CLLocationManager.locationServicesEnabled()
|
||||
}
|
||||
|
||||
// CLLocationManagerDelegate
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
override fun locationManager(manager: CLLocationManager, didUpdateLocations: List<*>) {
|
||||
val last = (didUpdateLocations.lastOrNull() as? CLLocation) ?: return
|
||||
val ts = last.timestamp.timeIntervalSince1970() * 1000.0
|
||||
last.coordinate.useContents {
|
||||
val lat = latitude
|
||||
val lon = longitude
|
||||
val p = GeoPoint(
|
||||
lat = lat,
|
||||
lon = lon,
|
||||
accuracyMeters = last.horizontalAccuracy,
|
||||
timeMillis = ts.toLong()
|
||||
)
|
||||
_location.value = p
|
||||
LastLocationStore.update(p)
|
||||
}
|
||||
}
|
||||
override fun locationManager(manager: CLLocationManager, didFailWithError: NSError) { /* ignore */ }
|
||||
}
|
||||
|
||||
actual object LocationServiceProvider {
|
||||
private val instance: LocationService by lazy { IOSLocationService() }
|
||||
actual fun get(): LocationService = instance
|
||||
}
|
||||
|
||||
// Obj-C delegate kept in a separate class to avoid mixing Kotlin/Obj-C supertypes
|
||||
private class LocationDelegate(
|
||||
private val onUpdate: (CLLocation) -> Unit,
|
||||
private val onError: (NSError) -> Unit
|
||||
) : NSObject(), CLLocationManagerDelegateProtocol {
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
override fun locationManager(manager: CLLocationManager, didUpdateLocations: List<*>) {
|
||||
val last = (didUpdateLocations.lastOrNull() as? CLLocation) ?: return
|
||||
onUpdate(last)
|
||||
}
|
||||
override fun locationManager(manager: CLLocationManager, didFailWithError: NSError) {
|
||||
onError(didFailWithError)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user