Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improved recomposition sample #475

Merged
merged 7 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +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
var location by remember { mutableStateOf(singapore) }
val singaporeState = MarkerState(position = location)
val singaporeState = remember { MarkerState(position = singapore) }
kikoso marked this conversation as resolved.
Show resolved Hide resolved

//...

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 `location` 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.


</details>
<details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 markerState = remember { MarkerState(position = singapore) }
kikoso marked this conversation as resolved.
Show resolved Hide resolved

val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) }
val mapProperties by remember {
Expand All @@ -93,7 +91,7 @@ class RecompositionActivity : ComponentActivity() {
}

Marker(
state = singaporeState,
state = markerState,
title = "Marker in Singapore",
onClick = markerClick
)
Expand All @@ -103,7 +101,7 @@ class RecompositionActivity : ComponentActivity() {
Column {
Button(onClick = {
val randomValue = Random.nextInt(3)
location = when (randomValue) {
markerState.position = when (randomValue) {
0 -> singapore
1 -> singapore2
2 -> singapore3
Expand Down
Loading