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 {
views.mapView.initialize(
url = urlMapProvider.getMapUrl(),
showLocateBtn = true,
locationTargetChangeListener = this@LocationSharingFragment
)
}

View File

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

View File

@ -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>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

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