-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathReactNativeCmpModule.kt
More file actions
213 lines (184 loc) · 6.7 KB
/
ReactNativeCmpModule.kt
File metadata and controls
213 lines (184 loc) · 6.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.sourcepoint.reactnativecmp
import android.view.View
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.sourcepoint.cmplibrary.SpClient
import com.sourcepoint.cmplibrary.SpConsentLib
import com.sourcepoint.cmplibrary.creation.ConfigOption.SUPPORT_LEGACY_USPSTRING
import com.sourcepoint.cmplibrary.creation.SpConfigDataBuilder
import com.sourcepoint.cmplibrary.creation.makeConsentLib
import com.sourcepoint.cmplibrary.data.network.util.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.arguments.BuildOptions
import com.sourcepoint.reactnativecmp.arguments.toList
import com.sourcepoint.reactnativecmp.consents.RNSPGDPRConsent
import com.sourcepoint.reactnativecmp.consents.RNSPUserData
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
data class SPLoadMessageParams(val authId: String?) {
constructor(fromReadableMap: ReadableMap?) : this(authId = fromReadableMap?.getString("authId"))
}
class ReactNativeCmpModule(reactContext: ReactApplicationContext) : NativeReactNativeCmpSpec(reactContext),
SpClient {
private var spConsentLib: SpConsentLib? = null
private var consentView: View? = null
override fun getName() = NAME
override fun build(
accountId: Double,
propertyId: Double,
propertyName: String,
campaigns: ReadableMap,
options: ReadableMap?,
) {
val convertedCampaigns = campaigns.SPCampaigns()
val parsedOptions = BuildOptions(options)
val config = SpConfigDataBuilder().apply {
addAccountId(accountId.toInt())
addPropertyName(propertyName)
addPropertyId(propertyId.toInt())
addMessageTimeout(parsedOptions.messageTimeoutInMilliseconds)
addMessageLanguage(parsedOptions.language)
convertedCampaigns.gdpr?.let {
addCampaign(campaignType = GDPR, params = it.targetingParams, groupPmId = it.groupPmId)
}
convertedCampaigns.usnat?.let {
addCampaign(
campaignType = USNAT,
params = it.targetingParams,
groupPmId = it.groupPmId,
configParams = if(it.supportLegacyUSPString) setOf(SUPPORT_LEGACY_USPSTRING) else emptySet()
)
}
convertedCampaigns.preferences?.let {
addCampaign(campaignType = PREFERENCES, params = it.targetingParams, groupPmId = it.groupPmId)
}
convertedCampaigns.globalcmp?.let {
addCampaign(campaignType = GLOBALCMP, params = it.targetingParams, groupPmId = it.groupPmId)
}
}.build()
reactApplicationContext.currentActivity?.let {
spConsentLib = makeConsentLib(config, it, this, parsedOptions.androidDismissMessageOnBackPress)
} ?: run {
onError(Error("No activity found when building the SDK"))
}
}
private fun runOnMainThread(runnable: () -> Unit) {
reactApplicationContext.runOnUiQueueThread(runnable)
}
override fun loadMessage(params: ReadableMap?) {
val parsedParams = SPLoadMessageParams(fromReadableMap = params)
runOnMainThread {
spConsentLib?.loadMessage(authId = parsedParams.authId, cmpViewId = View.generateViewId())
}
}
override fun clearLocalData() {
clearAllData(reactApplicationContext)
}
override fun getUserData(promise: Promise) {
promise.resolve(userConsentsToWriteableMap(userConsents(reactApplicationContext)))
}
override fun loadGDPRPrivacyManager(pmId: String) {
runOnMainThread { spConsentLib?.loadPrivacyManager(pmId, GDPR) }
}
override fun loadUSNatPrivacyManager(pmId: String) {
runOnMainThread { spConsentLib?.loadPrivacyManager(pmId, USNAT) }
}
override fun loadGlobalCmpPrivacyManager(pmId: String) {
runOnMainThread { spConsentLib?.loadPrivacyManager(pmId, GLOBALCMP) }
}
override fun loadPreferenceCenter(id: String) {
runOnMainThread { spConsentLib?.loadPrivacyManager(id, PREFERENCES) }
}
override fun dismissMessage() {
runOnMainThread { spConsentLib?.dismissMessage() }
}
override fun postCustomConsentGDPR(
vendors: ReadableArray,
categories: ReadableArray,
legIntCategories: ReadableArray,
callback: Callback
) {
runOnMainThread {
spConsentLib?.customConsentGDPR(
vendors.toList(),
categories.toList(),
legIntCategories.toList(),
success = { consents ->
if (consents?.gdpr != null) {
callback.invoke(RNSPGDPRConsent(consents.gdpr!!.consent).toRN())
} else {
callback.invoke(RNSPGDPRConsent(applies = true).toRN())
}
}
)
}
}
override fun postDeleteCustomConsentGDPR(
vendors: ReadableArray,
categories: ReadableArray,
legIntCategories: ReadableArray,
callback: Callback
) {
runOnMainThread {
spConsentLib?.deleteCustomConsentTo(
vendors.toList(),
categories.toList(),
legIntCategories.toList(),
success = { consents ->
if (consents?.gdpr != null) {
callback.invoke(RNSPGDPRConsent(consents.gdpr!!.consent).toRN())
} else {
callback.invoke(RNSPGDPRConsent(applies = true).toRN())
}
}
)
}
}
override fun rejectAll(campaignType: String) {
runOnMainThread {
spConsentLib?.rejectAll(campaignTypeFrom(campaignType))
}
}
companion object {
const val NAME = "ReactNativeCmp"
}
override fun onAction(view: View, consentAction: ConsentAction): ConsentAction {
emitInternalOnAction(Json.encodeToString(mapOf(
"actionType" to RNSourcepointActionType.from(consentAction.actionType).name,
"customActionId" to consentAction.customActionId
)))
return consentAction
}
override fun onConsentReady(consent: SPConsents) {}
override fun onError(error: Throwable) {
emitInternalOnError(RNSPError.from(error).toJsonString())
consentView?.let {
spConsentLib?.removeView(it)
}
consentView = null
}
override fun onNoIntentActivitiesFound(url: String) {}
override fun onSpFinished(sPConsents: SPConsents) {
emitOnFinished()
}
override fun onMessageInactivityTimeout() {
emitOnMessageInactivityTimeout()
}
override fun onUIFinished(view: View) {
consentView = null
spConsentLib?.removeView(view)
emitOnSPUIFinished()
}
override fun onUIReady(view: View) {
consentView = view
spConsentLib?.showView(view)
emitOnSPUIReady()
}
private fun userConsentsToWriteableMap(consents: SPConsents) = RNSPUserData(consents).toRN()
}