Using style attibute for locate button visibility

This commit is contained in:
Maxime Naturel 2022-03-08 14:11:11 +01:00
parent 40e92842ea
commit 01aff36597
5 changed files with 51 additions and 18 deletions

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MapTilerMapView">
<attr name="showLocateButton" format="boolean" />
</declare-styleable>
</resources>

View File

@ -67,7 +67,6 @@ class LocationSharingFragment @Inject constructor(
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
views.mapView.initialize( views.mapView.initialize(
url = urlMapProvider.getMapUrl(), url = urlMapProvider.getMapUrl(),
showLocateBtn = true,
locationTargetChangeListener = this@LocationSharingFragment locationTargetChangeListener = this@LocationSharingFragment
) )
} }

View File

@ -17,6 +17,7 @@
package im.vector.app.features.location package im.vector.app.features.location
import android.content.Context import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet import android.util.AttributeSet
import android.view.Gravity import android.view.Gravity
import android.widget.ImageView import android.widget.ImageView
@ -55,21 +56,38 @@ class MapTilerMapView @JvmOverloads constructor(
val locateBtn by lazy { createLocateBtn() } val locateBtn by lazy { createLocateBtn() }
private var mapRefs: MapRefs? = null private var mapRefs: MapRefs? = null
private var initZoomDone = false 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 * For location fragments
*/ */
fun initialize( fun initialize(
url: String, url: String,
showLocateBtn: Boolean = false, // TODO transform into xml attribute
locationTargetChangeListener: LocationTargetChangeListener? = null locationTargetChangeListener: LocationTargetChangeListener? = null
) { ) {
Timber.d("## Location: initialize") Timber.d("## Location: initialize")
getMapAsync { map -> getMapAsync { map ->
initMapStyle(map, url) initMapStyle(map, url)
if (showLocateBtn) { initLocateBtn(map)
showLocateBtn(map)
}
notifyLocationOfMapCenter(locationTargetChangeListener) notifyLocationOfMapCenter(locationTargetChangeListener)
listenCameraMove(map, locationTargetChangeListener) listenCameraMove(map, locationTargetChangeListener)
} }
@ -87,15 +105,10 @@ class MapTilerMapView @JvmOverloads constructor(
} }
} }
private fun listenCameraMove(map: MapboxMap, locationTargetChangeListener: LocationTargetChangeListener?) { private fun initLocateBtn(map: MapboxMap) {
map.addOnCameraMoveListener { if (showLocationBtn) {
notifyLocationOfMapCenter(locationTargetChangeListener) addView(locateBtn)
} adjustCompassBtn(map)
}
private fun notifyLocationOfMapCenter(locationTargetChangeListener: LocationTargetChangeListener?) {
getLocationOfMapCenter()?.let { target ->
locationTargetChangeListener?.onLocationTargetChange(target)
} }
} }
@ -114,8 +127,7 @@ class MapTilerMapView @JvmOverloads constructor(
} }
} }
private fun showLocateBtn(map: MapboxMap) { private fun adjustCompassBtn(map: MapboxMap) {
addView(locateBtn)
locateBtn.post { locateBtn.post {
val marginTop = locateBtn.height + locateBtn.marginTop + locateBtn.marginBottom val marginTop = locateBtn.height + locateBtn.marginTop + locateBtn.marginBottom
val marginRight = context.resources.getDimensionPixelOffset(R.dimen.location_sharing_compass_btn_margin_horizontal) 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) { fun render(state: MapState) {
val safeMapRefs = mapRefs ?: return Unit.also { val safeMapRefs = mapRefs ?: return Unit.also {
pendingState = state pendingState = state

View File

@ -8,6 +8,7 @@
android:id="@+id/mapView" android:id="@+id/mapView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:mapbox_renderTextureMode="true" /> app:mapbox_renderTextureMode="true"
app:showLocateButton="false" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -14,6 +14,7 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:mapbox_renderTextureMode="true" app:mapbox_renderTextureMode="true"
app:showLocateButton="true"
tools:background="#4F00" /> tools:background="#4F00" />
<ImageView <ImageView