Fix an issue with mixed iOS and Kotlin supertypes in ios LocationServices

This commit is contained in:
Olof Hedman
2025-09-29 14:09:50 +02:00
parent ccad0ec592
commit 167c69142b

View File

@@ -1,3 +1,5 @@
@file:OptIn(ExperimentalForeignApi::class)
package app.klottr.location package app.klottr.location
import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.ExperimentalForeignApi
@@ -12,7 +14,7 @@ import platform.darwin.NSObject
private class IOSLocationService( private class IOSLocationService(
private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
) : NSObject(), LocationService, CLLocationManagerDelegateProtocol { ) : LocationService {
private val manager = CLLocationManager() private val manager = CLLocationManager()
private val _location = MutableStateFlow<GeoPoint?>(null) private val _location = MutableStateFlow<GeoPoint?>(null)
@@ -21,9 +23,25 @@ private class IOSLocationService(
private var started = false private var started = false
private var highAccuracy = true private var highAccuracy = true
init { // Dedicated Obj-C delegate object; keeps Kotlin type separate from Obj-C supertypes
manager.delegate = this 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) { override fun start(updates: LocationUpdates) {
if (started) return if (started) return
@@ -61,28 +79,24 @@ private class IOSLocationService(
return CLLocationManager.locationServicesEnabled() 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 { actual object LocationServiceProvider {
private val instance: LocationService by lazy { IOSLocationService() } private val instance: LocationService by lazy { IOSLocationService() }
actual fun get(): LocationService = instance 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)
}
}