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 all 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ 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.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
Expand All @@ -30,19 +33,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
Expand All @@ -69,6 +75,17 @@ public fun Map(
mapView.onDestroy(lifecycleOwner)
}
}

val currentViewPointChanged by rememberUpdatedState(onViewpointChanged)
LaunchedEffect(Unit) {
launch {
mapView.viewpointChanged.collect {
currentViewPointChanged?.let {
it()
}
}
}
}
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