Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.core.content.ContextCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import com.mapconductor.example.navigation.NavigationViewModel
import com.mapconductor.example.pages.circle.CircleMapPage
import com.mapconductor.example.pages.map.flyto.FlyToMapComponent
import com.mapconductor.example.pages.map.flyto.FlyToMapIcons
import com.mapconductor.example.pages.map.flyto.FlyToMapPage
import com.mapconductor.example.pages.polyline.PolylineMapPage
Expand All @@ -32,14 +31,16 @@ fun DemoAppScreen() {
val currentPage by navigationViewModel.currentPage
val isSidebarExpanded by navigationViewModel.isSidebarExpanded
val context = LocalContext.current
val flyToMapPageIcons = remember {
FlyToMapIcons(
honolulu = ContextCompat.getDrawable(context, R.drawable.honolulu)!!,
tokyo = ContextCompat.getDrawable(context, R.drawable.tokyo)!!,
london = ContextCompat.getDrawable(context, R.drawable.london)!!,
newYork = ContextCompat.getDrawable(context, R.drawable.newyork)!!,
)
}
val flyToMapPageIcons =
remember {
FlyToMapIcons(
honolulu = ContextCompat.getDrawable(context, R.drawable.honolulu)!!,
tokyo = ContextCompat.getDrawable(context, R.drawable.tokyo)!!,
london = ContextCompat.getDrawable(context, R.drawable.london)!!,
newYork = ContextCompat.getDrawable(context, R.drawable.newyork)!!,
sydney = ContextCompat.getDrawable(context, R.drawable.sydney)!!,
)
}

val sidebarItems =
listOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
package com.mapconductor.example.pages.map.flyto

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.mapconductor.core.map.MapViewState
import com.mapconductor.core.map.OnMapEventHandler
import com.mapconductor.core.marker.Marker
import com.mapconductor.core.marker.OnMarkerEventHandler
import com.mapconductor.core.marker.MarkerState
import com.mapconductor.core.polyline.Polyline
import com.mapconductor.core.polyline.PolylineState
import com.mapconductor.example.MapViewContainer

@Composable
fun FlyToMapComponent(
mapViewState: MapViewState<*>?,
viewModel: FlyToPageViewModel,
polylines: List<PolylineState>,
markers: List<MarkerState>,
modifier: Modifier = Modifier,
onMapClick: OnMapEventHandler = {},
onMarkerClick: OnMarkerEventHandler = {},
) {
mapViewState?.let { state ->
MapViewContainer(
modifier = modifier,
state = state,
onMapClick = onMapClick,
onMarkerClick = onMarkerClick,
) {
// Render polylines connecting all markers
viewModel.polylines.forEach { polyline ->
polylines.forEach { polyline ->
key(polyline.id) {
Polyline(polyline)
}
}

// Render markers for fly to destinations
viewModel.markers.forEach { marker ->
markers.forEach { marker ->
key(marker.id) {
Marker(marker)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ data class FlyToMapIcons(
val tokyo: Drawable,
val newYork: Drawable,
val london: Drawable,
val sydney: Drawable,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,45 @@ import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import com.mapconductor.example.ui.DemoMapPageScaffold
import com.mapconductor.example.ui.MessageCard
import android.graphics.drawable.Drawable

@Composable
fun FlyToMapPage(
icons: FlyToMapIcons,
viewModel: FlyToPageViewModel = FlyToPageViewModelImpl(icons),
onToggleSidebar: () -> Unit = {},
) {
val viewModel: FlyToPageViewModel =
viewModel<FlyToPageViewModelImpl>(
factory =
object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(FlyToPageViewModelImpl::class.java)) {
@Suppress("UNCHECKED_CAST")
return FlyToPageViewModelImpl(icons) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
},
)

DemoMapPageScaffold(
initCameraPosition = viewModel.initCameraPosition,
onToggleSidebar = onToggleSidebar,
Expand All @@ -35,8 +56,8 @@ fun FlyToMapPage(

FlyToMapComponent(
mapViewState = mapViewState.value,
viewModel = viewModel,
onMapClick = viewModel::onMapClick,
polylines = viewModel.polylines,
markers = viewModel.markers,
)

// Control Panel
Expand All @@ -50,44 +71,86 @@ fun FlyToMapPage(
end = paddingValues.calculateEndPadding(LayoutDirection.Ltr) + 16.dp,
),
title = "Fly To Controls",
maxHeight = 200.dp,
) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp)
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp)
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Row(
modifier = Modifier.weight(1f),
verticalAlignment = Alignment.CenterVertically,
) {
Switch(
checked = viewModel.geodesic,
onCheckedChange = {
viewModel.geodesic = !viewModel.geodesic
},
thumbContent =
if (viewModel.geodesic) {
{
Icon(
imageVector = Icons.Filled.Check,
contentDescription = null,
modifier = Modifier.size(SwitchDefaults.IconSize),
)
}
} else {
null
},
)
Text(
text = "geodesic",
modifier =
Modifier
.align(Alignment.CenterVertically)
.padding(16.dp),
)
}
Button(
modifier = Modifier.weight(1f),
onClick = { viewModel.flyToSydney() },
) {
Text("Sydney")
}
}
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Button(
onClick = { viewModel.flyToHonolulu() },
modifier = Modifier.weight(1f)
modifier = Modifier.weight(1f),
) {
Text("Honolulu")
}

Button(
onClick = { viewModel.flyToTokyo() },
modifier = Modifier.weight(1f)
modifier = Modifier.weight(1f),
) {
Text("Tokyo")
}
}

Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp)
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Button(
onClick = { viewModel.flyToLondon() },
modifier = Modifier.weight(1f)
modifier = Modifier.weight(1f),
) {
Text("London")
}

Button(
onClick = { viewModel.flyToNewYork() },
modifier = Modifier.weight(1f)
modifier = Modifier.weight(1f),
) {
Text("New York")
}
Expand Down
Loading
Loading