Zaki Shaikh
01/10/2023, 2:11 PMZaki Shaikh
01/10/2023, 2:11 PM@Composable
fun MapViewFragmentScreen(
viewModel: SignUpAndEditProfileViewModel,
viewLand: Land?,
onDone: () -> Unit
) {
val context = LocalContext.current
val spacing = LocalSpacing.current
val shape = LocalCustomShapes.current.mediumShape
val cameraPositionState = rememberCameraPositionState {
if (viewModel.coordinates.value.isNotEmpty()) {
viewModel.coordinates.value.getCenterCoordinate()?.let {
position = CameraPosition.fromLatLngZoom(it, MAP_ZOOM_FACTOR)
}
} else {
DataHolder.getInstance().lastKnownLocation?.let {
position = CameraPosition.fromLatLngZoom(
LatLng(it.latitude, it.longitude),
MAP_ZOOM_FACTOR
)
}
}
}
val uiSettings by remember {
mutableStateOf(
MapUiSettings(
myLocationButtonEnabled = true,
)
)
}
val properties by remember {
mutableStateOf(MapProperties(mapType = MapType.NORMAL, isMyLocationEnabled = true))
}
var isAddingBoundary by remember { mutableStateOf(false) }
Box(modifier = Modifier.fillMaxSize()) {
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState,
properties = properties,
uiSettings = uiSettings,
onMapLoaded = {
moveMapToLocation(viewLand, cameraPositionState, viewModel)
},
onMapClick = {
if (isAddingBoundary) {
viewModel.coordinates.postValue(viewModel.coordinates.value + it)
}
}
) {
DrawLand(
getCoordinates = { viewModel.coordinates.value },
center = viewModel.coordinates.value.getCenterCoordinate(),
getFarmName = { viewModel.farmName.value }
)
/*viewModel.lands.forEach { land ->
land.coordinates?.map { LatLng(it.latitude, it.longitude) }?.let {
DrawLand(
getCoordinates = { it },
center = it.getCenterCoordinate(),
getFarmName = { land.landName ?: "N/A" }
)
}
}*/
}
Column(
modifier = Modifier
.wrapContentWidth()
.align(Alignment.BottomStart)
.padding(spacing.medium)
.border(width = .3.dp, color = Color.White, shape = shape)
.background(color = Color.OrangePeel.copy(alpha = .1f), shape = shape)
.padding(spacing.small),
verticalArrangement = Arrangement.spacedBy(spacing.extraSmall)
) {
MapButton(
iconId = if (isAddingBoundary) R.drawable.ic_close else R.drawable.ic_add_boundary
) {
if (!isAddingBoundary) {
viewModel.coordinates.clear()
viewModel.farmLocation = null
context.showToast(R.string.select_your_land)
}
isAddingBoundary = !isAddingBoundary
}
if (viewModel.coordinates.value.isNotEmpty() && isAddingBoundary) {
MapButton(
iconId = R.drawable.ic_clear_point
) {
viewModel.coordinates.postValue(
viewModel.coordinates.value - viewModel.coordinates.value.last()
)
}
}
MapButton(
iconId = R.drawable.ic_done,
onClick = {
if (viewModel.coordinates.value.size > 2) {
viewModel.farmLocation =
viewModel.coordinates.value.getCenterCoordinate()
?.getLocationName(context)
onDone()
} else {
context.showToast(R.string.farm_least_points_msg)
}
}
)
}
}
}
Zaki Shaikh
01/10/2023, 2:12 PM@Composable
fun DrawLand(
getCoordinates: () -> List<LatLng>,
center: LatLng?,
getFarmName: () -> String
) {
val context = LocalContext.current
val marker = bitmapDescriptor(context, R.mipmap.ic_sign_up_location_field_foreground)
val circle = bitmapDescriptor(context, R.drawable.ic_circle, 20)
getCoordinates().let { coordinates ->
if (coordinates.isNotEmpty()) {
Polygon(
points = getCoordinates(),
fillColor = Color.Green.copy(alpha = .2f),
strokeColor = Color.Cameron,
strokeJointType = JointType.BEVEL,
strokeWidth = 5f
)
center?.let {
Marker(
position = it,
title = getFarmName(),
snippet = it.getLocationName(context),
icon = marker
)
}
getCoordinates().forEach { position ->
Marker(
position = position,
title = getFarmName(),
snippet = position.getLocationName(context),
icon = circle
)
}
}
}
}
myanmarking
01/10/2023, 3:36 PMZaki Shaikh
01/10/2023, 3:50 PMmyanmarking
01/10/2023, 3:51 PM@Stable
@Keep
data class ImmutableList<out T>(val list: List<T>) : List<T> by list
myanmarking
01/10/2023, 3:51 PMmyanmarking
01/10/2023, 3:51 PMZaki Shaikh
01/10/2023, 3:52 PMmyanmarking
01/10/2023, 3:52 PMZaki Shaikh
01/10/2023, 3:53 PMmyanmarking
01/10/2023, 3:56 PMColton Idle
01/12/2023, 12:38 AMZaki Shaikh
01/12/2023, 5:55 AM