Skip to content

Commit

Permalink
refactor(android): refactorings on android and fixed issue with layer…
Browse files Browse the repository at this point in the history
…s not added (#3124)
  • Loading branch information
mfazekas authored Oct 20, 2023
1 parent 19bb63a commit 653b6ea
Show file tree
Hide file tree
Showing 33 changed files with 26 additions and 36 deletions.
18 changes: 9 additions & 9 deletions android/src/main/java/com/rnmapbox/rnmbx/RNMBXPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ import com.rnmapbox.rnmbx.utils.ViewTagResolver

class RNMBXPackage : TurboReactPackage() {

var mapViewTagResolver: ViewTagResolver<RNMBXMapView>? = null
fun getMapViewTagResolver(context: ReactApplicationContext) : ViewTagResolver<RNMBXMapView> {
val viewTagResolver = mapViewTagResolver
var viewTagResolver: ViewTagResolver? = null
fun getViewTagResolver(context: ReactApplicationContext) : ViewTagResolver {
val viewTagResolver = viewTagResolver
if (viewTagResolver == null) {
val viewTagResolver = ViewTagResolver<RNMBXMapView>(context)
mapViewTagResolver = viewTagResolver
val viewTagResolver = ViewTagResolver(context)
this.viewTagResolver = viewTagResolver
return viewTagResolver
}
return viewTagResolver
Expand All @@ -67,8 +67,8 @@ class RNMBXPackage : TurboReactPackage() {
RNMBXOfflineModule.REACT_CLASS -> return RNMBXOfflineModule(reactApplicationContext)
RNMBXSnapshotModule.REACT_CLASS -> return RNMBXSnapshotModule(reactApplicationContext)
RNMBXLogging.REACT_CLASS -> return RNMBXLogging(reactApplicationContext)
NativeMapViewModule.NAME -> return NativeMapViewModule(reactApplicationContext, getMapViewTagResolver(reactApplicationContext))
RNMBXShapeSourceModule.NAME -> return RNMBXShapeSourceModule(reactApplicationContext)
NativeMapViewModule.NAME -> return NativeMapViewModule(reactApplicationContext, getViewTagResolver(reactApplicationContext))
RNMBXShapeSourceModule.NAME -> return RNMBXShapeSourceModule(reactApplicationContext, getViewTagResolver(reactApplicationContext))
}
return null
}
Expand All @@ -83,8 +83,8 @@ class RNMBXPackage : TurboReactPackage() {

// components
managers.add(RNMBXCameraManager(reactApplicationContext))
managers.add(RNMBXAndroidTextureMapViewManager(reactApplicationContext, getMapViewTagResolver(reactApplicationContext)))
managers.add(RNMBXMapViewManager(reactApplicationContext, getMapViewTagResolver(reactApplicationContext)))
managers.add(RNMBXAndroidTextureMapViewManager(reactApplicationContext, getViewTagResolver(reactApplicationContext)))
managers.add(RNMBXMapViewManager(reactApplicationContext, getViewTagResolver(reactApplicationContext)))
managers.add(RNMBXStyleImportManager(reactApplicationContext))

// annotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.rnmapbox.rnmbx.utils.ViewTagResolver
import com.rnmapbox.rnmbx.utils.extensions.toCoordinate
import com.rnmapbox.rnmbx.utils.extensions.toScreenCoordinate

class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: ViewTagResolver<RNMBXMapView>) : NativeMapViewModuleSpec(context) {
class NativeMapViewModule(context: ReactApplicationContext, val viewTagResolver: ViewTagResolver) : NativeMapViewModuleSpec(context) {
private fun withMapViewOnUIThread(
viewRef: Double?,
reject: Promise,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.facebook.react.viewmanagers.RNMBXMapViewManagerDelegate
import com.mapbox.maps.MapInitOptions
import com.rnmapbox.rnmbx.utils.ViewTagResolver

class RNMBXAndroidTextureMapViewManager(context: ReactApplicationContext, viewTagResolver: ViewTagResolver<RNMBXMapView>) : RNMBXMapViewManager(
class RNMBXAndroidTextureMapViewManager(context: ReactApplicationContext, viewTagResolver: ViewTagResolver) : RNMBXMapViewManager(
context,
viewTagResolver
), RNMBXAndroidTextureMapViewManagerInterface<RNMBXMapView> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
savedStyle = style
styleLoaded = true
setUpImage(style)
addFeaturesToMap()
addFeaturesToMap(true)
applyLocalizeLabels()
style.setProjection(Projection(mProjection))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ interface CommandResponse {
fun error(message: String)
}

open class RNMBXMapViewManager(context: ReactApplicationContext, val viewTagResolver: ViewTagResolver<RNMBXMapView>) :
open class RNMBXMapViewManager(context: ReactApplicationContext, val viewTagResolver: ViewTagResolver) :
AbstractEventEmitter<RNMBXMapView>(context), RNMBXMapViewManagerInterface<RNMBXMapView> {
private val mViews: MutableMap<Int, RNMBXMapView>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,19 @@ import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.common.UIManagerType
import com.rnmapbox.rnmbx.BuildConfig
import com.rnmapbox.rnmbx.NativeRNMBXShapeSourceModuleSpec
import com.rnmapbox.rnmbx.components.mapview.RNMBXMapView
import com.rnmapbox.rnmbx.utils.ViewTagResolver

@ReactModule(name = RNMBXShapeSourceModule.NAME)
class RNMBXShapeSourceModule(reactContext: ReactApplicationContext?) :
class RNMBXShapeSourceModule(reactContext: ReactApplicationContext?, private val viewTagResolver: ViewTagResolver) :
NativeRNMBXShapeSourceModuleSpec(reactContext) {

private fun withShapeSourceOnUIThread(viewRef: Double?, promise: Promise, fn: (RNMBXShapeSource) -> Unit) {

private fun withShapeSourceOnUIThread(viewRef: Double?, reject: Promise, fn: (RNMBXShapeSource) -> Unit) {
if (viewRef == null) {
promise.reject(Exception("viewRef is null for RNMBXShapeSource"))
reject.reject(Exception("viewRef is null for RNMBXShapeSource"))
} else {

reactApplicationContext.runOnUiQueueThread {
val manager = if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED)
UIManagerHelper.getUIManager(reactApplicationContext, UIManagerType.FABRIC)
else
UIManagerHelper.getUIManager(reactApplicationContext, UIManagerType.DEFAULT)

val view = manager?.resolveView(viewRef.toInt()) as? RNMBXShapeSource

if (view != null) {
fn(view)
} else {
promise.reject(Exception("cannot find map view for tag ${viewRef.toInt()}"))
}
}
viewTagResolver.withViewResolved(viewRef.toInt(), reject, fn)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rnmapbox.rnmbx.utils

import android.view.View
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.UIManager
Expand All @@ -14,9 +15,9 @@ data class ViewTagWaiter<V>(
)

// see https://github.com/rnmapbox/maps/pull/3074
open class ViewTagResolver<V>(val context: ReactApplicationContext) {
open class ViewTagResolver(val context: ReactApplicationContext) {
private val createdViews: HashSet<Int> = hashSetOf<Int>()
private val viewWaiters: HashMap<Int, MutableList<ViewTagWaiter<V>>> = hashMapOf()
private val viewWaiters: HashMap<Int, MutableList<ViewTagWaiter<View>>> = hashMapOf()

// to be called from view.setId
fun tagAssigned(viewTag: Int) {
Expand All @@ -26,7 +27,7 @@ open class ViewTagResolver<V>(val context: ReactApplicationContext) {
if (list != null) {
context.runOnUiQueueThread {
try {
val view = manager.resolveView(viewTag) as V
val view = manager.resolveView(viewTag)

list.forEach { it.fn(view) }
} catch (err: IllegalViewOperationException) {
Expand All @@ -51,14 +52,14 @@ open class ViewTagResolver<V>(val context: ReactApplicationContext) {
}

// calls on UiQueueThread with resolved view
fun withViewResolved(viewTag: Int, reject: Promise? = null, fn: (V) -> Unit) {
fun <V>withViewResolved(viewTag: Int, reject: Promise? = null, fn: (V) -> Unit) {
context.runOnUiQueueThread() {
try {
val view = manager.resolveView(viewTag) as V
fn(view)
} catch (err: IllegalViewOperationException) {
if (!createdViews.contains(viewTag)) {
viewWaiters.getOrPut(viewTag) { mutableListOf<ViewTagWaiter<V>>() }.add(ViewTagWaiter(fn, reject))
viewWaiters.getOrPut(viewTag) { mutableListOf<ViewTagWaiter<View>>() }.add(ViewTagWaiter<View>({ view -> fn(view as V) }, reject))
} else {
reject?.reject(err)
}
Expand Down

0 comments on commit 653b6ea

Please sign in to comment.