From 820d64e0fcfe917090c421c551e4a3977061b63b Mon Sep 17 00:00:00 2001 From: JSpiner Date: Wed, 16 Oct 2024 13:50:15 +0900 Subject: [PATCH 01/11] feat: Change the `position` parameter of MarkerState to a clearer name. #637 --- .../java/com/google/maps/android/compose/Marker.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt index 476701f6e..716a95438 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt @@ -155,12 +155,12 @@ public class MarkerState private constructor(position: LatLng) { /** * Creates a new [MarkerState] object * - * @param position the initial marker position + * @param initialPosition the initial marker position */ @StateFactoryMarker public operator fun invoke( - position: LatLng = LatLng(0.0, 0.0) - ): MarkerState = MarkerState(position) + initialPosition: LatLng = LatLng(0.0, 0.0) + ): MarkerState = MarkerState(initialPosition) /** * The default saver implementation for [MarkerState] @@ -182,13 +182,15 @@ public class MarkerState private constructor(position: LatLng) { * Other use cases may be better served syncing [MarkerState.position] with a data model. * * This cannot be used to preserve info window visibility across configuration changes. + * + * @param initialPosition the initial marker position */ @Composable public fun rememberMarkerState( key: String? = null, - position: LatLng = LatLng(0.0, 0.0) + initialPosition: LatLng = LatLng(0.0, 0.0) ): MarkerState = rememberSaveable(key = key, saver = MarkerState.Saver) { - MarkerState(position) + MarkerState(initialPosition) } /** From 97ed22c7fa3d1cfea4d7ccbce31c289d99545698 Mon Sep 17 00:00:00 2001 From: JSpiner Date: Wed, 16 Oct 2024 13:56:12 +0900 Subject: [PATCH 02/11] feat: Rename example codes that were using the position `parameter` to `initialPosition`. #637 --- .../maps/android/compose/AccessibilityActivity.kt | 2 +- .../google/maps/android/compose/BasicMapActivity.kt | 10 +++++----- .../google/maps/android/compose/MapInColumnActivity.kt | 2 +- .../maps/android/compose/RecompositionActivity.kt | 2 +- .../compose/markerexamples/AdvancedMarkersActivity.kt | 8 ++++---- .../compose/markerexamples/MarkerClusteringActivity.kt | 2 +- .../markerdragevents/MarkerDragEventsActivity.kt | 2 +- .../UpdatingNoDragMarkerWithDataModelActivity.kt | 2 +- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/google/maps/android/compose/AccessibilityActivity.kt b/app/src/main/java/com/google/maps/android/compose/AccessibilityActivity.kt index 3e032238d..fcd2a5462 100644 --- a/app/src/main/java/com/google/maps/android/compose/AccessibilityActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/AccessibilityActivity.kt @@ -37,7 +37,7 @@ class AccessibilityActivity : ComponentActivity() { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { - val singaporeState = rememberMarkerState(position = singapore) + val singaporeState = rememberMarkerState(initialPosition = singapore) val cameraPositionState = rememberCameraPositionState { position = defaultCameraPosition } diff --git a/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt b/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt index 03be3a67b..4cabd722b 100644 --- a/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt @@ -142,11 +142,11 @@ fun GoogleMapView( mapColorScheme: ComposeMapColorScheme = ComposeMapColorScheme.FOLLOW_SYSTEM, content: @Composable () -> Unit = {} ) { - val singaporeState = rememberMarkerState(position = singapore) - val singapore2State = rememberMarkerState(position = singapore2) - val singapore3State = rememberMarkerState(position = singapore3) - val singapore4State = rememberMarkerState(position = singapore4) - val singapore5State = rememberMarkerState(position = singapore5) + val singaporeState = rememberMarkerState(initialPosition = singapore) + val singapore2State = rememberMarkerState(initialPosition = singapore2) + val singapore3State = rememberMarkerState(initialPosition = singapore3) + val singapore4State = rememberMarkerState(initialPosition = singapore4) + val singapore5State = rememberMarkerState(initialPosition = singapore5) var circleCenter by remember { mutableStateOf(singapore) } if (!singaporeState.isDragging) { diff --git a/app/src/main/java/com/google/maps/android/compose/MapInColumnActivity.kt b/app/src/main/java/com/google/maps/android/compose/MapInColumnActivity.kt index 1908df89f..2110ae44e 100644 --- a/app/src/main/java/com/google/maps/android/compose/MapInColumnActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/MapInColumnActivity.kt @@ -185,7 +185,7 @@ private fun GoogleMapViewInColumn( cameraPositionState: CameraPositionState, onMapLoaded: () -> Unit, ) { - val singaporeState = rememberMarkerState(position = singapore) + val singaporeState = rememberMarkerState(initialPosition = singapore) var uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } var mapProperties by remember { diff --git a/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt b/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt index d0fc8cd50..24df349d0 100644 --- a/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt @@ -70,7 +70,7 @@ class RecompositionActivity : ComponentActivity() { cameraPositionState: CameraPositionState = rememberCameraPositionState(), content: @Composable () -> Unit = {}, ) { - val markerState = rememberMarkerState(position = singapore) + val markerState = rememberMarkerState(initialPosition = singapore) val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } val mapProperties by remember { diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/AdvancedMarkersActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/AdvancedMarkersActivity.kt index 3683e7a99..592b91dd9 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/AdvancedMarkersActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/AdvancedMarkersActivity.kt @@ -70,10 +70,10 @@ class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallbac val mapProperties by remember { mutableStateOf(MapProperties(mapType = MapType.NORMAL)) } - val marker1State = rememberMarkerState(position = santiago) - val marker2State = rememberMarkerState(position = bogota) - val marker3State = rememberMarkerState(position = lima) - val marker4State = rememberMarkerState(position = salvador) + val marker1State = rememberMarkerState(initialPosition = santiago) + val marker2State = rememberMarkerState(initialPosition = bogota) + val marker3State = rememberMarkerState(initialPosition = lima) + val marker4State = rememberMarkerState(initialPosition = salvador) // Drawing on the map is accomplished with a child-based API val markerClick: (Marker) -> Boolean = { diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt index 09b8cd48f..77764d40f 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt @@ -119,7 +119,7 @@ fun GoogleMapClustering(items: List) { } MarkerInfoWindow( - state = rememberMarkerState(position = singapore), + state = rememberMarkerState(initialPosition = singapore), onClick = { Log.d(TAG, "Non-cluster marker clicked! $it") true diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/markerdragevents/MarkerDragEventsActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/markerdragevents/MarkerDragEventsActivity.kt index 75dcc7993..419d0156f 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/markerdragevents/MarkerDragEventsActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/markerdragevents/MarkerDragEventsActivity.kt @@ -87,7 +87,7 @@ private fun DraggableMarker( onDrag: (LatLng) -> Unit = {}, onDragEnd: () -> Unit = {} ) { - val markerState = rememberMarkerState(position = singapore) + val markerState = rememberMarkerState(initialPosition = singapore) Marker( state = markerState, diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt index 28fd66f88..b5ef65a71 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt @@ -152,6 +152,6 @@ fun rememberUpdatedMarkerState(position: LatLng): MarkerState = // rememberUpdatedState() uses MutableState, we use MarkerState. // This is more efficient than updating position in an effect, // as we avoid an additional recomposition. - remember { MarkerState(position = position) }.also { + remember { MarkerState(initialPosition = position) }.also { it.position = position } From a2667681a51cfbff78655741c2af73415f2e71ca Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sat, 14 Dec 2024 16:55:00 +0900 Subject: [PATCH 03/11] Revert "feat: Rename example codes that were using the position `parameter` to `initialPosition`. #637" This reverts commit 565007e4c2162a2b6e59e9eefee1718e067f0eb2. --- .../maps/android/compose/AccessibilityActivity.kt | 2 +- .../google/maps/android/compose/BasicMapActivity.kt | 10 +++++----- .../google/maps/android/compose/MapInColumnActivity.kt | 2 +- .../maps/android/compose/RecompositionActivity.kt | 2 +- .../compose/markerexamples/AdvancedMarkersActivity.kt | 8 ++++---- .../compose/markerexamples/MarkerClusteringActivity.kt | 2 +- .../markerdragevents/MarkerDragEventsActivity.kt | 2 +- .../UpdatingNoDragMarkerWithDataModelActivity.kt | 2 +- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/google/maps/android/compose/AccessibilityActivity.kt b/app/src/main/java/com/google/maps/android/compose/AccessibilityActivity.kt index fcd2a5462..3e032238d 100644 --- a/app/src/main/java/com/google/maps/android/compose/AccessibilityActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/AccessibilityActivity.kt @@ -37,7 +37,7 @@ class AccessibilityActivity : ComponentActivity() { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { - val singaporeState = rememberMarkerState(initialPosition = singapore) + val singaporeState = rememberMarkerState(position = singapore) val cameraPositionState = rememberCameraPositionState { position = defaultCameraPosition } diff --git a/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt b/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt index 4cabd722b..03be3a67b 100644 --- a/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/BasicMapActivity.kt @@ -142,11 +142,11 @@ fun GoogleMapView( mapColorScheme: ComposeMapColorScheme = ComposeMapColorScheme.FOLLOW_SYSTEM, content: @Composable () -> Unit = {} ) { - val singaporeState = rememberMarkerState(initialPosition = singapore) - val singapore2State = rememberMarkerState(initialPosition = singapore2) - val singapore3State = rememberMarkerState(initialPosition = singapore3) - val singapore4State = rememberMarkerState(initialPosition = singapore4) - val singapore5State = rememberMarkerState(initialPosition = singapore5) + val singaporeState = rememberMarkerState(position = singapore) + val singapore2State = rememberMarkerState(position = singapore2) + val singapore3State = rememberMarkerState(position = singapore3) + val singapore4State = rememberMarkerState(position = singapore4) + val singapore5State = rememberMarkerState(position = singapore5) var circleCenter by remember { mutableStateOf(singapore) } if (!singaporeState.isDragging) { diff --git a/app/src/main/java/com/google/maps/android/compose/MapInColumnActivity.kt b/app/src/main/java/com/google/maps/android/compose/MapInColumnActivity.kt index 2110ae44e..1908df89f 100644 --- a/app/src/main/java/com/google/maps/android/compose/MapInColumnActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/MapInColumnActivity.kt @@ -185,7 +185,7 @@ private fun GoogleMapViewInColumn( cameraPositionState: CameraPositionState, onMapLoaded: () -> Unit, ) { - val singaporeState = rememberMarkerState(initialPosition = singapore) + val singaporeState = rememberMarkerState(position = singapore) var uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } var mapProperties by remember { diff --git a/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt b/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt index 24df349d0..d0fc8cd50 100644 --- a/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/RecompositionActivity.kt @@ -70,7 +70,7 @@ class RecompositionActivity : ComponentActivity() { cameraPositionState: CameraPositionState = rememberCameraPositionState(), content: @Composable () -> Unit = {}, ) { - val markerState = rememberMarkerState(initialPosition = singapore) + val markerState = rememberMarkerState(position = singapore) val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } val mapProperties by remember { diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/AdvancedMarkersActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/AdvancedMarkersActivity.kt index 592b91dd9..3683e7a99 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/AdvancedMarkersActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/AdvancedMarkersActivity.kt @@ -70,10 +70,10 @@ class AdvancedMarkersActivity : ComponentActivity(), OnMapsSdkInitializedCallbac val mapProperties by remember { mutableStateOf(MapProperties(mapType = MapType.NORMAL)) } - val marker1State = rememberMarkerState(initialPosition = santiago) - val marker2State = rememberMarkerState(initialPosition = bogota) - val marker3State = rememberMarkerState(initialPosition = lima) - val marker4State = rememberMarkerState(initialPosition = salvador) + val marker1State = rememberMarkerState(position = santiago) + val marker2State = rememberMarkerState(position = bogota) + val marker3State = rememberMarkerState(position = lima) + val marker4State = rememberMarkerState(position = salvador) // Drawing on the map is accomplished with a child-based API val markerClick: (Marker) -> Boolean = { diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt index 77764d40f..09b8cd48f 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/MarkerClusteringActivity.kt @@ -119,7 +119,7 @@ fun GoogleMapClustering(items: List) { } MarkerInfoWindow( - state = rememberMarkerState(initialPosition = singapore), + state = rememberMarkerState(position = singapore), onClick = { Log.d(TAG, "Non-cluster marker clicked! $it") true diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/markerdragevents/MarkerDragEventsActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/markerdragevents/MarkerDragEventsActivity.kt index 419d0156f..75dcc7993 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/markerdragevents/MarkerDragEventsActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/markerdragevents/MarkerDragEventsActivity.kt @@ -87,7 +87,7 @@ private fun DraggableMarker( onDrag: (LatLng) -> Unit = {}, onDragEnd: () -> Unit = {} ) { - val markerState = rememberMarkerState(initialPosition = singapore) + val markerState = rememberMarkerState(position = singapore) Marker( state = markerState, diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt index b5ef65a71..28fd66f88 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt @@ -152,6 +152,6 @@ fun rememberUpdatedMarkerState(position: LatLng): MarkerState = // rememberUpdatedState() uses MutableState, we use MarkerState. // This is more efficient than updating position in an effect, // as we avoid an additional recomposition. - remember { MarkerState(initialPosition = position) }.also { + remember { MarkerState(position = position) }.also { it.position = position } From 690d094dabc9fd3d356a2b9089bc1dec85e2a72d Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sat, 14 Dec 2024 16:55:07 +0900 Subject: [PATCH 04/11] Revert "feat: Change the `position` parameter of MarkerState to a clearer name. #637" This reverts commit 2aa9d4aba5503f670087a975ae20c66935e26845. --- .../java/com/google/maps/android/compose/Marker.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt index 716a95438..476701f6e 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt @@ -155,12 +155,12 @@ public class MarkerState private constructor(position: LatLng) { /** * Creates a new [MarkerState] object * - * @param initialPosition the initial marker position + * @param position the initial marker position */ @StateFactoryMarker public operator fun invoke( - initialPosition: LatLng = LatLng(0.0, 0.0) - ): MarkerState = MarkerState(initialPosition) + position: LatLng = LatLng(0.0, 0.0) + ): MarkerState = MarkerState(position) /** * The default saver implementation for [MarkerState] @@ -182,15 +182,13 @@ public class MarkerState private constructor(position: LatLng) { * Other use cases may be better served syncing [MarkerState.position] with a data model. * * This cannot be used to preserve info window visibility across configuration changes. - * - * @param initialPosition the initial marker position */ @Composable public fun rememberMarkerState( key: String? = null, - initialPosition: LatLng = LatLng(0.0, 0.0) + position: LatLng = LatLng(0.0, 0.0) ): MarkerState = rememberSaveable(key = key, saver = MarkerState.Saver) { - MarkerState(initialPosition) + MarkerState(position) } /** From 2cb2fccf1cf1428341ddecf5ee20e80cb9a3a75b Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sat, 14 Dec 2024 17:09:01 +0900 Subject: [PATCH 05/11] feat: Add 'rememberUpdatedMarkerState' which updates the state value based on updates to the input parameters. --- .../com/google/maps/android/compose/Marker.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt index 476701f6e..c05c28525 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt @@ -191,6 +191,22 @@ public fun rememberMarkerState( MarkerState(position) } +/** + * Uses [rememberSaveable] to retain [MarkerState.position] across configuration changes. + * + * This function updates the state value according to the update of the input parameter, + * like 'rememberUpdatedState'. + * + * This cannot be used to preserve info window visibility across configuration changes. + */ +@Composable +public fun rememberUpdatedMarkerState( + key: String? = null, + position: LatLng = LatLng(0.0, 0.0) +): MarkerState = rememberSaveable(key = key, saver = MarkerState.Saver) { + MarkerState(position = position) +}.also { it.position = position } + /** * A composable for a marker on the map. * @param state the [MarkerState] to be used to control or observe the marker From 2fbf5368869febd4b43cd5516f2252c043aaa101 Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sat, 14 Dec 2024 17:19:35 +0900 Subject: [PATCH 06/11] chore: Add warning comment at rememberMarkerState. --- .../src/main/java/com/google/maps/android/compose/Marker.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt index c05c28525..fdf98f541 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt @@ -182,6 +182,9 @@ public class MarkerState private constructor(position: LatLng) { * Other use cases may be better served syncing [MarkerState.position] with a data model. * * This cannot be used to preserve info window visibility across configuration changes. + * + * This function does not automatically update the MarkerState when the input parameters change. + * If you need this implementation, use 'rememberUpdatedMarkerState'. */ @Composable public fun rememberMarkerState( From c91da9b1763a3698d7d8dc051df7110db780e3d1 Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sat, 14 Dec 2024 17:21:49 +0900 Subject: [PATCH 07/11] feat: Modify the example project to use the newly added 'rememberUpdatedMarkerState'. --- ...datingNoDragMarkerWithDataModelActivity.kt | 28 ++----------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt b/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt index 28fd66f88..03239d65e 100644 --- a/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt +++ b/app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt @@ -24,17 +24,15 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.Immutable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.lifecycle.lifecycleScope import com.google.android.gms.maps.model.LatLng import com.google.maps.android.compose.GoogleMap import com.google.maps.android.compose.Marker -import com.google.maps.android.compose.MarkerState import com.google.maps.android.compose.defaultCameraPosition import com.google.maps.android.compose.rememberCameraPositionState -import com.google.maps.android.compose.rememberMarkerState +import com.google.maps.android.compose.rememberUpdatedMarkerState import com.google.maps.android.compose.singapore import com.google.maps.android.compose.singapore2 import com.google.maps.android.compose.singapore3 @@ -128,30 +126,10 @@ fun Marker( position: LatLng, onClick: () -> Boolean = { false }, ) { - val markerState = rememberUpdatedMarkerState(position) + val markerState = rememberUpdatedMarkerState(position = position) Marker( state = markerState, onClick = { onClick() } ) -} - -/** - * Standard API pattern for remembering, initializing, and updating MarkerState for a - * non-draggable Marker, where [position] comes from a data model. - * - * Implementation modeled after `rememberUpdatedState`. - * - * This one uses [remember] behind the scenes, not [rememberMarkerState], which uses - * `rememberSaveable`. Our data model is the source of truth - `rememberSaveable` would - * create a conflicting source of truth. - */ -@Composable -fun rememberUpdatedMarkerState(position: LatLng): MarkerState = - // This pattern is equivalent to what rememberUpdatedState() does: - // rememberUpdatedState() uses MutableState, we use MarkerState. - // This is more efficient than updating position in an effect, - // as we avoid an additional recomposition. - remember { MarkerState(position = position) }.also { - it.position = position - } +} \ No newline at end of file From 7563ea8298b34982ed03eaeba655729889f24113 Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sat, 14 Dec 2024 17:44:47 +0900 Subject: [PATCH 08/11] feat: Add test case for 'testRememberUpdatedMarkerStateBeUpdate'. --- .../android/compose/GoogleMapViewTests.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app/src/androidTest/java/com/google/maps/android/compose/GoogleMapViewTests.kt b/app/src/androidTest/java/com/google/maps/android/compose/GoogleMapViewTests.kt index d84eae28d..a5a887345 100644 --- a/app/src/androidTest/java/com/google/maps/android/compose/GoogleMapViewTests.kt +++ b/app/src/androidTest/java/com/google/maps/android/compose/GoogleMapViewTests.kt @@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.Modifier import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createComposeRule @@ -281,6 +282,30 @@ class GoogleMapViewTests { .performClick() } + @Test + fun testRememberUpdatedMarkerStateBeUpdate() { + val testPoint0 = LatLng(0.0,0.0) + val testPoint1 = LatLng(37.6281576,-122.4264549) + val testPoint2 = LatLng(37.500012, 127.0364185) + + val positionState = mutableStateOf(testPoint0) + lateinit var markerState: MarkerState + + initMap { + markerState = rememberUpdatedMarkerState(position = positionState.value) + } + + assertEquals(testPoint0, markerState.position) + + positionState.value = testPoint1 + composeTestRule.waitForIdle() + assertEquals(testPoint1, markerState.position) + + positionState.value = testPoint2 + composeTestRule.waitForIdle() + assertEquals(testPoint2, markerState.position) + } + private fun zoom( shouldAnimate: Boolean, zoomIn: Boolean, From bebe715a80fbd9de9569acb7cc153892424c4270 Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sat, 14 Dec 2024 22:34:45 +0900 Subject: [PATCH 09/11] feat: remove rememberSavable for rememberUpdatedMarkerState. --- .../src/main/java/com/google/maps/android/compose/Marker.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt index fdf98f541..cd660e024 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt @@ -23,6 +23,7 @@ import androidx.compose.runtime.MutableState import androidx.compose.runtime.currentComposer import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCompositionContext import androidx.compose.runtime.saveable.Saver import androidx.compose.runtime.saveable.rememberSaveable @@ -195,8 +196,6 @@ public fun rememberMarkerState( } /** - * Uses [rememberSaveable] to retain [MarkerState.position] across configuration changes. - * * This function updates the state value according to the update of the input parameter, * like 'rememberUpdatedState'. * @@ -204,9 +203,8 @@ public fun rememberMarkerState( */ @Composable public fun rememberUpdatedMarkerState( - key: String? = null, position: LatLng = LatLng(0.0, 0.0) -): MarkerState = rememberSaveable(key = key, saver = MarkerState.Saver) { +): MarkerState = remember { MarkerState(position = position) }.also { it.position = position } From 20e603eb012217a8a3b46e0a079e8528dd25941b Mon Sep 17 00:00:00 2001 From: JSpiner Date: Sat, 14 Dec 2024 22:41:27 +0900 Subject: [PATCH 10/11] feat: Deprecate rememberMarkerState function. --- .../java/com/google/maps/android/compose/Marker.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt index cd660e024..8e8aab662 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt @@ -188,6 +188,18 @@ public class MarkerState private constructor(position: LatLng) { * If you need this implementation, use 'rememberUpdatedMarkerState'. */ @Composable +@Deprecated( + message = "Use 'rememberUpdatedMarkerState' instead - It may be confusing to think " + + "that the state is automatically updated as the position changes, " + + "so it will be changed or removed.", + replaceWith = ReplaceWith( + expression = """ + val markerState = rememberSaveable(key = key, saver = MarkerState.Saver) { + MarkerState(position) + } + """ + ) +) public fun rememberMarkerState( key: String? = null, position: LatLng = LatLng(0.0, 0.0) From 5761aafbc991ea4739a48273bc28da6d60010f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=84=B1=EB=AF=BC?= Date: Mon, 16 Dec 2024 23:17:19 +0900 Subject: [PATCH 11/11] Update maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt Co-authored-by: Uli Bubenheimer --- .../src/main/java/com/google/maps/android/compose/Marker.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt index 8e8aab662..b479c726f 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt @@ -211,7 +211,7 @@ public fun rememberMarkerState( * This function updates the state value according to the update of the input parameter, * like 'rememberUpdatedState'. * - * This cannot be used to preserve info window visibility across configuration changes. + * This cannot be used to preserve state across configuration changes. */ @Composable public fun rememberUpdatedMarkerState(