Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions api/android-sdk-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public final class com.mapconductor.compose.polygon.LocalPolygonCollectorKt {
}
public final class com.mapconductor.compose.polygon.PolygonComponentKt {
public static final void Polygon(com.mapconductor.compose.MapViewScope, com.mapconductor.core.polygon.PolygonState, androidx.compose.runtime.Composer, int);
public static final void Polygons(com.mapconductor.compose.MapViewScope, java.util.List<com.mapconductor.core.polygon.PolygonState>, androidx.compose.runtime.Composer, int);
public static final void Polygon-YoVAMyA(com.mapconductor.compose.MapViewScope, java.util.List<? extends com.mapconductor.core.features.GeoPointInterface>, java.util.List<? extends java.util.List<? extends com.mapconductor.core.features.GeoPointInterface>>, java.lang.String, long, float, long, boolean, int, java.io.Serializable, kotlin.jvm.functions.Function1<? super com.mapconductor.core.polygon.PolygonEvent, kotlin.Unit>, androidx.compose.runtime.Composer, int, int, int);
public static final void Polygon-IvR-a-0(com.mapconductor.compose.MapViewScope, com.mapconductor.core.features.GeoRectBounds, java.lang.String, long, float, long, boolean, int, java.io.Serializable, kotlin.jvm.functions.Function1<? super com.mapconductor.core.polygon.PolygonEvent, kotlin.Unit>, androidx.compose.runtime.Composer, int, int);
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/mapconductor/compose/polygon/PolygonComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.mapconductor.compose.polygon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand All @@ -14,6 +16,9 @@ import com.mapconductor.core.polygon.OnPolygonEventHandler
import com.mapconductor.core.polygon.PolygonState
import com.mapconductor.core.polygon.unionHolesInPlace
import java.io.Serializable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.withContext

@Composable
fun MapViewScope.Polygon(state: PolygonState) {
Expand All @@ -32,6 +37,57 @@ fun MapViewScope.Polygon(state: PolygonState) {
}
}

/**
* Efficiently add many polygons without creating one Composable per polygon.
*
* The same shape as [com.mapconductor.compose.marker.Markers]: one effect diffs
* the list against the ids this call added last time and publishes the result
* in a single collector update.
*/
@Composable
fun MapViewScope.Polygons(states: List<PolygonState>) {
val collector = LocalPolygonCollector.current
val prevIdsState = remember { mutableStateOf<Set<String>>(emptySet()) }

LaunchedEffect(states) {
withContext(Dispatchers.Default) {
val prevIds = prevIdsState.value
val nextIds = states.asSequence().map { it.id }.toSet()
val removedIds = prevIds - nextIds
// Union the holes once per newly added state, as Polygon(state) does.
for (state in states) {
if (state.id !in prevIds && state.holes.size > 1) {
state.unionHolesInPlace()
}
}
prevIdsState.value = nextIds
// Merge only the ids this Polygons() owns, so several
// Polygons()/Polygon() can share one map's collector.
collector.flow.update { current ->
val next = current.toMutableMap()
for (id in removedIds) next.remove(id)
for (state in states) next[state.id] = state
next
}
}
}

DisposableEffect(Unit) {
onDispose {
// Remove only the polygons this Polygons() owns on dispose.
val ownIds = prevIdsState.value
if (ownIds.isNotEmpty()) {
collector.flow.update { current ->
val next = current.toMutableMap()
for (id in ownIds) next.remove(id)
next
}
}
prevIdsState.value = emptySet()
}
}
}

@Composable
fun MapViewScope.Polygon(
points: List<GeoPointInterface>,
Expand Down
Loading