From ab6826628b91108459de98c6758feece8c948bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Lo=CC=81pez=20Man=CC=83as?= Date: Thu, 14 Dec 2023 17:18:39 +0100 Subject: [PATCH 1/7] docs: changed recomposition sample --- README.md | 3 +-- .../google/maps/android/compose/RecompositionActivity.kt | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6cb6432e..7f51150a 100644 --- a/README.md +++ b/README.md @@ -209,8 +209,7 @@ Markers and other elements need to be recomposed in the screen. To achieve recom have a mutable state variable: ```kotlin -var location by remember { mutableStateOf(singapore) } -val singaporeState = MarkerState(position = location) +val singaporeState = remember { MarkerState(singapore) } //... 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 8b342e02..88dc0a42 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 @@ -27,7 +27,6 @@ import androidx.compose.runtime.Composable 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 com.google.android.gms.maps.model.Marker import com.google.maps.android.compose.theme.MapsComposeSampleTheme @@ -65,8 +64,7 @@ class RecompositionActivity : ComponentActivity() { cameraPositionState: CameraPositionState = rememberCameraPositionState(), content: @Composable () -> Unit = {}, ) { - var location by remember { mutableStateOf(singapore) } - val singaporeState = MarkerState(position = location) + val singaporeState = remember { MarkerState(singapore) } val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } val mapProperties by remember { @@ -103,7 +101,7 @@ class RecompositionActivity : ComponentActivity() { Column { Button(onClick = { val randomValue = Random.nextInt(3) - location = when (randomValue) { + singaporeState.position = when (randomValue) { 0 -> singapore 1 -> singapore2 2 -> singapore3 From 345d8396b9ed8c99990b5ae2f2aa882bd71d0a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Lo=CC=81pez=20Man=CC=83as?= Date: Thu, 14 Dec 2023 17:36:48 +0100 Subject: [PATCH 2/7] docs: changed recomposition sample --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7f51150a..13dc24cb 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,7 @@ Marker( ) ``` -In the example above, when `location` changes the recomposition will be triggered. +In the example above, when the `position` attribute in the MarkerState changes, the recomposition will be triggered.
From 73abfbc7155d823217c70fa389e470574ea5dc2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Lo=CC=81pez=20Man=CC=83as?= Date: Fri, 15 Dec 2023 10:02:05 +0100 Subject: [PATCH 3/7] docs: comments from PR, improved docs --- README.md | 20 ++++++++++--------- .../android/compose/RecompositionActivity.kt | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 13dc24cb..18a9ae0a 100644 --- a/README.md +++ b/README.md @@ -205,22 +205,24 @@ GoogleMap( ### Recomposing elements -Markers and other elements need to be recomposed in the screen. To achieve recomposition you need to -have a mutable state variable: +Markers and other elements need to be recomposed in the screen. To achieve recomposition, you can set mutable properties of state objects: ```kotlin -val singaporeState = remember { MarkerState(singapore) } +val singaporeState = remember { MarkerState(position = singapore) } //... -Marker( - state = singaporeState, - title = "Marker in Singapore", - onClick = markerClick -) +LaunchedEffect(Unit) { + repeat(10) { + delay(5.seconds) + val old = singaporeState.position + singaporeState.position = LatLng(old.latitude + 1.0, old.longitude + 2.0) + } +} ``` -In the example above, when the `position` attribute in the MarkerState changes, the recomposition will be triggered. +In the example above, recomposition occurs as `MarkerState.position` is updated with different values over time, shifting the Marker around the screen. +
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 88dc0a42..4dcff949 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 @@ -64,7 +64,7 @@ class RecompositionActivity : ComponentActivity() { cameraPositionState: CameraPositionState = rememberCameraPositionState(), content: @Composable () -> Unit = {}, ) { - val singaporeState = remember { MarkerState(singapore) } + val singaporeState = remember { MarkerState(position = singapore) } val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } val mapProperties by remember { From 143f24c0f77ddf4f529b3b86b04406e1cf6cde45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Lo=CC=81pez=20Man=CC=83as?= Date: Fri, 15 Dec 2023 18:26:14 +0100 Subject: [PATCH 4/7] docs: rename singaporeState to markerState --- .../google/maps/android/compose/RecompositionActivity.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 4dcff949..469dc7e1 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 @@ -64,7 +64,7 @@ class RecompositionActivity : ComponentActivity() { cameraPositionState: CameraPositionState = rememberCameraPositionState(), content: @Composable () -> Unit = {}, ) { - val singaporeState = remember { MarkerState(position = singapore) } + val markerState = remember { MarkerState(position = singapore) } val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } val mapProperties by remember { @@ -91,7 +91,7 @@ class RecompositionActivity : ComponentActivity() { } Marker( - state = singaporeState, + state = markerState, title = "Marker in Singapore", onClick = markerClick ) @@ -101,7 +101,7 @@ class RecompositionActivity : ComponentActivity() { Column { Button(onClick = { val randomValue = Random.nextInt(3) - singaporeState.position = when (randomValue) { + markerState.position = when (randomValue) { 0 -> singapore 1 -> singapore2 2 -> singapore3 From 1db551e79b8f3dbb9539a13f42227eeb564e12ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Lo=CC=81pez=20Man=CC=83as?= Date: Thu, 4 Jan 2024 13:38:31 +0100 Subject: [PATCH 5/7] docs: changed naming in README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 18a9ae0a..55153666 100644 --- a/README.md +++ b/README.md @@ -208,15 +208,15 @@ GoogleMap( Markers and other elements need to be recomposed in the screen. To achieve recomposition, you can set mutable properties of state objects: ```kotlin -val singaporeState = remember { MarkerState(position = singapore) } +val markerState = remember { MarkerState(position = singapore) } //... LaunchedEffect(Unit) { repeat(10) { delay(5.seconds) - val old = singaporeState.position - singaporeState.position = LatLng(old.latitude + 1.0, old.longitude + 2.0) + val old = markerState.position + markerState.position = LatLng(old.latitude + 1.0, old.longitude + 2.0) } } ``` From 7cf9778677270d8901d9ac663841da6b1c344839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20L=C3=B3pez=20Ma=C3=B1as?= Date: Mon, 15 Jan 2024 15:24:15 +0100 Subject: [PATCH 6/7] docs: updated README.md Co-authored-by: Uli Bubenheimer --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 55153666..8c126290 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,7 @@ GoogleMap( Markers and other elements need to be recomposed in the screen. To achieve recomposition, you can set mutable properties of state objects: ```kotlin -val markerState = remember { MarkerState(position = singapore) } +val markerState = rememberMarkerState(position = singapore) //... From 11a12b19bcdc74fbd1aad451288946d13ff5ad13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20L=C3=B3pez=20Ma=C3=B1as?= Date: Mon, 15 Jan 2024 15:24:52 +0100 Subject: [PATCH 7/7] docs: updated Recomposition Activity Co-authored-by: Uli Bubenheimer --- .../com/google/maps/android/compose/RecompositionActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 469dc7e1..1154dc51 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 @@ -64,7 +64,7 @@ class RecompositionActivity : ComponentActivity() { cameraPositionState: CameraPositionState = rememberCameraPositionState(), content: @Composable () -> Unit = {}, ) { - val markerState = remember { MarkerState(position = singapore) } + val markerState = rememberMarkerState(position = singapore) val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) } val mapProperties by remember {