Skip to content

Commit

Permalink
fix: [ANDROAPP-6612] Disable GPS state in polygons
Browse files Browse the repository at this point in the history
Signed-off-by: andresmr <[email protected]>
  • Loading branch information
andresmr committed Oct 31, 2024
1 parent efb1080 commit 93790b4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ data class MapSelectorScreenState(
val locationState: LocationState,
val isManualCaptureEnabled: Boolean,
val forcedLocationAccuracy: Int,
val lastGPSLocation: SelectedLocation.GPSResult?,
) {

private fun getDoneButtonEnabledState(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class MapSelectorActivity : AppCompatActivity() {
mapManager.map,
screenState.captureMode,
screenState.mapData.featureCollection,
screenState.lastGPSLocation,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -98,6 +97,7 @@ class MapSelectorViewModel(
locationState = NOT_FIXED,
isManualCaptureEnabled = mapStyleConfig.isManualCaptureEnabled(),
forcedLocationAccuracy = mapStyleConfig.getForcedLocationAccuracy(),
lastGPSLocation = null,
),
)

Expand Down Expand Up @@ -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(
Expand All @@ -138,6 +139,7 @@ class MapSelectorViewModel(
searchOnAreaVisible = searchOnAreaVisible,
displayPolygonInfo = displayPolygonInfo,
locationState = locationState,
lastGPSLocation = lastGPSLocation,
)
}
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
}
}
}

Expand Down

0 comments on commit 93790b4

Please sign in to comment.