diff --git a/library/ui-styles/src/main/res/values/stylable_map_tiler_map_view.xml b/library/ui-styles/src/main/res/values/stylable_map_tiler_map_view.xml new file mode 100644 index 0000000000..a7c45918af --- /dev/null +++ b/library/ui-styles/src/main/res/values/stylable_map_tiler_map_view.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + + <declare-styleable name="MapTilerMapView"> + <attr name="showLocateButton" format="boolean" /> + </declare-styleable> + +</resources> 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 dc28c2dba9..8107966de9 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 @@ -67,7 +67,6 @@ class LocationSharingFragment @Inject constructor( lifecycleScope.launchWhenCreated { views.mapView.initialize( url = urlMapProvider.getMapUrl(), - showLocateBtn = true, locationTargetChangeListener = this@LocationSharingFragment ) } diff --git a/vector/src/main/java/im/vector/app/features/location/MapTilerMapView.kt b/vector/src/main/java/im/vector/app/features/location/MapTilerMapView.kt index 1c6a20aadf..5e2c593240 100644 --- a/vector/src/main/java/im/vector/app/features/location/MapTilerMapView.kt +++ b/vector/src/main/java/im/vector/app/features/location/MapTilerMapView.kt @@ -17,6 +17,7 @@ package im.vector.app.features.location import android.content.Context +import android.content.res.TypedArray import android.util.AttributeSet import android.view.Gravity import android.widget.ImageView @@ -55,21 +56,38 @@ class MapTilerMapView @JvmOverloads constructor( val locateBtn by lazy { createLocateBtn() } private var mapRefs: MapRefs? = null private var initZoomDone = false + private var showLocationBtn = false + + init { + context.theme.obtainStyledAttributes( + attrs, + R.styleable.MapTilerMapView, + 0, + 0 + ).run { + try { + setLocateBtnVisibility(this) + } finally { + recycle() + } + } + } + + private fun setLocateBtnVisibility(typedArray: TypedArray) { + showLocationBtn = typedArray.getBoolean(R.styleable.MapTilerMapView_showLocateButton, false) + } /** * For location fragments */ fun initialize( url: String, - showLocateBtn: Boolean = false, // TODO transform into xml attribute locationTargetChangeListener: LocationTargetChangeListener? = null ) { Timber.d("## Location: initialize") getMapAsync { map -> initMapStyle(map, url) - if (showLocateBtn) { - showLocateBtn(map) - } + initLocateBtn(map) notifyLocationOfMapCenter(locationTargetChangeListener) listenCameraMove(map, locationTargetChangeListener) } @@ -87,15 +105,10 @@ class MapTilerMapView @JvmOverloads constructor( } } - private fun listenCameraMove(map: MapboxMap, locationTargetChangeListener: LocationTargetChangeListener?) { - map.addOnCameraMoveListener { - notifyLocationOfMapCenter(locationTargetChangeListener) - } - } - - private fun notifyLocationOfMapCenter(locationTargetChangeListener: LocationTargetChangeListener?) { - getLocationOfMapCenter()?.let { target -> - locationTargetChangeListener?.onLocationTargetChange(target) + private fun initLocateBtn(map: MapboxMap) { + if (showLocationBtn) { + addView(locateBtn) + adjustCompassBtn(map) } } @@ -114,8 +127,7 @@ class MapTilerMapView @JvmOverloads constructor( } } - private fun showLocateBtn(map: MapboxMap) { - addView(locateBtn) + private fun adjustCompassBtn(map: MapboxMap) { locateBtn.post { val marginTop = locateBtn.height + locateBtn.marginTop + locateBtn.marginBottom val marginRight = context.resources.getDimensionPixelOffset(R.dimen.location_sharing_compass_btn_margin_horizontal) @@ -123,6 +135,18 @@ class MapTilerMapView @JvmOverloads constructor( } } + private fun listenCameraMove(map: MapboxMap, locationTargetChangeListener: LocationTargetChangeListener?) { + map.addOnCameraMoveListener { + notifyLocationOfMapCenter(locationTargetChangeListener) + } + } + + private fun notifyLocationOfMapCenter(locationTargetChangeListener: LocationTargetChangeListener?) { + getLocationOfMapCenter()?.let { target -> + locationTargetChangeListener?.onLocationTargetChange(target) + } + } + fun render(state: MapState) { val safeMapRefs = mapRefs ?: return Unit.also { pendingState = state diff --git a/vector/src/main/res/layout/fragment_location_preview.xml b/vector/src/main/res/layout/fragment_location_preview.xml index c2b3bdd739..5499e5b427 100644 --- a/vector/src/main/res/layout/fragment_location_preview.xml +++ b/vector/src/main/res/layout/fragment_location_preview.xml @@ -8,6 +8,7 @@ android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" - app:mapbox_renderTextureMode="true" /> + app:mapbox_renderTextureMode="true" + app:showLocateButton="false" /> -</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file +</androidx.constraintlayout.widget.ConstraintLayout> diff --git a/vector/src/main/res/layout/fragment_location_sharing.xml b/vector/src/main/res/layout/fragment_location_sharing.xml index abc390d1b0..cd15f418ea 100644 --- a/vector/src/main/res/layout/fragment_location_sharing.xml +++ b/vector/src/main/res/layout/fragment_location_sharing.xml @@ -14,6 +14,7 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:mapbox_renderTextureMode="true" + app:showLocateButton="true" tools:background="#4F00" /> <ImageView