Silence warning about using deprecated UIKitView

This commit is contained in:
Olof Hedman
2025-10-22 15:13:41 +02:00
parent 224fb9edd4
commit 6c37a4232d

View File

@@ -4,8 +4,9 @@ package app.klottr.maps
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.interop.UIKitView
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.viewinterop.UIKitInteropProperties
import androidx.compose.ui.viewinterop.UIKitView
import cocoapods.GoogleMaps.GMSMapView import cocoapods.GoogleMaps.GMSMapView
import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.channels.BufferOverflow
@@ -50,6 +51,17 @@ private class IOSMapController(
} }
} }
private class MapContainerView(val mapView: GMSMapView) : UIView(frame = CGRectZero.readValue()) {
override fun layoutSubviews() {
super.layoutSubviews()
// Fill the container’s bounds whenever Compose changes our size.
mapView.setFrame(frame = bounds)
mapView.setNeedsLayout()
mapView.layoutIfNeeded()
}
}
@OptIn(ExperimentalForeignApi::class) @OptIn(ExperimentalForeignApi::class)
@Composable @Composable
actual fun MapContainer( actual fun MapContainer(
@@ -61,46 +73,50 @@ actual fun MapContainer(
var mapViewRef by remember { mutableStateOf<GMSMapView?>(null) } var mapViewRef by remember { mutableStateOf<GMSMapView?>(null) }
var features by remember { mutableStateOf(state.features) } var features by remember { mutableStateOf(state.features) }
UIKitView( UIKitView(factory = {
factory = { val camera = GMSCameraPosition.cameraWithTarget(
val camera = GMSCameraPosition.cameraWithTarget( CLLocationCoordinate2DMake(state.camera.target.latitude, state.camera.target.longitude),
CLLocationCoordinate2DMake(state.camera.target.latitude, state.camera.target.longitude), state.camera.zoom
state.camera.zoom )
) val mv = GMSMapView.mapWithFrame(frame = CGRectZero.readValue(), camera = camera)
val mv = GMSMapView.mapWithFrame(frame = CGRectZero.readValue(), camera = camera) mv.settings.compassButton = state.ui.compassEnabled
mv.settings.compassButton = state.ui.compassEnabled mv.settings.zoomGestures = true
mv.settings.zoomGestures = true mv.settings.scrollGestures = true
mv.settings.scrollGestures = true mv.settings.myLocationButton = state.ui.myLocationButtonEnabled
mv.settings.myLocationButton = state.ui.myLocationButtonEnabled mv.mapType = when (state.ui.mapType) {
mv.mapType = when (state.ui.mapType) { MapType.SATELLITE -> kGMSTypeSatellite
MapType.SATELLITE -> kGMSTypeSatellite MapType.NORMAL -> kGMSTypeNormal
MapType.NORMAL -> kGMSTypeNormal }
mv.setDelegate(object : NSObject(), GMSMapViewDelegateProtocol {
override fun mapView(mapView: GMSMapView, didLongPressAtCoordinate: CValue<CLLocationCoordinate2D>) {
didLongPressAtCoordinate.useContents {
val lat = latitude
val lng = longitude
onMapClick?.invoke(LatLng(lat, lng))
}
}
override fun mapView(mapView: GMSMapView, didTapMarker: GMSMarker): Boolean {
val id = didTapMarker.userData as? String ?: return false
onMarkerClick?.invoke(id)
return true
} }
mv.setDelegate(object : NSObject(), GMSMapViewDelegateProtocol {
})
override fun mapView(mapView: GMSMapView, didLongPressAtCoordinate: CValue<CLLocationCoordinate2D>) { // Wrap the map in a container that handles resizing via layoutSubviews.
didLongPressAtCoordinate.useContents { val container = MapContainerView(mv)
val lat = latitude container.addSubview(mv)
val lng = longitude // initial features
onMapClick?.invoke(LatLng(lat, lng)) applyFeatures(mv, features)
} mapViewRef = mv
} container
override fun mapView(mapView: GMSMapView, didTapMarker: GMSMarker): Boolean { },
val id = didTapMarker.userData as? String ?: return false
onMarkerClick?.invoke(id)
return true
}
})
mapViewRef = mv
// initial features
applyFeatures(mv, features)
mv
},
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
update = { mv: UIView -> update = { root: UIView ->
(mv as GMSMapView).let { map -> // Support both direct map view (older code paths) and our container.
val map = (root as? MapContainerView)?.mapView ?: (root as GMSMapView)
run {
// update UI toggles // update UI toggles
map.settings.compassButton = state.ui.compassEnabled map.settings.compassButton = state.ui.compassEnabled
map.settings.myLocationButton = state.ui.myLocationButtonEnabled map.settings.myLocationButton = state.ui.myLocationButtonEnabled
@@ -120,21 +136,9 @@ actual fun MapContainer(
onRelease = { onRelease = {
mapViewRef = null mapViewRef = null
}, },
onResize = { view: GMSMapView, rect: CValue<CGRect> -> properties = UIKitInteropProperties(isInteractive = true,
// Compose gives the new CGRect for this UIKitView slot. isNativeAccessibilityEnabled = true
view.setFrame(frame = rect) ))
// Let Google Maps re-layout its subviews (compass, my-location, logo) after size changes.
view.setNeedsLayout()
view.layoutIfNeeded()
// If you rely on content padding/insets, re-apply here.
// view.setPadding(UIEdgeInsetsZero.readValue())
// Do not touch the camera to avoid jumps/flicker.
},
interactive = true,
accessibilityEnabled = true
)
// Expose controller once // Expose controller once
LaunchedEffect(mapViewRef) { LaunchedEffect(mapViewRef) {