Remove unnecessary state duplication for viewPoint

This commit is contained in:
Alexander Nozik 2022-09-09 21:23:38 +03:00
parent 48e425bbae
commit 7939677e5a
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
3 changed files with 6 additions and 15 deletions

View File

@ -59,7 +59,7 @@ fun App() {
MapView( MapView(
mapTileProvider = mapTileProvider, mapTileProvider = mapTileProvider,
initialViewPoint = viewPoint,// use null to infer view point from features initialViewPoint = null,// use null to infer view point from features
config = MapViewConfig( config = MapViewConfig(
onViewChange = { centerCoordinates = focus }, onViewChange = { centerCoordinates = focus },
) )

View File

@ -62,7 +62,6 @@ public data class MapViewConfig(
val onViewChange: MapViewPoint.() -> Unit = {}, val onViewChange: MapViewPoint.() -> Unit = {},
val onSelect: (GmcRectangle) -> Unit = {}, val onSelect: (GmcRectangle) -> Unit = {},
val zoomOnSelect: Boolean = true, val zoomOnSelect: Boolean = true,
val resetViewPoint: Boolean = false,
) )
@Composable @Composable

View File

@ -56,16 +56,8 @@ public actual fun MapView(
) { ) {
var canvasSize by remember { mutableStateOf(DpSize(512.dp, 512.dp)) } var canvasSize by remember { mutableStateOf(DpSize(512.dp, 512.dp)) }
var viewPointInternal: MapViewPoint? by remember { var viewPoint: MapViewPoint by remember {
mutableStateOf(null) mutableStateOf(computeViewPoint(canvasSize))
}
if (config.resetViewPoint) {
viewPointInternal = null
}
val viewPoint: MapViewPoint by derivedStateOf {
viewPointInternal ?: computeViewPoint(canvasSize)
} }
val zoom: Int by derivedStateOf { val zoom: Int by derivedStateOf {
@ -153,7 +145,7 @@ public actual fun MapView(
+dragAmount.y.toDp().value / tileScale +dragAmount.y.toDp().value / tileScale
) )
config.onViewChange(newViewPoint) config.onViewChange(newViewPoint)
viewPointInternal = newViewPoint viewPoint = newViewPoint
} }
} }
@ -169,7 +161,7 @@ public actual fun MapView(
val newViewPoint = gmcBox.computeViewPoint(mapTileProvider).invoke(canvasSize) val newViewPoint = gmcBox.computeViewPoint(mapTileProvider).invoke(canvasSize)
config.onViewChange(newViewPoint) config.onViewChange(newViewPoint)
viewPointInternal = newViewPoint viewPoint = newViewPoint
} }
selectRect = null selectRect = null
} }
@ -183,7 +175,7 @@ public actual fun MapView(
val invariant = DpOffset(xPos.toDp(), yPos.toDp()).toGeodetic() val invariant = DpOffset(xPos.toDp(), yPos.toDp()).toGeodetic()
val newViewPoint = viewPoint.zoom(-change.scrollDelta.y.toDouble() * config.zoomSpeed, invariant) val newViewPoint = viewPoint.zoom(-change.scrollDelta.y.toDouble() * config.zoomSpeed, invariant)
config.onViewChange(newViewPoint) config.onViewChange(newViewPoint)
viewPointInternal = newViewPoint viewPoint = newViewPoint
}.fillMaxSize() }.fillMaxSize()