Skip to content

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

Merged
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Copy link
Collaborator

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.

Suggested change
onViewPointChanged: (() -> Unit)? = null,
onViewPointChanged: () -> Unit = {},

Copy link
Collaborator

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.

Copy link
Collaborator

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 .

Copy link
Collaborator

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 type BoundingGeometry. We need to think about the naming, but something like this may work:

onViewPointChangedForScale: ((Viewpoint) -> Unit)? = null,
onViewPointChangedForVisibleArea: ((Viewpoint) -> Unit)? = null,

Copy link
Collaborator Author

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

overlay: @Composable () -> Unit = {}
) {
val lifecycleOwner = LocalLifecycleOwner.current
Expand All @@ -69,6 +73,16 @@ public fun Map(
mapView.onDestroy(lifecycleOwner)
}
}

LaunchedEffect(Unit) {
onViewPointChanged?.let { onViewPointChangedLambda ->
launch {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LaunchedEffect should create a coroutine scope, so a launch is not needed here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated it as per @gunt0001 's comment.

mapView.viewpointChanged.collect {
onViewPointChangedLambda()
}
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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()
         }
      }
   }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

}

@Preview
Expand Down