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.ui.Modifier
import androidx.compose.ui.interop.UIKitView
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.viewinterop.UIKitInteropProperties
import androidx.compose.ui.viewinterop.UIKitView
import cocoapods.GoogleMaps.GMSMapView
import kotlinx.cinterop.ExperimentalForeignApi
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)
@Composable
actual fun MapContainer(
@@ -61,46 +73,50 @@ actual fun MapContainer(
var mapViewRef by remember { mutableStateOf<GMSMapView?>(null) }
var features by remember { mutableStateOf(state.features) }
UIKitView(
factory = {
val camera = GMSCameraPosition.cameraWithTarget(
CLLocationCoordinate2DMake(state.camera.target.latitude, state.camera.target.longitude),
state.camera.zoom
)
val mv = GMSMapView.mapWithFrame(frame = CGRectZero.readValue(), camera = camera)
mv.settings.compassButton = state.ui.compassEnabled
mv.settings.zoomGestures = true
mv.settings.scrollGestures = true
mv.settings.myLocationButton = state.ui.myLocationButtonEnabled
mv.mapType = when (state.ui.mapType) {
MapType.SATELLITE -> kGMSTypeSatellite
MapType.NORMAL -> kGMSTypeNormal
UIKitView(factory = {
val camera = GMSCameraPosition.cameraWithTarget(
CLLocationCoordinate2DMake(state.camera.target.latitude, state.camera.target.longitude),
state.camera.zoom
)
val mv = GMSMapView.mapWithFrame(frame = CGRectZero.readValue(), camera = camera)
mv.settings.compassButton = state.ui.compassEnabled
mv.settings.zoomGestures = true
mv.settings.scrollGestures = true
mv.settings.myLocationButton = state.ui.myLocationButtonEnabled
mv.mapType = when (state.ui.mapType) {
MapType.SATELLITE -> kGMSTypeSatellite
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>) {
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
}
})
mapViewRef = mv
// initial features
applyFeatures(mv, features)
mv
},
})
// Wrap the map in a container that handles resizing via layoutSubviews.
val container = MapContainerView(mv)
container.addSubview(mv)
// initial features
applyFeatures(mv, features)
mapViewRef = mv
container
},
modifier = Modifier.fillMaxSize(),
update = { mv: UIView ->
(mv as GMSMapView).let { map ->
update = { root: UIView ->
// Support both direct map view (older code paths) and our container.
val map = (root as? MapContainerView)?.mapView ?: (root as GMSMapView)
run {
// update UI toggles
map.settings.compassButton = state.ui.compassEnabled
map.settings.myLocationButton = state.ui.myLocationButtonEnabled
@@ -120,21 +136,9 @@ actual fun MapContainer(
onRelease = {
mapViewRef = null
},
onResize = { view: GMSMapView, rect: CValue<CGRect> ->
// Compose gives the new CGRect for this UIKitView slot.
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
)
properties = UIKitInteropProperties(isInteractive = true,
isNativeAccessibilityEnabled = true
))
// Expose controller once
LaunchedEffect(mapViewRef) {