-
Notifications
You must be signed in to change notification settings - Fork 6
add viewpointChangedLambda #159
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ package com.arcgismaps.toolkit.geocompose | |
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
|
@@ -30,19 +31,22 @@ import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.viewinterop.AndroidView | ||
import com.arcgismaps.mapping.ArcGISMap | ||
import com.arcgismaps.mapping.view.MapView | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* A compose equivalent of the [MapView]. | ||
* | ||
* @param modifier Modifier to be applied to the Map | ||
* @param arcGISMap the [ArcGISMap] to be rendered by this composable | ||
* @param onViewPointChanged lambda invoked when the viewpoint of the Map has changed | ||
* @param overlay the composable overlays to display on top of the Map. Example, a compass, floorfilter etc. | ||
* @since 200.3.0 | ||
*/ | ||
@Composable | ||
public fun Map( | ||
modifier: Modifier = Modifier, | ||
arcGISMap: ArcGISMap? = null, | ||
onViewPointChanged: (() -> Unit)? = null, | ||
overlay: @Composable () -> Unit = {} | ||
) { | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
|
@@ -69,6 +73,16 @@ public fun Map( | |
mapView.onDestroy(lifecycleOwner) | ||
} | ||
} | ||
|
||
LaunchedEffect(Unit) { | ||
onViewPointChanged?.let { onViewPointChangedLambda -> | ||
launch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
mapView.viewpointChanged.collect { | ||
onViewPointChangedLambda() | ||
} | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should always collect the viewpoint changes on the MapView, regardless of whether a callback parameter was passed or not. We also need to prevent the LaunchedEffect from recomposing when the parameter changes. You can achieve this with rememberUpdatedState. In principle the implementation would look like this: val currentViewPointChanged = rememberUpdatedState(onViewPointChanged)
LaunchedEffect(Unit) {
launch {
mapView.viewpointChanged.collect {
currentViewPointChanged?.let {
it()
}
}
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
} | ||
|
||
@Preview | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if there is a reason this is initialized to be null, but you could initialize it to a default lambda.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaulting the parameter to null is the correct thing to do. It expresses to the user that the parameter is "absent". See Compose API guidelines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
casing: It''s a "Viewpoint" that is changing, not a "ViewPoint", so the parameter name should be
onViewpointChanged
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to consider passing the changed viewpoint to the event callback, so users can immediately process the changed viewpoint. Since there are two types of viewpoints the user might be interested in, we might have to support two different "viewpoint changed events", one passing a viewpoint of type
CenterAndScale
and another passing a viewpoint of typeBoundingGeometry
. We need to think about the naming, but something like this may work:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per our discussion, I have updated the parameter name