Skip to content

Commit

Permalink
feat: Replace minClusterSize param with a more configurable `onClus…
Browse files Browse the repository at this point in the history
…terManager`.

This allows callers to easily access the default `clusterManager` and renderer that the library sets up, allowing for further configuration beyond just `minClusterSize`, while keeping the number of params of `Composable` to a minimum.
  • Loading branch information
darronschall committed Feb 15, 2024
1 parent a6bf0b3 commit 9a0f38e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.clustering.ClusterItem
import com.google.maps.android.clustering.algo.NonHierarchicalViewBasedAlgorithm
import com.google.maps.android.clustering.view.DefaultClusterRenderer
import com.google.maps.android.compose.clustering.Clustering
import com.google.maps.android.compose.clustering.rememberClusterManager
import com.google.maps.android.compose.clustering.rememberClusterRenderer
Expand Down Expand Up @@ -145,7 +146,6 @@ private fun DefaultClustering(items: List<MyItem>) {
private fun CustomUiClustering(items: List<MyItem>) {
Clustering(
items = items,
minClusterSize = 2,
// Optional: Handle clicks on clusters, cluster items, and cluster item info windows
onClusterClick = {
Log.d(TAG, "Cluster clicked! $it")
Expand All @@ -167,7 +167,11 @@ private fun CustomUiClustering(items: List<MyItem>) {
)
},
// Optional: Custom rendering for non-clustered items
clusterItemContent = null
clusterItemContent = null,
// Optional: Customization hook for clusterManager and renderer when they're ready
onClusterManager = { clusterManager ->
(clusterManager.renderer as DefaultClusterRenderer).minClusterSize = 2
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,20 @@ public fun <T : ClusterItem> Clustering(
) {
Clustering(
items = items,
minClusterSize = null,
onClusterClick = onClusterClick,
onClusterItemClick = onClusterItemClick,
onClusterItemInfoWindowClick = onClusterItemInfoWindowClick,
onClusterItemInfoWindowLongClick = onClusterItemInfoWindowLongClick,
clusterContent = clusterContent,
clusterItemContent = clusterItemContent,
onClusterManager = null,
)
}

/**
* Groups many items on a map based on zoom level.
*
* @param items all items to show
* @param minClusterSize optional minimum number of items needed to form a cluster (default 4)
* @param onClusterClick a lambda invoked when the user clicks a cluster of items
* @param onClusterItemClick a lambda invoked when the user clicks a non-clustered item
* @param onClusterItemInfoWindowClick a lambda invoked when the user clicks the info window of a
Expand All @@ -150,22 +149,24 @@ public fun <T : ClusterItem> Clustering(
* window of a non-clustered item
* @param clusterContent an optional Composable that is rendered for each [Cluster].
* @param clusterItemContent an optional Composable that is rendered for each non-clustered item.
* @param onClusterManager an optional lambda invoked with the clusterManager as a param when the
* clusterManager and renderer are set up, allowing further customization.
*/
@Composable
@GoogleMapComposable
@MapsComposeExperimentalApi
public fun <T : ClusterItem> Clustering(
items: Collection<T>,
minClusterSize: Int? = null,
onClusterClick: (Cluster<T>) -> Boolean = { false },
onClusterItemClick: (T) -> Boolean = { false },
onClusterItemInfoWindowClick: (T) -> Unit = { },
onClusterItemInfoWindowLongClick: (T) -> Unit = { },
clusterContent: @[UiComposable Composable] ((Cluster<T>) -> Unit)? = null,
clusterItemContent: @[UiComposable Composable] ((T) -> Unit)? = null,
onClusterManager: ((ClusterManager<T>) -> Unit)? = null,
) {
val clusterManager = rememberClusterManager<T>()
val renderer = rememberClusterRenderer(clusterContent, clusterItemContent, clusterManager, minClusterSize)
val renderer = rememberClusterRenderer(clusterContent, clusterItemContent, clusterManager)
SideEffect {
if (clusterManager?.renderer != renderer) {
clusterManager?.renderer = renderer ?: return@SideEffect
Expand All @@ -178,6 +179,8 @@ public fun <T : ClusterItem> Clustering(
clusterManager.setOnClusterItemClickListener(onClusterItemClick)
clusterManager.setOnClusterItemInfoWindowClickListener(onClusterItemInfoWindowClick)
clusterManager.setOnClusterItemInfoWindowLongClickListener(onClusterItemInfoWindowLongClick)

onClusterManager?.invoke(clusterManager)
}

if (clusterManager != null) {
Expand Down Expand Up @@ -268,7 +271,6 @@ public fun <T : ClusterItem> rememberClusterRenderer(
clusterContent: @Composable ((Cluster<T>) -> Unit)?,
clusterItemContent: @Composable ((T) -> Unit)?,
clusterManager: ClusterManager<T>?,
minClusterSize: Int? = null,
): ClusterRenderer<T>? {
val clusterContentState = rememberUpdatedState(clusterContent)
val clusterItemContentState = rememberUpdatedState(clusterItemContent)
Expand All @@ -287,9 +289,6 @@ public fun <T : ClusterItem> rememberClusterRenderer(
clusterContentState,
clusterItemContentState,
)
minClusterSize?.let {
renderer.minClusterSize = minClusterSize
}
clusterRendererState.value = renderer
}
return clusterRendererState.value
Expand Down

0 comments on commit 9a0f38e

Please sign in to comment.