-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathMPRoktModule.kt
More file actions
79 lines (71 loc) · 2.92 KB
/
Copy pathMPRoktModule.kt
File metadata and controls
79 lines (71 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.mparticle.react.rokt
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.uimanager.NativeViewHierarchyManager
import com.facebook.react.uimanager.UIManagerModule
import com.mparticle.MParticle
import com.mparticle.internal.Logger
import com.mparticle.react.NativeMPRoktSpec
import com.mparticle.rokt.RoktEmbeddedView
import java.lang.ref.WeakReference
class MPRoktModule(
private val reactContext: ReactApplicationContext,
) : NativeMPRoktSpec(reactContext) {
private val impl = MPRoktModuleImpl(reactContext)
override fun getName(): String = impl.getName()
@ReactMethod
override fun selectPlacements(
identifier: String,
attributes: ReadableMap?,
placeholders: ReadableMap?,
roktConfig: ReadableMap?,
fontFilesMap: ReadableMap?,
) {
if (identifier.isBlank()) {
Logger.warning("selectPlacements failed. identifier cannot be empty")
return
}
val uiManager = reactContext.getNativeModule(UIManagerModule::class.java)
MParticle.getInstance()?.Rokt()?.events(identifier)?.let {
impl.startRoktEventListener(it, reactContext.currentActivity, identifier)
}
val config = roktConfig?.let { impl.buildRoktConfig(it) }
uiManager?.addUIBlock { nativeViewHierarchyManager ->
MParticle.getInstance()?.Rokt()?.selectPlacements(
identifier = identifier,
attributes = impl.readableMapToMapOfStrings(attributes),
callbacks = impl.createRoktCallback(),
embeddedViews = safeUnwrapPlaceholders(placeholders, nativeViewHierarchyManager),
fontTypefaces = null, // TODO
config = config,
)
}
}
@ReactMethod
override fun purchaseFinalized(
placementId: String,
catalogItemId: String,
success: Boolean,
) {
impl.purchaseFinalized(placementId, catalogItemId, success)
}
private fun safeUnwrapPlaceholders(
placeholders: ReadableMap?,
nativeViewHierarchyManager: NativeViewHierarchyManager,
): Map<String, WeakReference<RoktEmbeddedView>> {
val placeholderMap: MutableMap<String, WeakReference<RoktEmbeddedView>> = HashMap()
if (placeholders != null) {
placeholderMap.putAll(
placeholders
.toHashMap()
.filterValues { value -> value is Double }
.mapValues { pair -> (pair.value as Double).toInt() }
.mapValues { pair -> nativeViewHierarchyManager.resolveView(pair.value) as? RoktEmbeddedView }
.filterValues { value -> value != null }
.mapValues { WeakReference(it.value as RoktEmbeddedView) },
)
}
return placeholderMap
}
}