Skip to content

Commit

Permalink
fix(android): android init set camera (#3073)
Browse files Browse the repository at this point in the history
* feat: Add StyleImport for v11

* chore: add old-arch generation to autogenerate

* fix: remove _setTimeout from _runNativeCommand

* chore: pods cache key fix

* fix(android): only set initial camera after view was attached to window
  • Loading branch information
mfazekas authored Sep 26, 2023
1 parent c60584d commit c529890
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame

override fun addToMap(mapView: RNMBXMapView) {
super.addToMap(mapView)
setInitialCamera()
updateMaxBounds()
mCameraStop?.let { updateCamera(it) }
mapView.callIfAttachedToWindow {
setInitialCamera()
updateMaxBounds()
mCameraStop?.let { updateCamera(it) }
}
_observeViewportState(mapView.mapView)
_updateViewportState()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.PointF
import android.graphics.RectF
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.Gravity
import android.view.View
Expand Down Expand Up @@ -102,6 +104,10 @@ interface RNMBXLifeCycleOwner : LifecycleOwner {
fun handleLifecycleEvent(event: Lifecycle.Event)
}

fun interface Cancelable {
fun cancel()
}

class RNMBXLifeCycle {
private var lifecycleOwner : RNMBXLifeCycleOwner? = null

Expand Down Expand Up @@ -150,6 +156,25 @@ class RNMBXLifeCycle {
fun getState() : Lifecycle.State {
return lifecycleOwner?.lifecycle?.currentState ?: Lifecycle.State.INITIALIZED;
}

var attachedToWindowWaiters : MutableList<()-> Unit> = mutableListOf()

fun callIfAttachedToWindow(callback: () -> Unit) : com.rnmapbox.rnmbx.components.mapview.Cancelable {
if (getState() == Lifecycle.State.STARTED) {
callback()
return com.rnmapbox.rnmbx.components.mapview.Cancelable {}
} else {
attachedToWindowWaiters.add(callback)
return com.rnmapbox.rnmbx.components.mapview.Cancelable {
attachedToWindowWaiters.removeIf { it === callback }
}
}
}

fun afterAttachFromLooper() {
attachedToWindowWaiters.forEach { it() }
attachedToWindowWaiters.clear()
}
}

data class FeatureEntry(val feature: AbstractMapFeature?, val view: View?, var addedToMap: Boolean = false) {
Expand Down Expand Up @@ -1418,6 +1443,13 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
override fun onAttachedToWindow() {
lifecycle.onAttachedToWindow(this)
super.onAttachedToWindow()
Handler(Looper.getMainLooper()).post {
lifecycle.afterAttachFromLooper()
}
}

fun callIfAttachedToWindow(callback: () -> Unit) {
lifecycle.callIfAttachedToWindow(callback)
}

override fun onLayoutChange(
Expand Down

0 comments on commit c529890

Please sign in to comment.