-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRNSourcepointCmpModule.kt
More file actions
161 lines (131 loc) · 5.23 KB
/
Copy pathRNSourcepointCmpModule.kt
File metadata and controls
161 lines (131 loc) · 5.23 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.sourcepoint.reactnativecmp
import android.view.View
import com.facebook.react.bridge.Arguments.createMap
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import com.facebook.react.modules.core.DeviceEventManagerModule
import com.sourcepoint.cmplibrary.NativeMessageController
import com.sourcepoint.cmplibrary.SpClient
import com.sourcepoint.cmplibrary.SpConsentLib
import com.sourcepoint.cmplibrary.core.nativemessage.MessageStructure
import com.sourcepoint.cmplibrary.creation.ConfigOption
import com.sourcepoint.cmplibrary.creation.SpConfigDataBuilder
import com.sourcepoint.cmplibrary.creation.makeConsentLib
import com.sourcepoint.cmplibrary.exception.CampaignType
import com.sourcepoint.cmplibrary.model.ConsentAction
import com.sourcepoint.cmplibrary.model.exposed.SPConsents
import com.sourcepoint.cmplibrary.util.clearAllData
import com.sourcepoint.cmplibrary.util.userConsents
import com.sourcepoint.reactnativecmp.consents.RNSPUserData
import org.json.JSONObject
data class SPLoadMessageParams(val authId: String?) {
constructor(fromReadableMap: ReadableMap?) : this(authId = fromReadableMap?.getString("authId"))
}
class RNSourcepointCmpModule internal constructor(context: ReactApplicationContext) :
RNSourcepointCmpSpec(context) , SpClient {
enum class SDKEvent {
onSPUIReady, onSPUIFinished, onAction, onSPFinished, onError
}
private var spConsentLib: SpConsentLib? = null
override fun getName() = NAME
@ReactMethod
override fun build(accountId: Int, propertyId: Int, propertyName: String, campaigns: ReadableMap) {
val convertedCampaigns = campaigns.SPCampaigns()
val config = SpConfigDataBuilder().apply {
addAccountId(accountId)
addPropertyName(propertyName)
addPropertyId(propertyId)
addMessageTimeout(30000)
convertedCampaigns.gdpr?.let {
addCampaign(campaignType = CampaignType.GDPR, params = it.targetingParams, groupPmId = null)
}
convertedCampaigns.usnat?.let {
addCampaign(
campaignType = CampaignType.USNAT,
params = it.targetingParams,
groupPmId = null,
configParams = if(it.supportLegacyUSPString) setOf(ConfigOption.SUPPORT_LEGACY_USPSTRING) else emptySet()
)
}
}.build()
reactApplicationContext.currentActivity?.let {
spConsentLib = makeConsentLib(config, it, this)
} ?: run {
onError(Error("No activity found when building the SDK"))
}
}
private fun runOnMainThread(runnable: () -> Unit) {
reactApplicationContext.runOnUiQueueThread(runnable)
}
@ReactMethod
override fun loadMessage(params: ReadableMap?) {
val parsedParams = SPLoadMessageParams(fromReadableMap = params)
runOnMainThread { spConsentLib?.loadMessage(
authId = parsedParams.authId,
cmpViewId = View.generateViewId()
) }
}
@ReactMethod
override fun clearLocalData() {
clearAllData(reactApplicationContext)
}
@ReactMethod
override fun getUserData(promise: Promise) {
promise.resolve(userConsentsToWriteableMap(userConsents(reactApplicationContext)))
}
@ReactMethod
override fun loadGDPRPrivacyManager(pmId: String) {
runOnMainThread { spConsentLib?.loadPrivacyManager(pmId, CampaignType.GDPR) }
}
@ReactMethod
override fun loadUSNatPrivacyManager(pmId: String) {
runOnMainThread { spConsentLib?.loadPrivacyManager(pmId, CampaignType.USNAT) }
}
@ReactMethod
override fun supportedEvents(): Array<String> {
return SDKEvent.values().map { it.name }.toTypedArray()
}
private fun sendEvent(event: SDKEvent, params: WritableMap? = null) = reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(event.name, params)
@ReactMethod
fun addListener(eventName: String) {}
@ReactMethod
fun removeListeners(count: Int) {}
companion object {
const val NAME = "RNSourcepointCmp"
}
override fun onAction(view: View, consentAction: ConsentAction): ConsentAction {
sendEvent(SDKEvent.onAction, createMap().apply {
putString("actionType", RNSourcepointActionType.from(consentAction.actionType).name)
})
return consentAction
}
override fun onConsentReady(consent: SPConsents) {
}
override fun onError(error: Throwable) {
sendEvent(SDKEvent.onError, createMap().apply { putString("description", error.message) })
}
@Deprecated("onMessageReady callback will be removed in favor of onUIReady. Currently this callback is disabled.")
override fun onMessageReady(message: JSONObject) {}
override fun onNativeMessageReady(
message: MessageStructure,
messageController: NativeMessageController
) {}
override fun onNoIntentActivitiesFound(url: String) {}
override fun onSpFinished(sPConsents: SPConsents) {
sendEvent(SDKEvent.onSPFinished)
}
override fun onUIFinished(view: View) {
spConsentLib?.removeView(view)
sendEvent(SDKEvent.onSPUIFinished)
}
override fun onUIReady(view: View) {
spConsentLib?.showView(view)
sendEvent(SDKEvent.onSPUIReady)
}
private fun userConsentsToWriteableMap(consents: SPConsents) = RNSPUserData(consents).toRN()
}