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 ad8054d2ab..cb9f403eff 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 @@ -30,6 +30,9 @@ import im.vector.app.R import im.vector.app.core.extensions.exhaustive import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.databinding.FragmentLocationSharingBinding +import im.vector.app.features.home.AvatarRenderer +import im.vector.app.features.home.room.detail.timeline.helper.MatrixItemColorProvider +import org.matrix.android.sdk.api.util.MatrixItem import java.lang.ref.WeakReference import javax.inject.Inject @@ -37,7 +40,9 @@ import javax.inject.Inject * We should consider using SupportMapFragment for a out of the box lifecycle handling */ class LocationSharingFragment @Inject constructor( - private val urlMapProvider: UrlMapProvider + private val urlMapProvider: UrlMapProvider, + private val avatarRenderer: AvatarRenderer, + private val matrixItemColorProvider: MatrixItemColorProvider ) : VectorBaseFragment() { private val viewModel: LocationSharingViewModel by fragmentViewModel() @@ -45,6 +50,8 @@ class LocationSharingFragment @Inject constructor( // Keep a ref to handle properly the onDestroy callback private var mapView: WeakReference? = null + private var hasRenderedUserAvatar = false + override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentLocationSharingBinding { return FragmentLocationSharingBinding.inflate(inflater, container, false) } @@ -108,6 +115,7 @@ class LocationSharingFragment @Inject constructor( override fun invalidate() = withState(viewModel) { state -> views.mapView.render(state.toMapState()) views.shareLocationGpsLoading.isGone = state.lastKnownLocation != null + updateUserAvatar(state.userItem) } private fun handleLocationNotAvailableError() { @@ -123,7 +131,6 @@ class LocationSharingFragment @Inject constructor( private fun initOptionsPicker() { // TODO - // set avatar and user color for the current user location option // change the options dynamically depending on the current chosen location views.shareLocationOptionsPicker.setOptions(LocationSharingOption.PINNED) views.shareLocationOptionsPicker.optionPinned.debouncedClicks { @@ -136,4 +143,14 @@ class LocationSharingFragment @Inject constructor( // TODO } } + + private fun updateUserAvatar(userItem: MatrixItem.UserItem?) { + userItem?.takeUnless { hasRenderedUserAvatar } + ?.let { + hasRenderedUserAvatar = true + avatarRenderer.render(it, views.shareLocationOptionsPicker.optionUserCurrent.iconView) + val tintColor = matrixItemColorProvider.getColor(it) + views.shareLocationOptionsPicker.optionUserCurrent.setIconBackgroundTint(tintColor) + } + } } 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 f4e1fd0281..989ec255e5 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 @@ -26,6 +26,7 @@ import im.vector.app.core.extensions.exhaustive import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.home.room.detail.timeline.helper.LocationPinProvider import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.util.toMatrixItem class LocationSharingViewModel @AssistedInject constructor( @Assisted private val initialState: LocationSharingViewState, @@ -45,9 +46,14 @@ class LocationSharingViewModel @AssistedInject constructor( init { locationTracker.start(this) + setUserItem() createPin() } + private fun setUserItem() { + setState { copy(userItem = session.getUser(session.myUserId)?.toMatrixItem()) } + } + private fun createPin() { locationPinProvider.create(session.myUserId) { setState { 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 a9a24094eb..e63206f515 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 @@ -20,6 +20,7 @@ import android.graphics.drawable.Drawable import androidx.annotation.StringRes import com.airbnb.mvrx.MavericksState import im.vector.app.R +import org.matrix.android.sdk.api.util.MatrixItem enum class LocationSharingMode(@StringRes val titleRes: Int) { STATIC_SHARING(R.string.location_activity_title_static_sharing), @@ -29,6 +30,7 @@ enum class LocationSharingMode(@StringRes val titleRes: Int) { data class LocationSharingViewState( val roomId: String, val mode: LocationSharingMode, + val userItem: MatrixItem.UserItem? = null, val lastKnownLocation: LocationData? = null, val pinDrawable: Drawable? = null ) : MavericksState { diff --git a/vector/src/main/java/im/vector/app/features/location/view/LocationSharingOptionView.kt b/vector/src/main/java/im/vector/app/features/location/view/LocationSharingOptionView.kt index 95a3ce23ff..b19b2cdc81 100644 --- a/vector/src/main/java/im/vector/app/features/location/view/LocationSharingOptionView.kt +++ b/vector/src/main/java/im/vector/app/features/location/view/LocationSharingOptionView.kt @@ -18,10 +18,10 @@ package im.vector.app.features.location.view import android.content.Context import android.content.res.TypedArray -import android.graphics.drawable.Drawable import android.util.AttributeSet -import android.util.TypedValue import android.view.LayoutInflater +import android.widget.ImageView +import androidx.annotation.ColorInt import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat import androidx.core.graphics.drawable.DrawableCompat @@ -36,6 +36,9 @@ class LocationSharingOptionView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : ConstraintLayout(context, attrs, defStyleAttr) { + val iconView: ImageView + get() = binding.shareLocationOptionIcon + private val binding = ViewLocationSharingOptionBinding.inflate( LayoutInflater.from(context), this @@ -57,11 +60,12 @@ class LocationSharingOptionView @JvmOverloads constructor( } } - fun setIcon(icon: Drawable) { - binding.shareLocationOptionIcon.setImageDrawable(icon) - } - - fun setIconBackground(bkg: Drawable) { + fun setIconBackgroundTint(@ColorInt color: Int) { + val bkg = binding.shareLocationOptionIcon.background?.let { + val backgroundDrawable = DrawableCompat.wrap(binding.shareLocationOptionIcon.background) + DrawableCompat.setTint(backgroundDrawable, color) + backgroundDrawable + } binding.shareLocationOptionIcon.background = bkg } @@ -78,15 +82,15 @@ class LocationSharingOptionView @JvmOverloads constructor( ) val description = typedArray.getString(R.styleable.LocationSharingOptionView_iconDescription) - binding.shareLocationOptionIcon.setImageDrawable(icon) + iconView.setImageDrawable(icon) val bkg = background?.let { val backgroundDrawable = DrawableCompat.wrap(it) DrawableCompat.setTint(backgroundDrawable, backgroundTint) backgroundDrawable - } ?: background - binding.shareLocationOptionIcon.background = bkg - binding.shareLocationOptionIcon.setPadding(padding) - binding.shareLocationOptionIcon.contentDescription = description + } + iconView.background = bkg + iconView.setPadding(padding) + iconView.contentDescription = description } private fun setTitle(typedArray: TypedArray) { diff --git a/vector/src/main/res/layout/view_location_sharing_option.xml b/vector/src/main/res/layout/view_location_sharing_option.xml index 814d6660ba..e1b71ddfaf 100644 --- a/vector/src/main/res/layout/view_location_sharing_option.xml +++ b/vector/src/main/res/layout/view_location_sharing_option.xml @@ -29,7 +29,7 @@ android:contentDescription="@string/a11y_location_share_icon" tools:background="@drawable/circle" tools:backgroundTint="?colorPrimary" - tools:padding="10dp" + tools:padding="11dp" tools:src="@drawable/ic_attachment_location_white" />