Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.rnmapbox.rnmbx.components.mapview

enum class MapGestureType {
Move, Scale, Rotate, Fling, Shove
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.rnmapbox.rnmbx.components.location.LocationComponentManager
import com.rnmapbox.rnmbx.components.location.RNMBXNativeUserLocation
import com.rnmapbox.rnmbx.components.mapview.helpers.CameraChangeReason
import com.rnmapbox.rnmbx.components.mapview.helpers.CameraChangeTracker
import com.rnmapbox.rnmbx.components.mapview.helpers.MapSteadyDetector
import com.rnmapbox.rnmbx.components.styles.layers.RNMBXLayer
import com.rnmapbox.rnmbx.components.styles.light.RNMBXLight
import com.rnmapbox.rnmbx.components.styles.sources.RNMBXSource
Expand Down Expand Up @@ -91,10 +92,6 @@ data class OrnamentSettings(
var position: Int = -1
)

enum class MapGestureType {
Move,Scale,Rotate,Fling,Shove
}

fun interface Cancelable {
fun cancel()
}
Expand Down Expand Up @@ -156,6 +153,7 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
private val mFeatures = mutableListOf<FeatureEntry>()
private var mQueuedFeatures: MutableList<AbstractMapFeature>? = ArrayList()
private val mCameraChangeTracker = CameraChangeTracker()
private var mMapSteadyDetector: MapSteadyDetector? = null
private var mPreferredFrameRate: Int? = null
private var mMaxPitch: Double? = null
private lateinit var mMap: MapboxMap
Expand Down Expand Up @@ -240,6 +238,13 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
handleMapChangedEvent(EventTypes.MAP_IDLE);
})

mMapSteadyDetector = MapSteadyDetector(map).apply {
onSteady = { idleDurationMs, lastGestureType ->
mCameraChangeTracker.clear()
}
attach()
}

val gesturesPlugin: GesturesPlugin = mapView.gestures
gesturesPlugin.addOnMapLongClickListener(_this)
gesturesPlugin.addOnMapClickListener(_this)
Expand Down Expand Up @@ -315,11 +320,10 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie

fun<T> mapGestureBegin(type:MapGestureType, gesture: T) {
isGestureActive = true
mCameraChangeTracker.setReason(CameraChangeReason.USER_GESTURE)
mCameraChangeTracker.setReason(type, CameraChangeReason.USER_GESTURE)
handleMapChangedEvent(EventTypes.REGION_WILL_CHANGE)
}
fun<T> mapGesture(type: MapGestureType, gesture: T): Boolean {
mCameraChangeTracker.setReason(CameraChangeReason.USER_GESTURE)
handleMapChangedEvent(EventTypes.REGION_IS_CHANGING)
return false
}
Expand Down Expand Up @@ -425,7 +429,7 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
val didChangeEvent = MapChangeEvent(this, EventTypes.REGION_DID_CHANGE,
makeRegionPayload(isAnimated))
mManager.handleEvent(didChangeEvent)
mCameraChangeTracker.setReason(CameraChangeReason.NONE)
mCameraChangeTracker.clear()
}

private fun removeAllFeaturesFromMap(reason: RemovalReason) {
Expand Down Expand Up @@ -781,7 +785,7 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie

fun sendRegionDidChangeEvent() {
handleMapChangedEvent(EventTypes.REGION_DID_CHANGE)
mCameraChangeTracker.setReason(CameraChangeReason.NONE)
mCameraChangeTracker.clear()
}

private fun handleMapChangedEvent(eventType: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
package com.rnmapbox.rnmbx.components.mapview.helpers

import com.rnmapbox.rnmbx.components.mapview.MapGestureType

enum class CameraChangeReason {
NONE,
USER_GESTURE,
DEVELOPER_ANIMATION,
SDK_ANIMATION
}

class CameraChangeTracker {
private var reason : CameraChangeReason = CameraChangeReason.NONE
private val reasonByGesture = mutableMapOf<MapGestureType, CameraChangeReason>()
var isAnimating = false
fun setReason(reason: CameraChangeReason) {
this.reason = reason

fun setReason(type: MapGestureType, reason: CameraChangeReason) {
reasonByGesture[type] = reason
}

fun clear() {
reasonByGesture.clear()
}

fun clearReason(type: MapGestureType) {
reasonByGesture.remove(type)
}

val isUserInteraction: Boolean
get() = reason == CameraChangeReason.USER_GESTURE || reason == CameraChangeReason.DEVELOPER_ANIMATION
get() = reasonByGesture.values.any { it == CameraChangeReason.USER_GESTURE }
val isAnimated: Boolean
get() = reason == CameraChangeReason.DEVELOPER_ANIMATION || reason == CameraChangeReason.SDK_ANIMATION
get() = reasonByGesture.values.any { it == CameraChangeReason.DEVELOPER_ANIMATION || it == CameraChangeReason.SDK_ANIMATION }
val isEmpty: Boolean
get() = reason == CameraChangeReason.NONE
}
get() = reasonByGesture.isEmpty()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package com.rnmapbox.rnmbx.components.mapview.helpers

import android.animation.ValueAnimator
import android.os.Handler
import android.os.Looper
import com.mapbox.android.gestures.MoveGestureDetector
import com.mapbox.android.gestures.RotateGestureDetector
import com.mapbox.android.gestures.ShoveGestureDetector
import com.mapbox.android.gestures.StandardScaleGestureDetector
import com.mapbox.maps.MapboxMap
import com.mapbox.maps.plugin.animation.CameraAnimationsLifecycleListener
import com.mapbox.maps.plugin.animation.CameraAnimatorType
import com.mapbox.maps.plugin.gestures.OnMoveListener
import com.mapbox.maps.plugin.gestures.OnRotateListener
import com.mapbox.maps.plugin.gestures.OnScaleListener
import com.mapbox.maps.plugin.gestures.OnShoveListener
import com.rnmapbox.rnmbx.components.mapview.MapGestureType

class MapSteadyDetector(
private val mapboxMap: MapboxMap,
var quietPeriodMs: Double = 200.0,
var maxIntervalMs: Double? = null,
) {
var onSteady: ((idleDurationMs: Double, lastGestureType: MapGestureType?) -> Unit)? = null
var onTimeout: ((lastGestureType: MapGestureType?) -> Unit)? = null

private val handler = Handler(Looper.getMainLooper())
var activeAnimations: Int = 0
private set
var isGestureActive: Boolean = false
private set
private var lastGestureType: MapGestureType? = null
var lastTransitionEndedAtMs: Double? = null
private set
private var quietRunnable: Runnable? = null
private var timeoutRunnable: Runnable? = null

private fun nowMs(): Double = System.currentTimeMillis().toDouble()

private val canEmitSteady: Boolean
get() = activeAnimations == 0 && !isGestureActive && lastTransitionEndedAtMs != null

fun attach() {
mapboxMap.cameraAnimationsPlugin {
addCameraAnimationsLifecycleListener(object : CameraAnimationsLifecycleListener {
override fun onAnimatorStarting(type: CameraAnimatorType, animator: ValueAnimator, owner: String?) {
if (owner != GESTURES_OWNER) return
activeAnimations++
lastTransitionEndedAtMs = null
markActivity()
}

override fun onAnimatorEnding(type: CameraAnimatorType, animator: ValueAnimator, owner: String?) {
if (owner != GESTURES_OWNER) return
handleAnimatorEnd()
}

override fun onAnimatorCancelling(type: CameraAnimatorType, animator: ValueAnimator, owner: String?) {
if (owner != GESTURES_OWNER) return
handleAnimatorEnd()
}

override fun onAnimatorInterrupting(
type: CameraAnimatorType,
runningAnimator: ValueAnimator,
runningAnimatorOwner: String?,
newAnimator: ValueAnimator,
newAnimatorOwner: String?
) {}
})
}

mapboxMap.gesturesPlugin {
addOnMoveListener(object : OnMoveListener {
override fun onMoveBegin(detector: MoveGestureDetector) { handleGestureBegin(MapGestureType.Move) }
override fun onMove(detector: MoveGestureDetector): Boolean = false
override fun onMoveEnd(detector: MoveGestureDetector) { handleGestureEnd(MapGestureType.Move) }
})
addOnScaleListener(object : OnScaleListener {
override fun onScaleBegin(detector: StandardScaleGestureDetector) { handleGestureBegin(MapGestureType.Scale) }
override fun onScale(detector: StandardScaleGestureDetector) {}
override fun onScaleEnd(detector: StandardScaleGestureDetector) { handleGestureEnd(MapGestureType.Scale) }
})
addOnRotateListener(object : OnRotateListener {
override fun onRotateBegin(detector: RotateGestureDetector) { handleGestureBegin(MapGestureType.Rotate) }
override fun onRotate(detector: RotateGestureDetector) {}
override fun onRotateEnd(detector: RotateGestureDetector) { handleGestureEnd(MapGestureType.Rotate) }
})
addOnShoveListener(object : OnShoveListener {
override fun onShoveBegin(detector: ShoveGestureDetector) { handleGestureBegin(MapGestureType.Shove) }
override fun onShove(detector: ShoveGestureDetector) {}
override fun onShoveEnd(detector: ShoveGestureDetector) { handleGestureEnd(MapGestureType.Shove) }
})
}
}

fun detach() {
cancelQuietTimer()
cancelTimeoutTimer()
}

private fun cancelQuietTimer() {
quietRunnable?.let { handler.removeCallbacks(it) }
quietRunnable = null
}

private fun cancelTimeoutTimer() {
timeoutRunnable?.let { handler.removeCallbacks(it) }
timeoutRunnable = null
}

private fun scheduleQuietCheck() {
cancelQuietTimer()
if (quietPeriodMs <= 0) {
maybeEmitSteady()
return
}
val runnable = Runnable { maybeEmitSteady() }
handler.postDelayed(runnable, quietPeriodMs.toLong())
quietRunnable = runnable
}

private fun scheduleTimeoutTimer() {
if (timeoutRunnable != null) return
val delay = maxIntervalMs ?: return
if (delay <= 0) return
val runnable = Runnable {
timeoutRunnable = null
onTimeout?.invoke(lastGestureType)
scheduleTimeoutTimer()
}
handler.postDelayed(runnable, delay.toLong())
timeoutRunnable = runnable
}

private fun markActivity(gestureType: MapGestureType? = null) {
if (gestureType != null) lastGestureType = gestureType
scheduleQuietCheck()
scheduleTimeoutTimer()
}

private fun maybeEmitSteady() {
if (!canEmitSteady) return
val lastEnd = lastTransitionEndedAtMs ?: return
val sinceEnd = nowMs() - lastEnd
if (sinceEnd < quietPeriodMs) return
cancelQuietTimer()
cancelTimeoutTimer()
val gesture = lastGestureType
onSteady?.invoke(sinceEnd, gesture)
lastGestureType = null
}

private fun handleAnimatorEnd() {
activeAnimations--
if (activeAnimations < 0) activeAnimations = 0
lastTransitionEndedAtMs = nowMs()
scheduleQuietCheck()
}

private fun handleGestureBegin(type: MapGestureType) {
isGestureActive = true
lastGestureType = type
lastTransitionEndedAtMs = null
markActivity(lastGestureType)
}

private fun handleGestureEnd(type: MapGestureType) {
lastGestureType = type
isGestureActive = false
lastTransitionEndedAtMs = nowMs()
markActivity(lastGestureType)
}

companion object {
private const val GESTURES_OWNER = "Maps-Gestures"
}
}
Loading