|
| 1 | +package com.mparticle.react.rokt |
| 2 | + |
| 3 | +import com.facebook.react.bridge.ReactApplicationContext |
| 4 | +import com.facebook.react.bridge.ReactContextBaseJavaModule |
| 5 | +import com.facebook.react.bridge.ReactMethod |
| 6 | +import com.facebook.react.bridge.ReadableMap |
| 7 | +import com.facebook.react.bridge.ReadableType |
| 8 | +import com.facebook.react.bridge.UiThreadUtil |
| 9 | +import com.facebook.react.uimanager.NativeViewHierarchyManager |
| 10 | +import com.facebook.react.uimanager.UIManagerHelper |
| 11 | +import com.facebook.react.uimanager.UIManagerModule |
| 12 | +import com.mparticle.MParticle |
| 13 | +import com.mparticle.WrapperSdk |
| 14 | +import com.mparticle.react.NativeMPRoktSpec |
| 15 | +import com.mparticle.rokt.RoktEmbeddedView |
| 16 | +import java.lang.ref.WeakReference |
| 17 | +import java.util.concurrent.CountDownLatch |
| 18 | + |
| 19 | +class MPRoktModule( |
| 20 | + private val reactContext: ReactApplicationContext, |
| 21 | +) : NativeMPRoktSpec(reactContext) { |
| 22 | + init { |
| 23 | + MParticle.getInstance()?.setWrapperSdk(WrapperSdk.WrapperSdkReactNative, "") |
| 24 | + } |
| 25 | + |
| 26 | + private val impl = MPRoktModuleImpl(reactContext) |
| 27 | + |
| 28 | + override fun getName(): String = impl.getName() |
| 29 | + |
| 30 | + @ReactMethod |
| 31 | + override fun selectPlacements( |
| 32 | + identifier: String, |
| 33 | + attributes: ReadableMap?, |
| 34 | + placeholders: ReadableMap?, |
| 35 | + roktConfig: ReadableMap?, |
| 36 | + fontFilesMap: ReadableMap?, |
| 37 | + ) { |
| 38 | + if (identifier.isBlank()) { |
| 39 | + impl.logDebug("selectPlacements failed. identifier cannot be empty") |
| 40 | + return |
| 41 | + } |
| 42 | + MParticle.getInstance()?.Rokt()?.events(identifier)?.let { |
| 43 | + impl.startRoktEventListener(it, reactContext.currentActivity, identifier) |
| 44 | + } |
| 45 | + |
| 46 | + // Process placeholders for Fabric |
| 47 | + val placeholdersMap = processPlaceholders(placeholders) |
| 48 | + val config = roktConfig?.let { impl.buildRoktConfig(it) } |
| 49 | + |
| 50 | + MParticle.getInstance()?.Rokt()?.selectPlacements( |
| 51 | + identifier = identifier, |
| 52 | + attributes = impl.readableMapToMapOfStrings(attributes), |
| 53 | + callbacks = impl.createRoktCallback(), |
| 54 | + embeddedViews = placeholdersMap, |
| 55 | + fontTypefaces = null, // TODO |
| 56 | + config = config, |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * Process placeholders from ReadableMap to a map of Widgets for use with Rokt. |
| 63 | + * This method handles the Fabric-specific view resolution. |
| 64 | + */ |
| 65 | + private fun processPlaceholders(placeholders: ReadableMap?): Map<String, WeakReference<RoktEmbeddedView>> { |
| 66 | + val placeholdersMap = HashMap<String, WeakReference<RoktEmbeddedView>>() |
| 67 | + |
| 68 | + if (placeholders != null) { |
| 69 | + // Use CountDownLatch to wait for UI thread processing |
| 70 | + val latch = CountDownLatch(1) |
| 71 | + |
| 72 | + // Run view resolution on UI thread |
| 73 | + UiThreadUtil.runOnUiThread { |
| 74 | + try { |
| 75 | + val iterator = placeholders.keySetIterator() |
| 76 | + while (iterator.hasNextKey()) { |
| 77 | + val key = iterator.nextKey() |
| 78 | + try { |
| 79 | + // Get the tag value as an integer |
| 80 | + val reactTag = |
| 81 | + when { |
| 82 | + placeholders.getType(key) == ReadableType.Number -> |
| 83 | + placeholders.getDouble(key).toInt() |
| 84 | + |
| 85 | + else -> { |
| 86 | + impl.logDebug("Invalid view tag for key: $key") |
| 87 | + continue |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Get the UIManager for this specific tag |
| 92 | + val uiManager = |
| 93 | + UIManagerHelper.getUIManagerForReactTag(reactContext, reactTag) |
| 94 | + if (uiManager == null) { |
| 95 | + impl.logDebug("UIManager not found for tag: $reactTag") |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + // Resolve the view using the manager (now on UI thread) |
| 100 | + val view = uiManager.resolveView(reactTag) |
| 101 | + if (view is RoktEmbeddedView) { |
| 102 | + placeholdersMap[key] = WeakReference(view) |
| 103 | + impl.logDebug("Successfully found Widget for key: $key with tag: $reactTag") |
| 104 | + } else { |
| 105 | + impl.logDebug("View with tag $reactTag is not a Widget: ${view?.javaClass?.simpleName}") |
| 106 | + } |
| 107 | + } catch (e: Exception) { |
| 108 | + impl.logDebug("Error processing placeholder for key $key: ${e.message}") |
| 109 | + e.printStackTrace() |
| 110 | + } |
| 111 | + } |
| 112 | + } finally { |
| 113 | + latch.countDown() |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + try { |
| 118 | + // Wait for UI thread to finish processing |
| 119 | + latch.await() |
| 120 | + } catch (e: InterruptedException) { |
| 121 | + impl.logDebug("Interrupted while waiting for UI thread: ${e.message}") |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return placeholdersMap |
| 126 | + } |
| 127 | +} |
0 commit comments