Skip to content

Commit

Permalink
Merge pull request #74 from canopas/megh/fix-camera-update-factory-crash
Browse files Browse the repository at this point in the history
Fix camera update factory crash
  • Loading branch information
cp-megh-l authored Aug 22, 2024
2 parents 4482ebe + e2d0c38 commit a68bc11
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ fun LocateOnMapScreen() {
position = CameraPosition.fromLatLngZoom(userLocation, DEFAULT_CAMERA_ZOOM)
}

LaunchedEffect(userLocation) {
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(
userLocation,
DEFAULT_CAMERA_ZOOM
LaunchedEffect(userLocation, state.isMapLoaded) {
if (state.isMapLoaded) {
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(
userLocation,
DEFAULT_CAMERA_ZOOM
)
)
)
}
}

Scaffold(
Expand Down Expand Up @@ -161,17 +163,19 @@ private fun LocateOnMapContent(
)
}
Spacer(modifier = Modifier.height(16.dp))
MapView(Modifier.fillMaxSize(), cameraPositionState, userLocation)
MapView(Modifier.fillMaxSize(), viewModel, cameraPositionState, userLocation)
}
}

@Composable
private fun MapView(
modifier: Modifier,
viewModel: LocateOnMapViewModel,
cameraPositionState: CameraPositionState,
userLocation: LatLng
) {
val scope = rememberCoroutineScope()
val state by viewModel.state.collectAsState()
val relocate by remember {
derivedStateOf {
val distance = cameraPositionState.position.target.distanceTo(userLocation)
Expand Down Expand Up @@ -200,7 +204,10 @@ private fun MapView(
myLocationButtonEnabled = false,
compassEnabled = false,
mapToolbarEnabled = false
)
),
onMapLoaded = {
viewModel.onMapLoaded()
}
)

MapControlBtn(
Expand All @@ -210,13 +217,15 @@ private fun MapView(
icon = R.drawable.ic_relocate,
show = relocate
) {
scope.launch {
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(
userLocation,
DEFAULT_CAMERA_ZOOM
if (state.isMapLoaded) {
scope.launch {
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(
userLocation,
DEFAULT_CAMERA_ZOOM
)
)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,19 @@ class LocateOnMapViewModel @Inject constructor(
)
)
}

fun onMapLoaded() {
viewModelScope.launch(appDispatcher.IO) {
_state.emit(state.value.copy(isMapLoaded = true))
}
}
}

data class LocateOnMapState(
val updatedPlaceName: String = "",
val selectedPlaceName: String? = "",
val defaultLocation: Location? = null,
val addingPlace: Boolean = false,
val isMapLoaded: Boolean = false,
val error: Exception? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private fun EditPlaceContent() {
place.radius,
cameraPositionState,
enabled = state.isAdmin,
viewModel,
viewModel::onPlaceLocationChanged
)

Expand Down Expand Up @@ -426,19 +427,23 @@ private fun MapView(
placeRadius: Double,
cameraPositionState: CameraPositionState,
enabled: Boolean,
viewModel: EditPlaceViewModel,
onPlaceLocationChanged: (LatLng) -> Unit
) {
LaunchedEffect(key1 = cameraPositionState.position.target) {
onPlaceLocationChanged(cameraPositionState.position.target)
}

LaunchedEffect(placeRadius) {
val state by viewModel.state.collectAsState()
LaunchedEffect(placeRadius, state.isMapLoaded) {
snapshotFlow { placeRadius }
.distinctUntilChanged()
.collect {
delay(500)
val newBound = toBounds(cameraPositionState.position.target, it)
cameraPositionState.animate(CameraUpdateFactory.newLatLngBounds(newBound, 50))
if (state.isMapLoaded) {
val newBound = toBounds(cameraPositionState.position.target, it)
cameraPositionState.animate(CameraUpdateFactory.newLatLngBounds(newBound, 50))
}
}
}

Expand All @@ -464,7 +469,10 @@ private fun MapView(
myLocationButtonEnabled = false,
compassEnabled = false,
mapToolbarEnabled = false
)
),
onMapLoaded = {
viewModel.onMapLoaded()
}
)
PlaceMarker(placeRadius, cameraPositionState)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ class EditPlaceViewModel @Inject constructor(
fun resetErrorState() {
_state.value = _state.value.copy(error = null)
}

fun onMapLoaded() {
viewModelScope.launch(appDispatcher.IO) {
_state.emit(state.value.copy(isMapLoaded = true))
}
}
}

data class EditPlaceState(
Expand All @@ -208,5 +214,6 @@ data class EditPlaceState(
val saving: Boolean = false,
val deleting: Boolean = false,
val showDeletePlaceConfirmation: Boolean = false,
val isMapLoaded: Boolean = false,
val error: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,16 @@ fun MapScreen() {
}
}

LaunchedEffect(userLocation) {
val location = state.selectedUser?.location
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(
userLocation,
if (location != null) DEFAULT_CAMERA_ZOOM_FOR_SELECTED_USER else defaultCameraZoom
LaunchedEffect(userLocation, state.isMapLoaded) {
if (state.isMapLoaded) {
val location = state.selectedUser?.location
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(
userLocation,
if (location != null) DEFAULT_CAMERA_ZOOM_FOR_SELECTED_USER else defaultCameraZoom
)
)
)
}
}

Box(modifier = Modifier.fillMaxSize()) {
Expand All @@ -139,12 +141,14 @@ fun MapScreen() {
show = relocate
) {
scope.launch {
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(
userLocation,
DEFAULT_CAMERA_ZOOM
if (state.isMapLoaded) {
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(
userLocation,
DEFAULT_CAMERA_ZOOM
)
)
)
}
}
}
if (state.enabledAddPlaces) {
Expand Down Expand Up @@ -322,7 +326,10 @@ private fun MapView(
myLocationButtonEnabled = false,
compassEnabled = false,
mapToolbarEnabled = false
)
),
onMapLoaded = {
viewModel.onMapLoaded()
}
) {
if (state.members.isNotEmpty()) {
state.members.filter { it.location != null && it.isLocationEnable }.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ class MapViewModel @Inject constructor(
).path
)
}

fun onMapLoaded() {
viewModelScope.launch(appDispatcher.IO) {
_state.emit(_state.value.copy(isMapLoaded = true))
}
}
}

data class MapScreenState(
Expand All @@ -175,5 +181,6 @@ data class MapScreenState(
val showUserDetails: Boolean = false,
val loadingInviteCode: Boolean = false,
val enabledAddPlaces: Boolean = true,
val isMapLoaded: Boolean = false,
val error: Exception? = null
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.canopas.yourspace.ui.flow.journey.components

import android.location.Location
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.platform.LocalContext
Expand All @@ -14,6 +16,7 @@ import com.canopas.yourspace.data.models.location.toRoute
import com.canopas.yourspace.ui.theme.AppTheme
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.Dash
import com.google.android.gms.maps.model.Gap
import com.google.android.gms.maps.model.LatLng
Expand All @@ -40,15 +43,8 @@ fun JourneyMap(
) {
val fromLatLang = LatLng(location?.from_latitude ?: 0.0, location?.from_longitude ?: 0.0)
val toLatLang = LatLng(location?.to_latitude ?: 0.0, location?.to_longitude ?: 0.0)

val fromLocation = Location("").apply {
latitude = fromLatLang.latitude
longitude = fromLatLang.longitude
}

val toLocation = Location("").apply {
latitude = toLatLang.latitude
longitude = toLatLang.longitude
var isMapLoaded by remember {
mutableStateOf(false)
}

val isDarkMode = isSystemInDarkTheme()
Expand All @@ -63,20 +59,27 @@ fun JourneyMap(
)
}

val cameraPositionState = rememberCameraPositionState()
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(fromLatLang, 15f)
}

LaunchedEffect(key1 = location) {
if (location == null) return@LaunchedEffect
val boundsBuilder = LatLngBounds.builder()
.apply {
include(fromLatLang)
location.toRoute().forEach { latLng ->
include(latLng)
}
include(toLatLang)
}.build()
val update = CameraUpdateFactory.newLatLngBounds(boundsBuilder, 50)
cameraPositionState.move(update)
LaunchedEffect(key1 = location, isMapLoaded) {
if (isMapLoaded) {
try {
val boundsBuilder = LatLngBounds.builder()
.apply {
include(fromLatLang)
location?.toRoute()?.forEach { latLng ->
include(latLng)
}
include(toLatLang)
}.build()
val update = CameraUpdateFactory.newLatLngBounds(boundsBuilder, 50)
cameraPositionState.move(update)
} catch (e: Exception) {
e.printStackTrace()
}
}
}

GoogleMap(
Expand All @@ -99,7 +102,10 @@ fun JourneyMap(
zoomGesturesEnabled = gestureEnable,
scrollGesturesEnabledDuringRotateOrZoom = gestureEnable,
indoorLevelPickerEnabled = gestureEnable
)
),
onMapLoaded = {
isMapLoaded = true
}
) {
location?.let {
LocationMarker(fromLatLang, anchor, fromMarkerContent)
Expand Down

0 comments on commit a68bc11

Please sign in to comment.