diff --git a/dhis2_android_maps/src/main/java/org/dhis2/maps/camera/MapSelectorZoomHandler.kt b/dhis2_android_maps/src/main/java/org/dhis2/maps/camera/MapSelectorZoomHandler.kt index 584c1d18b4a..cb056e2e016 100644 --- a/dhis2_android_maps/src/main/java/org/dhis2/maps/camera/MapSelectorZoomHandler.kt +++ b/dhis2_android_maps/src/main/java/org/dhis2/maps/camera/MapSelectorZoomHandler.kt @@ -16,6 +16,7 @@ import org.dhis2.maps.views.MapSelectorViewModel.CaptureMode.MANUAL import org.dhis2.maps.views.MapSelectorViewModel.CaptureMode.MANUAL_SWIPE import org.dhis2.maps.views.MapSelectorViewModel.CaptureMode.NONE import org.dhis2.maps.views.MapSelectorViewModel.CaptureMode.SEARCH +import org.dhis2.maps.views.SelectedLocation const val INITIAL_ZOOM_LEVEL = 13.0 const val SEARCH_ZOOM_LEVEL = 15.0 @@ -29,13 +30,14 @@ object MapSelectorZoomHandler { map: MapboxMap?, captureMode: MapSelectorViewModel.CaptureMode, featureCollection: FeatureCollection, + lastGPSLocation: SelectedLocation.GPSResult?, ) { val selectedFeature = getSelectedFeature(featureCollection) val cameraUpdate = when (captureMode) { NONE -> selectedFeature?.let { initialZoomWithSelectedFeature(it) - } ?: initialZoomWithNoSelection() + } ?: initialZoomWithNoSelection(lastGPSLocation) GPS -> selectedFeature?.let { gpsZoom(it) } MANUAL -> null @@ -79,7 +81,13 @@ object MapSelectorZoomHandler { private fun initialZoomWithSelectedFeature(selectedFeature: Feature) = buildCameraUpdate(selectedFeature) - private fun initialZoomWithNoSelection() = null + private fun initialZoomWithNoSelection(lastGPSLocation: SelectedLocation.GPSResult?) = + lastGPSLocation?.asLatLng()?.let { + CameraUpdateFactory.newLatLngZoom( + latLng = lastGPSLocation.asLatLng(), + zoom = INITIAL_ZOOM_LEVEL, + ) + } private fun gpsZoom(selectedFeature: Feature) = buildCameraUpdate(selectedFeature, GPS_ZOOM_LEVEL) diff --git a/dhis2_android_maps/src/main/java/org/dhis2/maps/model/MapSelectorScreenState.kt b/dhis2_android_maps/src/main/java/org/dhis2/maps/model/MapSelectorScreenState.kt index 7c9d32184f7..83043679919 100644 --- a/dhis2_android_maps/src/main/java/org/dhis2/maps/model/MapSelectorScreenState.kt +++ b/dhis2_android_maps/src/main/java/org/dhis2/maps/model/MapSelectorScreenState.kt @@ -17,6 +17,7 @@ data class MapSelectorScreenState( val locationState: LocationState, val isManualCaptureEnabled: Boolean, val forcedLocationAccuracy: Int, + val lastGPSLocation: SelectedLocation.GPSResult?, ) { private fun getDoneButtonEnabledState(): Boolean { diff --git a/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorActivity.kt b/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorActivity.kt index 32a30eb9ffb..26040958919 100644 --- a/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorActivity.kt +++ b/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorActivity.kt @@ -201,6 +201,7 @@ class MapSelectorActivity : AppCompatActivity() { mapManager.map, screenState.captureMode, screenState.mapData.featureCollection, + screenState.lastGPSLocation, ) } diff --git a/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorScreen.kt b/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorScreen.kt index 67d9695d533..ee6c8f25f2c 100644 --- a/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorScreen.kt +++ b/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorScreen.kt @@ -431,10 +431,12 @@ private fun Map( SwipeToChangeLocationInfo(modifier = Modifier.weight(1f)) } - LocationIcon( - locationState = locationState, - onLocationButtonClicked = onMyLocationButtonClicked, - ) + if (!isPolygonMode) { + LocationIcon( + locationState = locationState, + onLocationButtonClicked = onMyLocationButtonClicked, + ) + } } }, map = { diff --git a/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorViewModel.kt b/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorViewModel.kt index 4e315b048c3..1d7f271188b 100644 --- a/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorViewModel.kt +++ b/dhis2_android_maps/src/main/java/org/dhis2/maps/views/MapSelectorViewModel.kt @@ -77,7 +77,6 @@ class MapSelectorViewModel( SelectedLocation.None() } - private var _lastGPSLocation: SelectedLocation? = null private var _currentFeature: Feature? = initialGeometry?.let { Feature.fromGeometry(it) } private var _currentVisibleRegion: AvailableLatLngBounds? = null private var searchRegion: AvailableLatLngBounds? = null @@ -98,6 +97,7 @@ class MapSelectorViewModel( locationState = NOT_FIXED, isManualCaptureEnabled = mapStyleConfig.isManualCaptureEnabled(), forcedLocationAccuracy = mapStyleConfig.getForcedLocationAccuracy(), + lastGPSLocation = null, ), ) @@ -127,6 +127,7 @@ class MapSelectorViewModel( searchOnAreaVisible: Boolean = _screenState.value.searchOnAreaVisible, displayPolygonInfo: Boolean = _screenState.value.displayPolygonInfo, locationState: LocationState = _screenState.value.locationState, + lastGPSLocation: SelectedLocation.GPSResult? = _screenState.value.lastGPSLocation, ) { _screenState.update { it.copy( @@ -138,6 +139,7 @@ class MapSelectorViewModel( searchOnAreaVisible = searchOnAreaVisible, displayPolygonInfo = displayPolygonInfo, locationState = locationState, + lastGPSLocation = lastGPSLocation, ) } } @@ -148,19 +150,6 @@ class MapSelectorViewModel( when (featureType) { FeatureType.POINT -> Feature.fromGeometry(newPoint) - FeatureType.POLYGON -> { - val coordinates = - (_currentFeature?.geometry() as Polygon?)?.coordinates() - ?.map { points -> - val newList = points.toMutableList() - newList.add(newPoint) - newList - } ?: listOf(listOf(newPoint)) - Feature.fromGeometry( - Polygon.fromLngLats(coordinates), - ) - } - else -> { null } @@ -176,7 +165,9 @@ class MapSelectorViewModel( fun onNewLocation(gpsResult: SelectedLocation.GPSResult) { viewModelScope.launch(dispatchers.io()) { - _lastGPSLocation = gpsResult + updateScreenState( + lastGPSLocation = gpsResult, + ) if (_screenState.value.canCaptureGps(gpsResult.accuracy)) { _currentFeature = updateSelectedGeometry(gpsResult) val mapData = when { @@ -394,14 +385,14 @@ class MapSelectorViewModel( fun onMyLocationButtonClick() { viewModelScope.launch(dispatchers.io()) { - _currentFeature = _lastGPSLocation?.let { updateSelectedGeometry(it) } + _currentFeature = _screenState.value.lastGPSLocation?.let { updateSelectedGeometry(it) } updateScreenState( mapData = GetMapData( _currentFeature, _screenState.value.locationItems, CaptureMode.GPS, ), - selectedLocation = _lastGPSLocation ?: SelectedLocation.None(), + selectedLocation = _screenState.value.lastGPSLocation ?: SelectedLocation.None(), captureMode = CaptureMode.GPS, locationState = FIXED, ) @@ -463,23 +454,31 @@ class MapSelectorViewModel( } fun updateLocationState(locationState: LocationState) { - if (locationState == FIXED && _lastGPSLocation != null) { - _currentFeature = updateSelectedGeometry(_lastGPSLocation!!) - updateScreenState( - mapData = GetMapData( - _currentFeature, - _screenState.value.locationItems, - CaptureMode.GPS, - ), - selectedLocation = _lastGPSLocation ?: _screenState.value.selectedLocation, - captureMode = CaptureMode.GPS, - locationState = locationState, - ) - } else { - updateScreenState( - captureMode = if (locationState == FIXED) CaptureMode.GPS else _screenState.value.captureMode, - locationState = locationState, - ) + when { + _screenState.value.displayPolygonInfo -> { + // Location updates not available in polygons + } + + locationState == FIXED && _screenState.value.lastGPSLocation != null -> { + _currentFeature = updateSelectedGeometry(_screenState.value.lastGPSLocation!!) + updateScreenState( + mapData = GetMapData( + _currentFeature, + _screenState.value.locationItems, + CaptureMode.GPS, + ), + selectedLocation = _screenState.value.lastGPSLocation!!, // Use !! since it's not null + captureMode = CaptureMode.GPS, + locationState = locationState, + ) + } + + else -> { + updateScreenState( + captureMode = if (locationState == FIXED) CaptureMode.GPS else _screenState.value.captureMode, + locationState = locationState, + ) + } } }