From 93c397d492a2e45b06b98e3e5028a8645da95fe9 Mon Sep 17 00:00:00 2001 From: Maxime Naturel Date: Mon, 7 Mar 2022 14:55:50 +0100 Subject: [PATCH] Updating location target drawable dynamically --- .../location/LocationSharingFragment.kt | 12 +++------ .../location/LocationSharingViewModel.kt | 27 +++++++++++++------ .../location/LocationSharingViewState.kt | 5 ++-- 3 files changed, 25 insertions(+), 19 deletions(-) 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 4ffe16d738..fb86033065 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 @@ -53,7 +53,6 @@ class LocationSharingFragment @Inject constructor( private var mapView: WeakReference? = null private var hasRenderedUserAvatar = false - private var hasUpdatedPin = false override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentLocationSharingBinding { return FragmentLocationSharingBinding.inflate(inflater, container, false) @@ -125,9 +124,8 @@ class LocationSharingFragment @Inject constructor( override fun invalidate() = withState(viewModel) { state -> updateMap(state) updateUserAvatar(state.userItem) - if (!hasUpdatedPin && state.pinDrawable != null) { - hasUpdatedPin = true - updateStaticPin(state.pinDrawable) + if (state.locationTargetDrawable != null) { + updateLocationTargetPin(state.locationTargetDrawable) } views.shareLocationGpsLoading.isGone = state.lastKnownUserLocation != null } @@ -145,10 +143,8 @@ class LocationSharingFragment @Inject constructor( private fun initOptionsPicker() { // TODO - // create a useCase to compare 2 locations - // update options menu dynamically - // move pin creation into the Fragment? => need to ask other's opinions // reset map to user location when clicking on reset icon + // unit tests // need changes in the event sent when this is a pin drop location? // need changes in the parsing of events when receiving pin drop location?: should it be shown with user avatar or with pin? // set no option at start @@ -196,7 +192,7 @@ class LocationSharingFragment @Inject constructor( } } - private fun updateStaticPin(drawable: Drawable) { + private fun updateLocationTargetPin(drawable: Drawable) { views.shareLocationPin.setImageDrawable(drawable) } } 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 b68c058f13..4c2be8694f 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 @@ -16,6 +16,7 @@ package im.vector.app.features.location +import android.graphics.drawable.Drawable import com.airbnb.mvrx.MavericksViewModelFactory import dagger.assisted.Assisted import dagger.assisted.AssistedFactory @@ -60,7 +61,7 @@ class LocationSharingViewModel @AssistedInject constructor( init { locationTracker.start(this) setUserItem() - createPin() + updatePin() compareTargetAndUserLocation() } @@ -68,13 +69,23 @@ class LocationSharingViewModel @AssistedInject constructor( setState { copy(userItem = session.getUser(session.myUserId)?.toMatrixItem()) } } - private fun createPin() { - locationPinProvider.create(session.myUserId) { - setState { - copy( - pinDrawable = it - ) + private fun updatePin(isUserPin: Boolean? = true) { + if (isUserPin == true) { + locationPinProvider.create(userId = session.myUserId) { + updatePinDrawableInState(it) } + } else { + locationPinProvider.create(userId = null) { + updatePinDrawableInState(it) + } + } + } + + private fun updatePinDrawableInState(drawable: Drawable) { + setState { + copy( + locationTargetDrawable = drawable + ) } } @@ -83,8 +94,8 @@ 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) } } + .onEach { updatePin(isUserPin = it) } .launchIn(viewModelScope) } 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 fb730ce7e8..317bc5a721 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 @@ -33,8 +33,7 @@ data class LocationSharingViewState( val userItem: MatrixItem.UserItem? = null, val areTargetAndUserLocationEqual: Boolean? = null, val lastKnownUserLocation: LocationData? = null, - // TODO move pin drawable creation into the view? - val pinDrawable: Drawable? = null + val locationTargetDrawable: Drawable? = null ) : MavericksState { constructor(locationSharingArgs: LocationSharingArgs) : this( @@ -47,5 +46,5 @@ fun LocationSharingViewState.toMapState() = MapState( zoomOnlyOnce = true, userLocationData = lastKnownUserLocation, pinId = DEFAULT_PIN_ID, - pinDrawable = pinDrawable + pinDrawable = locationTargetDrawable )