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,8 +73,7 @@ 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
@@ -93,14 +104,19 @@ actual fun MapContainer(
} }
}) })
mapViewRef = mv // Wrap the map in a container that handles resizing via layoutSubviews.
val container = MapContainerView(mv)
container.addSubview(mv)
// initial features // initial features
applyFeatures(mv, features) applyFeatures(mv, features)
mv mapViewRef = mv
container
}, },
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) {