Compare locations use case implementation

This commit is contained in:
Maxime Naturel 2022-03-07 14:33:59 +01:00
parent 01879e252d
commit 6fc6bf1c7d
4 changed files with 17 additions and 11 deletions

View file

@ -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 {

View file

@ -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() {

View file

@ -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

View file

@ -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
}
}