Skip to content

Instantly share code, notes, and snippets.

@stakancheck
Last active January 23, 2024 17:44
Show Gist options
  • Save stakancheck/636d4c63bb9944e0e4b228a939c4ace4 to your computer and use it in GitHub Desktop.
Save stakancheck/636d4c63bb9944e0e4b228a939c4ace4 to your computer and use it in GitHub Desktop.
YandexMapView.ios.kt (iosMain)
@OptIn(ExperimentalForeignApi::class)
@Composable
actual fun YandexMap(
modifier: Modifier,
enabled: Boolean,
zoom: Float,
location: LatLng?,
startPosition: LatLng?,
points: List<PointMapModel>,
onPointClick: (id: Long) -> Unit,
customPosition: Boolean,
canSelectPosition: State<Boolean>,
anotherLocationSelected: Boolean,
bottomFocusAreaPadding: Int,
onPositionSelected: (lat: Double, lng: Double) -> Unit,
onDragged: () -> Unit
) {
val yandexMapProtocol = koinInject<YandexMapProtocol>().apply {
addCameraListener(onDragged)
addMapPointListener(onPointClick)
}
LaunchedEffect(canSelectPosition) {
yandexMapProtocol.addMapListener { latitude, longitude ->
if (canSelectPosition.value) {
yandexMapProtocol.updateCustomPoint(
latLng = LatLng(latitude, longitude),
visible = true
)
onPositionSelected(latitude, longitude)
yandexMapProtocol.onMapMove(LatLng(latitude, longitude))
}
}
}
LaunchedEffect(Unit) {
yandexMapProtocol.onMapStart()
location?.let { latLng ->
Napier.d(tag = "YandexMap") { "Update map user location" }
if (customPosition) {
yandexMapProtocol.onMapMove(latLng)
}
yandexMapProtocol.updateMyPoint(latLng)
}
startPosition?.let { latLng ->
yandexMapProtocol.onMapMove(latLng)
if (anotherLocationSelected && canSelectPosition.value) {
yandexMapProtocol.updateCustomPoint(
latLng = latLng
)
}
}
yandexMapProtocol.updateCustomPoint(
visible = anotherLocationSelected
)
}
LaunchedEffect(enabled) {
if (enabled) {
yandexMapProtocol.onMapStart()
} else {
yandexMapProtocol.onMapStop()
}
}
LaunchedEffect(points) {
yandexMapProtocol.updatePointsCollection(points)
}
DisposableEffect(Unit) {
onDispose {
yandexMapProtocol.onMapStop()
}
}
UIKitView(
factory = {
yandexMapProtocol.viewController.view
},
modifier = modifier.fillMaxSize(),
update = {
location?.let { latLng ->
if (customPosition) {
yandexMapProtocol.onMapMove(latLng)
}
yandexMapProtocol.updateMyPoint(latLng = latLng)
}
if (canSelectPosition.value) {
yandexMapProtocol.updateCustomPoint(visible = anotherLocationSelected)
} else {
yandexMapProtocol.updateCustomPoint(visible = false)
}
yandexMapProtocol.setupFocusRect(bottomFocusAreaPadding)
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment