Skip to content

Commit

Permalink
fix: [ANDROAPP-6645] Search results are lost when moving or selecting…
Browse files Browse the repository at this point in the history
… another point (#3874)

* Search result are kept until new search is made

* fix: [ANDROAPP-6645] add search swipe and manual state, to manage zoom and pin behaviour correctly

* fix: [ANDROAPP-6645] manage search state and zoom correctly

* Search results are kept and marker animation when selecting search result

---------

Co-authored-by: Xavier Molloy <[email protected]>
  • Loading branch information
ferdyrod and xavimolloy authored Nov 14, 2024
1 parent 27ff180 commit 2a6b92a
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ 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.MapSelectorViewModel.CaptureMode.SEARCH_MANUAL
import org.dhis2.maps.views.MapSelectorViewModel.CaptureMode.SEARCH_PIN_CLICKED
import org.dhis2.maps.views.MapSelectorViewModel.CaptureMode.SEARCH_SWIPE
import org.dhis2.maps.views.SelectedLocation

const val INITIAL_ZOOM_LEVEL = 13.0
Expand All @@ -38,13 +41,13 @@ object MapSelectorZoomHandler {
NONE -> selectedFeature?.let {
initialZoomWithSelectedFeature(it)
} ?: initialZoomWithNoSelection(lastGPSLocation)

GPS -> selectedFeature?.let { gpsZoom(it) }
MANUAL -> null
MANUAL_SWIPE -> null
SEARCH -> selectedFeature?.let {
SEARCH, SEARCH_PIN_CLICKED -> selectedFeature?.let {
searchZoomWithSelectedFeature(it)
} ?: searchZoomWithNoSelection(featureCollection)
}
SEARCH_SWIPE, SEARCH_MANUAL -> null
}

map?.let { mapboxMap ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ data class MapSelectorScreenState(
displayPolygonInfo -> {
(mapData.featureCollection.features()?.size ?: 0) >= 4
}

(captureMode.isSearchSwipe()) -> {
false
}
(captureMode.isSearchManual()) -> {
selectedLocation !is SelectedLocation.None
}
(forcedLocationAccuracy == DEFAULT_FORCED_LOCATION_ACCURACY) -> {
selectedLocation !is SelectedLocation.None && !captureMode.isSwipe()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object GetMapData {

val locationFeatures = locationItems
.filter { it !is LocationItemModel.StoredResult }
.takeIf { captureMode.isSearch() }
.takeIf { captureMode.isSearchMode() }
?.map { locationItem ->
Feature.fromGeometry(
Point.fromLngLat(locationItem.longitude, locationItem.latitude),
Expand Down Expand Up @@ -67,5 +67,8 @@ object GetMapData {
MapSelectorViewModel.CaptureMode.SEARCH -> getBoundingBox.getEnclosingBoundingBox(
(selectedFeatures + locationFeatures).getLatLngPointList(),
)
MapSelectorViewModel.CaptureMode.SEARCH_SWIPE -> null
MapSelectorViewModel.CaptureMode.SEARCH_MANUAL -> null
MapSelectorViewModel.CaptureMode.SEARCH_PIN_CLICKED -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private fun LocationInfoContent(
)
}

(captureMode.isManual() or captureMode.isNone()) && selectedLocation is SelectedLocation.ManualResult -> {
(captureMode.isManual() or captureMode.isNone() || captureMode.isSearchManual()) && selectedLocation is SelectedLocation.ManualResult -> {
LocationItem(
locationItemModel = LocationItemModel.SearchResult(
searchedTitle = stringResource(R.string.selected_location),
Expand All @@ -354,7 +354,7 @@ private fun LocationInfoContent(
) { }
}

captureMode.isSwipe() -> {
captureMode.isSwipe() || captureMode.isSearchSwipe() -> {
LocationItem(
locationItemModel = LocationItemModel.SearchResult(
searchedTitle = stringResource(R.string.drop_to_select),
Expand Down Expand Up @@ -386,7 +386,7 @@ private fun LocationInfoContent(
}
}

captureMode.isSearch() -> {
captureMode.isSearch() || captureMode.isSearchManual() -> {
if (selectedLocation is SelectedLocation.None) {
LocationItem(
locationItemModel = LocationItemModel.SearchResult(
Expand Down Expand Up @@ -583,7 +583,7 @@ private fun DraggableSelectedIcon(
mutableStateOf(0.dp)
}
val iconOffset by animateDpAsState(
if (captureMode.isSwipe()) {
if (captureMode.isSwipe() || captureMode.isSearchSwipe() || captureMode.isSearchPinClicked()) {
(-15).dp
} else {
0.dp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ class MapSelectorViewModel(
MANUAL,
MANUAL_SWIPE,
SEARCH,
SEARCH_SWIPE,
SEARCH_MANUAL,
SEARCH_PIN_CLICKED,
;

fun isNone() = this == NONE
fun isGps() = this == GPS
fun isManual() = this == MANUAL
fun isSwipe() = this == MANUAL_SWIPE
fun isSearch() = this == SEARCH
fun isSearchSwipe() = this == SEARCH_SWIPE
fun isSearchManual() = this == SEARCH_MANUAL
fun isSearchMode() = (this == SEARCH_MANUAL || this == SEARCH_SWIPE || this == SEARCH)
fun isSearchPinClicked() = this == SEARCH_PIN_CLICKED
}

private val initialGeometry =
Expand Down Expand Up @@ -260,7 +267,7 @@ class MapSelectorViewModel(
query: String = _searchLocationQuery.value,
regionToSearch: AvailableLatLngBounds? = _currentVisibleRegion,
) {
if (_screenState.value.captureMode.isSearch()) {
if (_screenState.value.captureMode.isSearch() || _screenState.value.captureMode.isSearchManual()) {
updateScreenState(searching = true)
val filteredPreviousLocation =
searchLocationManager.getAvailableLocations(query)
Expand Down Expand Up @@ -352,6 +359,7 @@ class MapSelectorViewModel(
_screenState.value.captureMode,
),
selectedLocation = selectedLocation,
captureMode = CaptureMode.SEARCH_PIN_CLICKED,
)
}
}
Expand Down Expand Up @@ -415,7 +423,11 @@ class MapSelectorViewModel(

fun onMove(point: LatLng) {
if (canCaptureWithSwipe()) {
val captureMode = if (!_screenState.value.captureMode.isSwipe()) {
val captureMode = if (_screenState.value.captureMode.isSearchMode() ||
_screenState.value.captureMode.isSearchPinClicked()
) {
CaptureMode.SEARCH_SWIPE
} else if (!_screenState.value.captureMode.isSwipe()) {
CaptureMode.MANUAL_SWIPE
} else {
_screenState.value.captureMode
Expand Down Expand Up @@ -450,6 +462,17 @@ class MapSelectorViewModel(
),
captureMode = CaptureMode.MANUAL,
)
} else if (_screenState.value.captureMode.isSearchSwipe() ||
_screenState.value.captureMode.isSearchPinClicked()
) {
updateScreenState(
mapData = GetMapData(
_currentFeature,
screenState.value.locationItems,
CaptureMode.SEARCH_MANUAL,
),
captureMode = CaptureMode.SEARCH_MANUAL,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,63 @@ class MapSelectorViewModelTest {
}
}

@Test
fun shouldSwitchCaptureModeToSearchPinClickedWhenPinClicked() = runTest {
mapSelectorViewModelNoInitialGeometry.screenState.initTest(
given = { givenSearchAction(mapSelectorViewModelNoInitialGeometry) {} },
`when` = {
mapSelectorViewModelNoInitialGeometry.onPinClicked(
Feature.fromGeometry(
Point.fromLngLat(
mockedSearchResult.longitude,
mockedSearchResult.latitude,
),
).also {
it.withPlacesProperties(
title = mockedSearchResult.title,
subtitle = mockedSearchResult.address,
)
},
)
1
},
then = { screenState ->
assertTrue(screenState.captureMode.isSearchPinClicked())
},
)
}

@Test
fun shouldSwitchCaptureModeToSearchManualWhenPinClickedFinishMove() = runTest {
mapSelectorViewModelNoInitialGeometry.screenState.initTest(
given = {
givenSearchAction(mapSelectorViewModelNoInitialGeometry) {}
mapSelectorViewModelNoInitialGeometry.onPinClicked(
Feature.fromGeometry(
Point.fromLngLat(
mockedSearchResult.longitude,
mockedSearchResult.latitude,
),
).also {
it.withPlacesProperties(
title = mockedSearchResult.title,
subtitle = mockedSearchResult.address,
)
},
)
val item = awaitItem()
assertTrue(item.captureMode.isSearchPinClicked())
},
`when` = {
mapSelectorViewModelNoInitialGeometry.onMoveEnd()
1
},
then = { screenState ->
assertTrue(screenState.captureMode.isSearchManual())
},
)
}

private val mockedLocationItemSearchResults = listOf(
LocationItemModel.SearchResult(
searchedTitle = "Search title",
Expand Down

0 comments on commit 2a6b92a

Please sign in to comment.