Skip to content

Commit bb99b37

Browse files
Merge branch 'development' into feat/SQDSDKS-7213-send-versions-to-rokt-kit
2 parents eb75f94 + 7ef4293 commit bb99b37

File tree

2 files changed

+199
-176
lines changed

2 files changed

+199
-176
lines changed

src/main/kotlin/com/mparticle/kits/RoktKit.kt

Lines changed: 100 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ import android.content.pm.PackageManager
77
import android.graphics.Typeface
88
import android.os.Build
99
import com.mparticle.BuildConfig
10+
import com.mparticle.MParticle
11+
import com.mparticle.MParticle.IdentityType
1012
import com.mparticle.commerce.CommerceEvent
1113
import com.mparticle.identity.MParticleUser
14+
import com.mparticle.internal.Constants
1215
import com.mparticle.internal.Logger
1316
import com.mparticle.kits.KitIntegration.CommerceListener
1417
import com.mparticle.kits.KitIntegration.IdentityListener
1518
import com.mparticle.kits.KitIntegration.RoktListener
19+
import com.mparticle.rokt.RoktEmbeddedView
1620
import com.rokt.roktsdk.Rokt
21+
import com.rokt.roktsdk.RoktWidgetDimensionCallBack
1722
import com.rokt.roktsdk.Widget
1823
import java.lang.ref.WeakReference
1924
import java.math.BigDecimal
@@ -26,10 +31,7 @@ import java.math.BigDecimal
2631
*/
2732
class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListener, Rokt.RoktCallback {
2833
private var applicationContext: Context? = null
29-
private var onUnloadCallback: Runnable? = null
30-
private var onLoadCallback: Runnable? = null
31-
private var onShouldHideLoadingIndicatorCallback: Runnable? = null
32-
private var onShouldShowLoadingIndicatorCallback: Runnable? = null
34+
private var mpRoktEventCallback: MParticle.MpRoktEventCallback? = null
3335
override fun getName(): String = NAME
3436

3537
override fun getInstance(): RoktKit = this
@@ -134,38 +136,54 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
134136
@Suppress("UNCHECKED_CAST", "CAST_NEVER_SUCCEEDS")
135137
override fun execute(
136138
viewName: String,
137-
attributes: Map<String, String>?,
138-
onUnload: Runnable?,
139-
onLoad: Runnable?,
140-
onShouldHideLoadingIndicator: Runnable?,
141-
onShouldShowLoadingIndicator: Runnable?,
142-
placeHolders: MutableMap<String, WeakReference<com.mparticle.rokt.RoktEmbeddedView>>?,
139+
attributes: Map<String, String>,
140+
mpRoktEventCallback: MParticle.MpRoktEventCallback?,
141+
placeHolders: MutableMap<String, WeakReference<RoktEmbeddedView>>?,
143142
fontTypefaces: MutableMap<String, WeakReference<Typeface>>?,
144143
filterUser: FilteredMParticleUser?
145144
) {
146-
// Converting the placeholders to a Map<String, WeakReference<Widget>> by filtering and casting each entry
147145
val placeholders: Map<String, WeakReference<Widget>>? = placeHolders?.mapNotNull { entry ->
148-
val weakRef = entry.value
149-
val widget = weakRef.get() as? Widget // Safe cast to Widget
150-
widget?.let { entry.key to weakRef as WeakReference<Widget> } // Only include if it's a Widget
146+
val widget = Widget(entry.value.get()?.context as Context)
147+
entry.value.get()?.removeAllViews()
148+
entry.value.get()?.addView(widget)
149+
entry.value.get()?.dimensionCallBack?.let {
150+
widget.registerDimensionListener(object: RoktWidgetDimensionCallBack {
151+
override fun onHeightChanged(height: Int) {
152+
it.onHeightChanged(height)
153+
}
154+
155+
override fun onMarginChanged(
156+
start: Int,
157+
top: Int,
158+
end: Int,
159+
bottom: Int
160+
) {
161+
it.onMarginChanged(start, top, end, bottom)
162+
}
163+
164+
})
165+
}
166+
entry.key to WeakReference(widget)
151167
}?.toMap()
152-
onUnloadCallback = onUnload
153-
onLoadCallback = onLoad
154-
onShouldHideLoadingIndicatorCallback = onShouldHideLoadingIndicator
155-
onShouldShowLoadingIndicatorCallback = onShouldShowLoadingIndicator
168+
169+
this.mpRoktEventCallback = mpRoktEventCallback
156170
val finalAttributes: HashMap<String, String> = HashMap<String, String>()
157171
filterUser?.userAttributes?.let { userAttrs ->
158172
for ((key, value) in userAttrs) {
159173
finalAttributes[key] = value.toString()
160174
}
161175
}
162-
filterAttributes(finalAttributes, configuration).let {
163-
finalAttributes.putAll(it)
164-
}
165-
filterUser?.id?.let { mpid ->
166-
finalAttributes.put(MPID, mpid.toString())
167-
} ?: run {
168-
Logger.warning("RoktKit: No user ID available for placement")
176+
177+
filterUser?.id?.toString()?.let { mpid ->
178+
finalAttributes[MPID] = mpid
179+
} ?: Logger.warning("RoktKit: No user ID available for placement")
180+
181+
addIdentityAttributes(finalAttributes, filterUser)
182+
183+
184+
val SANDBOX_MODE_ROKT: String = "sandbox"
185+
attributes?.get(SANDBOX_MODE_ROKT)?.let { value ->
186+
finalAttributes.put(SANDBOX_MODE_ROKT, value)
169187
}
170188

171189
Rokt.execute(
@@ -178,15 +196,49 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
178196
)
179197
}
180198

181-
private fun filterAttributes(attributes: Map<String, String>, kitConfiguration: KitConfiguration): Map<String, String> {
182-
val userAttributes: MutableMap<String, String> = HashMap<String, String>()
183-
for ((key, value) in attributes) {
184-
val hashKey = KitUtils.hashForFiltering(key)
185-
if (!kitConfiguration.mAttributeFilters.get(hashKey)) {
186-
userAttributes[key] = value
199+
200+
private fun addIdentityAttributes(attributes: MutableMap<String, String>?, filterUser: FilteredMParticleUser?): MutableMap<String, String> {
201+
val identityAttributes = mutableMapOf<String, String>()
202+
if (filterUser != null) {
203+
for ((identityNumberKey, identityValue) in filterUser.userIdentities) {
204+
val identityType = getStringForIdentity(identityNumberKey)
205+
identityAttributes[identityType] = identityValue
187206
}
188207
}
189-
return userAttributes
208+
if (attributes != null) {
209+
attributes.putAll(identityAttributes)
210+
return attributes
211+
} else {
212+
return identityAttributes
213+
}
214+
}
215+
216+
private fun getStringForIdentity(identityType: IdentityType): String {
217+
return when (identityType) {
218+
IdentityType.Other -> "other"
219+
IdentityType.CustomerId -> "customerid"
220+
IdentityType.Facebook -> "facebook"
221+
IdentityType.Twitter -> "twitter"
222+
IdentityType.Google -> "google"
223+
IdentityType.Microsoft -> "microsoft"
224+
IdentityType.Yahoo -> "yahoo"
225+
IdentityType.Email -> "email"
226+
IdentityType.Alias -> "alias"
227+
IdentityType.FacebookCustomAudienceId -> "facebookcustomaudienceid"
228+
IdentityType.Other2 -> "other2"
229+
IdentityType.Other3 -> "other3"
230+
IdentityType.Other4 -> "other4"
231+
IdentityType.Other5 -> "other5"
232+
IdentityType.Other6 -> "other6"
233+
IdentityType.Other7 -> "other7"
234+
IdentityType.Other8 -> "other8"
235+
IdentityType.Other9 -> "other9"
236+
IdentityType.Other10 -> "other10"
237+
IdentityType.MobileNumber -> "mobilenumber"
238+
IdentityType.PhoneNumber2 -> "phonenumber2"
239+
IdentityType.PhoneNumber3 -> "phonenumber3"
240+
else -> ""
241+
}
190242
}
191243

192244
companion object {
@@ -197,20 +249,32 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
197249
const val NO_APP_VERSION_FOUND = "No App version found, can't initialize kit."
198250
}
199251

252+
200253
override fun onLoad() : Unit{
201-
onLoadCallback?.run()
254+
mpRoktEventCallback?.onLoad()
202255
}
203256

204257
override fun onShouldHideLoadingIndicator() : Unit {
205-
onShouldHideLoadingIndicatorCallback?.run()
258+
mpRoktEventCallback?.onShouldHideLoadingIndicator()
206259
}
207260

208261
override fun onShouldShowLoadingIndicator() : Unit {
209-
onShouldShowLoadingIndicatorCallback?.run()
262+
mpRoktEventCallback?.onShouldShowLoadingIndicator()
210263
}
211264

212265
override fun onUnload(reason: Rokt.UnloadReasons) : Unit {
213-
onUnloadCallback?.run()
266+
mpRoktEventCallback?.onUnload(
267+
when (reason) {
268+
Rokt.UnloadReasons.NO_OFFERS -> MParticle.UnloadReasons.NO_OFFERS
269+
Rokt.UnloadReasons.FINISHED -> MParticle.UnloadReasons.FINISHED
270+
Rokt.UnloadReasons.TIMEOUT -> MParticle.UnloadReasons.TIMEOUT
271+
Rokt.UnloadReasons.NETWORK_ERROR -> MParticle.UnloadReasons.NETWORK_ERROR
272+
Rokt.UnloadReasons.NO_WIDGET -> MParticle.UnloadReasons.NO_WIDGET
273+
Rokt.UnloadReasons.INIT_FAILED -> MParticle.UnloadReasons.INIT_FAILED
274+
Rokt.UnloadReasons.UNKNOWN_PLACEHOLDER -> MParticle.UnloadReasons.UNKNOWN_PLACEHOLDER
275+
Rokt.UnloadReasons.UNKNOWN -> MParticle.UnloadReasons.UNKNOWN
276+
}
277+
)
214278
}
215279
}
216280

0 commit comments

Comments
 (0)