-
Notifications
You must be signed in to change notification settings - Fork 135
/
Clustering.kt
331 lines (313 loc) · 13 KB
/
Clustering.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package com.google.maps.android.compose.clustering
import android.os.Handler
import android.os.Looper
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.UiComposable
import androidx.compose.ui.platform.LocalContext
import com.google.android.gms.maps.GoogleMap
import com.google.maps.android.clustering.Cluster
import com.google.maps.android.clustering.ClusterItem
import com.google.maps.android.clustering.ClusterManager
import com.google.maps.android.clustering.view.ClusterRenderer
import com.google.maps.android.clustering.view.DefaultClusterRenderer
import com.google.maps.android.collections.MarkerManager
import com.google.maps.android.compose.GoogleMapComposable
import com.google.maps.android.compose.InputHandler
import com.google.maps.android.compose.MapEffect
import com.google.maps.android.compose.MapsComposeExperimentalApi
import com.google.maps.android.compose.currentCameraPositionState
import com.google.maps.android.compose.rememberComposeUiViewRenderer
import com.google.maps.android.compose.rememberReattachClickListenersHandle
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.launch
/**
* Groups many items on a map based on zoom level.
*
* @param items all items to show
* @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
* non-clustered item
* @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.
* @param clusterRenderer an optional ClusterRenderer that can be used to specify the algorithm used by the rendering.
*/
@Composable
@GoogleMapComposable
@MapsComposeExperimentalApi
@Deprecated(
message = "If clusterRenderer is specified, clusterContent and clusterItemContent are not used; use a function that takes ClusterManager as an argument instead.",
replaceWith = ReplaceWith(
expression = """
val clusterManager = rememberClusterManager<T>()
LaunchedEffect(clusterManager, clusterRenderer) {
clusterManager?.renderer = clusterRenderer
}
SideEffect {
clusterManager ?: return@SideEffect
clusterManager.setOnClusterClickListener(onClusterClick)
clusterManager.setOnClusterItemClickListener(onClusterItemClick)
clusterManager.setOnClusterItemInfoWindowClickListener(onClusterItemInfoWindowClick)
clusterManager.setOnClusterItemInfoWindowLongClickListener(onClusterItemInfoWindowLongClick)
}
if (clusterManager != null) {
Clustering(
items = items,
clusterManager = clusterManager,
)
}
""",
imports = [
"com.google.maps.android.compose.clustering.Clustering",
"androidx.compose.runtime.SideEffect",
"com.google.maps.android.clustering.ClusterManager",
],
),
)
public fun <T : ClusterItem> Clustering(
items: Collection<T>,
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,
clusterRenderer: ClusterRenderer<T>? = null,
) {
val clusterManager = rememberClusterManager(clusterContent, clusterItemContent, clusterRenderer)
?: return
SideEffect {
clusterManager.setOnClusterClickListener(onClusterClick)
clusterManager.setOnClusterItemClickListener(onClusterItemClick)
clusterManager.setOnClusterItemInfoWindowClickListener(onClusterItemInfoWindowClick)
clusterManager.setOnClusterItemInfoWindowLongClickListener(onClusterItemInfoWindowLongClick)
}
Clustering(
items = items,
clusterManager = clusterManager,
)
}
/**
* Groups many items on a map based on zoom level.
*
* @param items all items to show
* @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
* non-clustered item
* @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
@MapsComposeExperimentalApi
public fun <T : ClusterItem> Clustering(
items: Collection<T>,
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,
) {
val clusterManager = rememberClusterManager<T>()
val renderer = rememberClusterRenderer(clusterContent, clusterItemContent, clusterManager)
SideEffect {
if (clusterManager?.renderer != renderer) {
clusterManager?.renderer = renderer ?: return@SideEffect
}
}
SideEffect {
clusterManager ?: return@SideEffect
clusterManager.setOnClusterClickListener(onClusterClick)
clusterManager.setOnClusterItemClickListener(onClusterItemClick)
clusterManager.setOnClusterItemInfoWindowClickListener(onClusterItemInfoWindowClick)
clusterManager.setOnClusterItemInfoWindowLongClickListener(onClusterItemInfoWindowLongClick)
}
if (clusterManager != null) {
Clustering(
items = items,
clusterManager = clusterManager,
)
}
}
/**
* Groups many items on a map based on clusterManager.
*
* @param items all items to show
* @param clusterManager a [ClusterManager] that can be used to specify the algorithm used by the rendering.
*/
@Composable
@GoogleMapComposable
@MapsComposeExperimentalApi
public fun <T : ClusterItem> Clustering(
items: Collection<T>,
clusterManager: ClusterManager<T>,
) {
ResetMapListeners(clusterManager)
InputHandler(
>
>
>
>
>
>
)
val cameraPositionState = currentCameraPositionState
LaunchedEffect(cameraPositionState) {
snapshotFlow { cameraPositionState.isMoving }
.collect { isMoving ->
if (!isMoving) {
clusterManager.onCameraIdle()
}
}
}
val itemsState = rememberUpdatedState(items)
LaunchedEffect(itemsState) {
snapshotFlow { itemsState.value.toList() }
.collect { items ->
clusterManager.clearItems()
clusterManager.addItems(items)
clusterManager.cluster()
}
}
DisposableEffect(itemsState) {
onDispose {
clusterManager.clearItems()
clusterManager.cluster()
}
}
}
@Composable
@GoogleMapComposable
@MapsComposeExperimentalApi
public fun <T : ClusterItem> rememberClusterRenderer(
clusterManager: ClusterManager<T>?,
): ClusterRenderer<T>? {
val context = LocalContext.current
val clusterRendererState: MutableState<ClusterRenderer<T>?> = remember { mutableStateOf(null) }
clusterManager ?: return null
MapEffect(context) { map ->
val renderer = DefaultClusterRenderer(context, map, clusterManager)
clusterRendererState.value = renderer
}
return clusterRendererState.value
}
/**
* Default Renderer for drawing Composable.
*
* @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> rememberClusterRenderer(
clusterContent: @Composable ((Cluster<T>) -> Unit)?,
clusterItemContent: @Composable ((T) -> Unit)?,
clusterManager: ClusterManager<T>?,
): ClusterRenderer<T>? {
val clusterContentState = rememberUpdatedState(clusterContent)
val clusterItemContentState = rememberUpdatedState(clusterItemContent)
val context = LocalContext.current
val viewRendererState = rememberUpdatedState(rememberComposeUiViewRenderer())
val clusterRendererState: MutableState<ClusterRenderer<T>?> = remember { mutableStateOf(null) }
clusterManager ?: return null
MapEffect(context) { map ->
val renderer = ComposeUiClusterRenderer(
context,
scope = this,
map,
clusterManager,
viewRendererState,
clusterContentState,
clusterItemContentState,
)
clusterRendererState.value = renderer
awaitCancellation()
}
return clusterRendererState.value
}
@Composable
@GoogleMapComposable
@MapsComposeExperimentalApi
public fun <T : ClusterItem> rememberClusterManager(): ClusterManager<T>? {
val context = LocalContext.current
val clusterManagerState: MutableState<ClusterManager<T>?> = remember { mutableStateOf(null) }
MapEffect(context) { map ->
clusterManagerState.value = ClusterManager<T>(context, map)
}
return clusterManagerState.value
}
@OptIn(MapsComposeExperimentalApi::class)
@Composable
private fun <T : ClusterItem> rememberClusterManager(
clusterContent: @Composable ((Cluster<T>) -> Unit)?,
clusterItemContent: @Composable ((T) -> Unit)?,
clusterRenderer: ClusterRenderer<T>? = null,
): ClusterManager<T>? {
val clusterContentState = rememberUpdatedState(clusterContent)
val clusterItemContentState = rememberUpdatedState(clusterItemContent)
val context = LocalContext.current
val viewRendererState = rememberUpdatedState(rememberComposeUiViewRenderer())
val clusterManagerState: MutableState<ClusterManager<T>?> = remember { mutableStateOf(null) }
MapEffect(context) { map ->
val clusterManager = ClusterManager<T>(context, map)
launch {
snapshotFlow {
clusterContentState.value != null || clusterItemContentState.value != null
}
.collect { hasCustomContent ->
val renderer = clusterRenderer
?: if (hasCustomContent) {
ComposeUiClusterRenderer(
context,
scope = this,
map,
clusterManager,
viewRendererState,
clusterContentState,
clusterItemContentState,
)
} else {
DefaultClusterRenderer(context, map, clusterManager)
}
clusterManager.renderer = renderer
}
}
clusterManagerState.value = clusterManager
}
return clusterManagerState.value
}
/**
* This is a hack.
* [ClusterManager] instantiates a [MarkerManager], which posts a runnable to the UI thread that
* overwrites a bunch of [GoogleMap]'s listeners. Many Maps composables rely on those listeners
* being set by [com.google.maps.android.compose.MapApplier].
* This posts _another_ runnable which effectively undoes that, signaling MapApplier to set the
* listeners again.
* This is heavily coupled to implementation details of [MarkerManager].
*/
@Composable
private fun ResetMapListeners(
clusterManager: ClusterManager<*>,
) {
val reattach = rememberReattachClickListenersHandle()
LaunchedEffect(clusterManager, reattach) {
Handler(Looper.getMainLooper()).post {
reattach()
}
}
}
You can’t perform that action at this time.