diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt index a6b73d7f36..4ffe16d738 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt @@ -147,7 +147,6 @@ class LocationSharingFragment @Inject constructor( // TODO // create a useCase to compare 2 locations // update options menu dynamically - // change the pin dynamically depending on the current chosen location: cf. LocationPinProvider // move pin creation into the Fragment? => need to ask other's opinions // reset map to user location when clicking on reset icon // need changes in the event sent when this is a pin drop location? @@ -168,15 +167,15 @@ class LocationSharingFragment @Inject constructor( private fun updateMap(state: LocationSharingViewState) { // first, update the options view - if (state.areTargetAndUserLocationEqual) { + when (state.areTargetAndUserLocationEqual) { // TODO activate USER_LIVE option when implemented - views.shareLocationOptionsPicker.render( + true -> views.shareLocationOptionsPicker.render( LocationSharingOption.USER_CURRENT ) - } else { - views.shareLocationOptionsPicker.render( + false -> views.shareLocationOptionsPicker.render( LocationSharingOption.PINNED ) + else -> views.shareLocationOptionsPicker.render() } // then, update the map using the height of the options view after it has been rendered views.shareLocationOptionsPicker.post { diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingViewModel.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingViewModel.kt index 3fafbd0519..b68c058f13 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingViewModel.kt @@ -83,14 +83,14 @@ class LocationSharingViewModel @AssistedInject constructor( .sample(TARGET_LOCATION_CHANGE_SAMPLING_PERIOD_IN_MS) .map { compareTargetLocation(it) } .distinctUntilChanged() + // TODO change the pin dynamically depending on the current chosen location: cf. LocationPinProvider .onEach { setState { copy(areTargetAndUserLocationEqual = it) } } .launchIn(viewModelScope) } - private suspend fun compareTargetLocation(targetLocation: LocationData): Boolean { + private suspend fun compareTargetLocation(targetLocation: LocationData): Boolean? { return awaitState().lastKnownUserLocation ?.let { userLocation -> compareLocationsUseCase.execute(userLocation, targetLocation) } - ?: false } override fun onCleared() { diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingViewState.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingViewState.kt index 8faa392416..fb730ce7e8 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingViewState.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingViewState.kt @@ -31,8 +31,7 @@ data class LocationSharingViewState( val roomId: String, val mode: LocationSharingMode, val userItem: MatrixItem.UserItem? = null, - // TODO declare as nullable when we cannot compare? - val areTargetAndUserLocationEqual: Boolean = true, + val areTargetAndUserLocationEqual: Boolean? = null, val lastKnownUserLocation: LocationData? = null, // TODO move pin drawable creation into the view? val pinDrawable: Drawable? = null diff --git a/vector/src/main/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCase.kt b/vector/src/main/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCase.kt index a2a5486923..3c3f1bb8f6 100644 --- a/vector/src/main/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCase.kt @@ -16,11 +16,17 @@ package im.vector.app.features.location.domain.usecase +import com.mapbox.mapboxsdk.geometry.LatLng import im.vector.app.features.location.LocationData import kotlinx.coroutines.withContext import org.matrix.android.sdk.api.session.Session import javax.inject.Inject +/** + * Threshold in meters to consider 2 locations as equal. + */ +private const val SAME_LOCATION_THRESHOLD_IN_METERS = 5 + /** * Use case to check if 2 locations can be considered as equal. */ @@ -35,7 +41,9 @@ class CompareLocationsUseCase @Inject constructor( */ suspend fun execute(location1: LocationData, location2: LocationData): Boolean = withContext(session.coroutineDispatchers.io) { - // TODO implement real comparison - location1 == location2 + val loc1 = LatLng(location1.latitude, location1.longitude) + val loc2 = LatLng(location2.latitude, location2.longitude) + val distance = loc1.distanceTo(loc2) + distance <= SAME_LOCATION_THRESHOLD_IN_METERS } }