Skip to content

Commit

Permalink
Replace minClusterSize param with a more configurable `onClusterMan…
Browse files Browse the repository at this point in the history
…ager`.

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 Dec 13, 2023
1 parent 9be83e5 commit 3871375
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.ui.unit.sp
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.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 @@ -143,7 +144,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 @@ -165,7 +165,10 @@ private fun CustomUiClustering(items: List<MyItem>) {
)
},
// Optional: Custom rendering for non-clustered items
clusterItemContent = null
clusterItemContent = null,
onClusterManager = { clusterManager ->
(clusterManager.renderer as DefaultClusterRenderer).minClusterSize = 2
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public fun <T : ClusterItem> Clustering(
* @param onClusterItemInfoWindowLongClick a lambda invoked when the user long-clicks the info
* 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.
*
*/
@Composable
@GoogleMapComposable
Expand All @@ -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,23 @@ 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.
*
*/
@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 +178,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 +270,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 +288,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 3871375

Please sign in to comment.