Skip to content

Commit 0d6f023

Browse files
JSpinerbubenheimerkikoso
authored
chore: Change the confusing parameter position to a clear name. #637 (#638)
* feat: Change the `position` parameter of MarkerState to a clearer name. #637 * feat: Rename example codes that were using the position `parameter` to `initialPosition`. #637 * Revert "feat: Rename example codes that were using the position `parameter` to `initialPosition`. #637" This reverts commit 565007e4c2162a2b6e59e9eefee1718e067f0eb2. * Revert "feat: Change the `position` parameter of MarkerState to a clearer name. #637" This reverts commit 2aa9d4aba5503f670087a975ae20c66935e26845. * feat: Add 'rememberUpdatedMarkerState' which updates the state value based on updates to the input parameters. * chore: Add warning comment at rememberMarkerState. * feat: Modify the example project to use the newly added 'rememberUpdatedMarkerState'. * feat: Add test case for 'testRememberUpdatedMarkerStateBeUpdate'. * feat: remove rememberSavable for rememberUpdatedMarkerState. * feat: Deprecate rememberMarkerState function. * Update maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt Co-authored-by: Uli Bubenheimer <[email protected]> --------- Co-authored-by: Uli Bubenheimer <[email protected]> Co-authored-by: Enrique López Mañas <[email protected]>
1 parent ad7026a commit 0d6f023

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

app/src/androidTest/java/com/google/maps/android/compose/GoogleMapViewTests.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.Box
1818
import androidx.compose.foundation.layout.fillMaxSize
1919
import androidx.compose.material.Text
2020
import androidx.compose.runtime.Composable
21+
import androidx.compose.runtime.mutableStateOf
2122
import androidx.compose.ui.Modifier
2223
import androidx.compose.ui.test.assertIsDisplayed
2324
import androidx.compose.ui.test.junit4.createComposeRule
@@ -281,6 +282,30 @@ class GoogleMapViewTests {
281282
.performClick()
282283
}
283284

285+
@Test
286+
fun testRememberUpdatedMarkerStateBeUpdate() {
287+
val testPoint0 = LatLng(0.0,0.0)
288+
val testPoint1 = LatLng(37.6281576,-122.4264549)
289+
val testPoint2 = LatLng(37.500012, 127.0364185)
290+
291+
val positionState = mutableStateOf(testPoint0)
292+
lateinit var markerState: MarkerState
293+
294+
initMap {
295+
markerState = rememberUpdatedMarkerState(position = positionState.value)
296+
}
297+
298+
assertEquals(testPoint0, markerState.position)
299+
300+
positionState.value = testPoint1
301+
composeTestRule.waitForIdle()
302+
assertEquals(testPoint1, markerState.position)
303+
304+
positionState.value = testPoint2
305+
composeTestRule.waitForIdle()
306+
assertEquals(testPoint2, markerState.position)
307+
}
308+
284309
private fun zoom(
285310
shouldAnimate: Boolean,
286311
zoomIn: Boolean,

app/src/main/java/com/google/maps/android/compose/markerexamples/updatingnodragmarkerwithdatamodel/UpdatingNoDragMarkerWithDataModelActivity.kt

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@ import androidx.compose.runtime.Composable
2424
import androidx.compose.runtime.Immutable
2525
import androidx.compose.runtime.getValue
2626
import androidx.compose.runtime.mutableStateOf
27-
import androidx.compose.runtime.remember
2827
import androidx.compose.runtime.setValue
2928
import androidx.compose.ui.Modifier
3029
import androidx.lifecycle.lifecycleScope
3130
import com.google.android.gms.maps.model.LatLng
3231
import com.google.maps.android.compose.GoogleMap
3332
import com.google.maps.android.compose.Marker
34-
import com.google.maps.android.compose.MarkerState
3533
import com.google.maps.android.compose.defaultCameraPosition
3634
import com.google.maps.android.compose.rememberCameraPositionState
37-
import com.google.maps.android.compose.rememberMarkerState
35+
import com.google.maps.android.compose.rememberUpdatedMarkerState
3836
import com.google.maps.android.compose.singapore
3937
import com.google.maps.android.compose.singapore2
4038
import com.google.maps.android.compose.singapore3
@@ -128,30 +126,10 @@ fun Marker(
128126
position: LatLng,
129127
onClick: () -> Boolean = { false },
130128
) {
131-
val markerState = rememberUpdatedMarkerState(position)
129+
val markerState = rememberUpdatedMarkerState(position = position)
132130

133131
Marker(
134132
state = markerState,
135133
onClick = { onClick() }
136134
)
137-
}
138-
139-
/**
140-
* Standard API pattern for remembering, initializing, and updating MarkerState for a
141-
* non-draggable Marker, where [position] comes from a data model.
142-
*
143-
* Implementation modeled after `rememberUpdatedState`.
144-
*
145-
* This one uses [remember] behind the scenes, not [rememberMarkerState], which uses
146-
* `rememberSaveable`. Our data model is the source of truth - `rememberSaveable` would
147-
* create a conflicting source of truth.
148-
*/
149-
@Composable
150-
fun rememberUpdatedMarkerState(position: LatLng): MarkerState =
151-
// This pattern is equivalent to what rememberUpdatedState() does:
152-
// rememberUpdatedState() uses MutableState, we use MarkerState.
153-
// This is more efficient than updating position in an effect,
154-
// as we avoid an additional recomposition.
155-
remember { MarkerState(position = position) }.also {
156-
it.position = position
157-
}
135+
}

maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.compose.runtime.MutableState
2323
import androidx.compose.runtime.currentComposer
2424
import androidx.compose.runtime.getValue
2525
import androidx.compose.runtime.mutableStateOf
26+
import androidx.compose.runtime.remember
2627
import androidx.compose.runtime.rememberCompositionContext
2728
import androidx.compose.runtime.saveable.Saver
2829
import androidx.compose.runtime.saveable.rememberSaveable
@@ -182,15 +183,43 @@ public class MarkerState private constructor(position: LatLng) {
182183
* Other use cases may be better served syncing [MarkerState.position] with a data model.
183184
*
184185
* This cannot be used to preserve info window visibility across configuration changes.
186+
*
187+
* This function does not automatically update the MarkerState when the input parameters change.
188+
* If you need this implementation, use 'rememberUpdatedMarkerState'.
185189
*/
186190
@Composable
191+
@Deprecated(
192+
message = "Use 'rememberUpdatedMarkerState' instead - It may be confusing to think " +
193+
"that the state is automatically updated as the position changes, " +
194+
"so it will be changed or removed.",
195+
replaceWith = ReplaceWith(
196+
expression = """
197+
val markerState = rememberSaveable(key = key, saver = MarkerState.Saver) {
198+
MarkerState(position)
199+
}
200+
"""
201+
)
202+
)
187203
public fun rememberMarkerState(
188204
key: String? = null,
189205
position: LatLng = LatLng(0.0, 0.0)
190206
): MarkerState = rememberSaveable(key = key, saver = MarkerState.Saver) {
191207
MarkerState(position)
192208
}
193209

210+
/**
211+
* This function updates the state value according to the update of the input parameter,
212+
* like 'rememberUpdatedState'.
213+
*
214+
* This cannot be used to preserve state across configuration changes.
215+
*/
216+
@Composable
217+
public fun rememberUpdatedMarkerState(
218+
position: LatLng = LatLng(0.0, 0.0)
219+
): MarkerState = remember {
220+
MarkerState(position = position)
221+
}.also { it.position = position }
222+
194223
/**
195224
* A composable for a marker on the map.
196225
* @param state the [MarkerState] to be used to control or observe the marker

0 commit comments

Comments
 (0)