Skip to content

Commit 7667321

Browse files
Merge branch 'development' into feat/SQDSDKS-7206-sandbox-mode
2 parents e035b8f + eaf7e0e commit 7667321

File tree

2 files changed

+197
-181
lines changed

2 files changed

+197
-181
lines changed

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

Lines changed: 98 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import android.content.pm.PackageInfo
66
import android.content.pm.PackageManager
77
import android.graphics.Typeface
88
import android.os.Build
9+
import com.mparticle.MParticle
10+
import com.mparticle.MParticle.IdentityType
911
import com.mparticle.commerce.CommerceEvent
1012
import com.mparticle.identity.MParticleUser
1113
import com.mparticle.internal.Constants
1214
import com.mparticle.internal.Logger
1315
import com.mparticle.kits.KitIntegration.CommerceListener
1416
import com.mparticle.kits.KitIntegration.IdentityListener
1517
import com.mparticle.kits.KitIntegration.RoktListener
18+
import com.mparticle.rokt.RoktEmbeddedView
1619
import com.rokt.roktsdk.Rokt
20+
import com.rokt.roktsdk.RoktWidgetDimensionCallBack
1721
import com.rokt.roktsdk.Widget
1822
import java.lang.ref.WeakReference
1923
import java.math.BigDecimal
@@ -26,10 +30,7 @@ import java.math.BigDecimal
2630
*/
2731
class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListener, Rokt.RoktCallback {
2832
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
33+
private var mpRoktEventCallback: MParticle.MpRoktEventCallback? = null
3334
override fun getName(): String = NAME
3435

3536
override fun getInstance(): RoktKit = this
@@ -122,39 +123,49 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
122123
@Suppress("UNCHECKED_CAST", "CAST_NEVER_SUCCEEDS")
123124
override fun execute(
124125
viewName: String,
125-
attributes: Map<String, String>?,
126-
onUnload: Runnable?,
127-
onLoad: Runnable?,
128-
onShouldHideLoadingIndicator: Runnable?,
129-
onShouldShowLoadingIndicator: Runnable?,
130-
placeHolders: MutableMap<String, WeakReference<com.mparticle.rokt.RoktEmbeddedView>>?,
126+
attributes: Map<String, String>,
127+
mpRoktEventCallback: MParticle.MpRoktEventCallback?,
128+
placeHolders: MutableMap<String, WeakReference<RoktEmbeddedView>>?,
131129
fontTypefaces: MutableMap<String, WeakReference<Typeface>>?,
132130
filterUser: FilteredMParticleUser?
133131
) {
134-
// Converting the placeholders to a Map<String, WeakReference<Widget>> by filtering and casting each entry
135132
val placeholders: Map<String, WeakReference<Widget>>? = placeHolders?.mapNotNull { entry ->
136-
val weakRef = entry.value
137-
val widget = weakRef.get() as? Widget // Safe cast to Widget
138-
widget?.let { entry.key to weakRef as WeakReference<Widget> } // Only include if it's a Widget
133+
val widget = Widget(entry.value.get()?.context as Context)
134+
entry.value.get()?.removeAllViews()
135+
entry.value.get()?.addView(widget)
136+
entry.value.get()?.dimensionCallBack?.let {
137+
widget.registerDimensionListener(object: RoktWidgetDimensionCallBack {
138+
override fun onHeightChanged(height: Int) {
139+
it.onHeightChanged(height)
140+
}
141+
142+
override fun onMarginChanged(
143+
start: Int,
144+
top: Int,
145+
end: Int,
146+
bottom: Int
147+
) {
148+
it.onMarginChanged(start, top, end, bottom)
149+
}
150+
151+
})
152+
}
153+
entry.key to WeakReference(widget)
139154
}?.toMap()
140-
onUnloadCallback = onUnload
141-
onLoadCallback = onLoad
142-
onShouldHideLoadingIndicatorCallback = onShouldHideLoadingIndicator
143-
onShouldShowLoadingIndicatorCallback = onShouldShowLoadingIndicator
155+
156+
this.mpRoktEventCallback = mpRoktEventCallback
144157
val finalAttributes: HashMap<String, String> = HashMap<String, String>()
145158
filterUser?.userAttributes?.let { userAttrs ->
146159
for ((key, value) in userAttrs) {
147160
finalAttributes[key] = value.toString()
148161
}
149162
}
150-
filterAttributes(finalAttributes, configuration).let {
151-
finalAttributes.putAll(it)
152-
}
153-
filterUser?.id?.let { mpid ->
154-
finalAttributes.put(MPID, mpid.toString())
155-
} ?: run {
156-
Logger.warning("RoktKit: No user ID available for placement")
157-
}
163+
164+
filterUser?.id?.toString()?.let { mpid ->
165+
finalAttributes[MPID] = mpid
166+
} ?: Logger.warning("RoktKit: No user ID available for placement")
167+
168+
addIdentityAttributes(finalAttributes, filterUser)
158169

159170

160171
val SANDBOX_MODE_ROKT: String = "sandbox"
@@ -172,15 +183,49 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
172183
)
173184
}
174185

175-
private fun filterAttributes(attributes: Map<String, String>, kitConfiguration: KitConfiguration): Map<String, String> {
176-
val userAttributes: MutableMap<String, String> = HashMap<String, String>()
177-
for ((key, value) in attributes) {
178-
val hashKey = KitUtils.hashForFiltering(key)
179-
if (!kitConfiguration.mAttributeFilters.get(hashKey)) {
180-
userAttributes[key] = value
186+
187+
private fun addIdentityAttributes(attributes: MutableMap<String, String>?, filterUser: FilteredMParticleUser?): MutableMap<String, String> {
188+
val identityAttributes = mutableMapOf<String, String>()
189+
if (filterUser != null) {
190+
for ((identityNumberKey, identityValue) in filterUser.userIdentities) {
191+
val identityType = getStringForIdentity(identityNumberKey)
192+
identityAttributes[identityType] = identityValue
181193
}
182194
}
183-
return userAttributes
195+
if (attributes != null) {
196+
attributes.putAll(identityAttributes)
197+
return attributes
198+
} else {
199+
return identityAttributes
200+
}
201+
}
202+
203+
private fun getStringForIdentity(identityType: IdentityType): String {
204+
return when (identityType) {
205+
IdentityType.Other -> "other"
206+
IdentityType.CustomerId -> "customerid"
207+
IdentityType.Facebook -> "facebook"
208+
IdentityType.Twitter -> "twitter"
209+
IdentityType.Google -> "google"
210+
IdentityType.Microsoft -> "microsoft"
211+
IdentityType.Yahoo -> "yahoo"
212+
IdentityType.Email -> "email"
213+
IdentityType.Alias -> "alias"
214+
IdentityType.FacebookCustomAudienceId -> "facebookcustomaudienceid"
215+
IdentityType.Other2 -> "other2"
216+
IdentityType.Other3 -> "other3"
217+
IdentityType.Other4 -> "other4"
218+
IdentityType.Other5 -> "other5"
219+
IdentityType.Other6 -> "other6"
220+
IdentityType.Other7 -> "other7"
221+
IdentityType.Other8 -> "other8"
222+
IdentityType.Other9 -> "other9"
223+
IdentityType.Other10 -> "other10"
224+
IdentityType.MobileNumber -> "mobilenumber"
225+
IdentityType.PhoneNumber2 -> "phonenumber2"
226+
IdentityType.PhoneNumber3 -> "phonenumber3"
227+
else -> ""
228+
}
184229
}
185230

186231
companion object {
@@ -191,20 +236,32 @@ class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListen
191236
const val NO_APP_VERSION_FOUND = "No App version found, can't initialize kit."
192237
}
193238

194-
override fun onLoad(): Unit {
195-
onLoadCallback?.run()
239+
240+
override fun onLoad() : Unit{
241+
mpRoktEventCallback?.onLoad()
196242
}
197243

198-
override fun onShouldHideLoadingIndicator(): Unit {
199-
onShouldHideLoadingIndicatorCallback?.run()
244+
override fun onShouldHideLoadingIndicator() : Unit {
245+
mpRoktEventCallback?.onShouldHideLoadingIndicator()
200246
}
201247

202-
override fun onShouldShowLoadingIndicator(): Unit {
203-
onShouldShowLoadingIndicatorCallback?.run()
248+
override fun onShouldShowLoadingIndicator() : Unit {
249+
mpRoktEventCallback?.onShouldShowLoadingIndicator()
204250
}
205251

206-
override fun onUnload(reason: Rokt.UnloadReasons): Unit {
207-
onUnloadCallback?.run()
252+
override fun onUnload(reason: Rokt.UnloadReasons) : Unit {
253+
mpRoktEventCallback?.onUnload(
254+
when (reason) {
255+
Rokt.UnloadReasons.NO_OFFERS -> MParticle.UnloadReasons.NO_OFFERS
256+
Rokt.UnloadReasons.FINISHED -> MParticle.UnloadReasons.FINISHED
257+
Rokt.UnloadReasons.TIMEOUT -> MParticle.UnloadReasons.TIMEOUT
258+
Rokt.UnloadReasons.NETWORK_ERROR -> MParticle.UnloadReasons.NETWORK_ERROR
259+
Rokt.UnloadReasons.NO_WIDGET -> MParticle.UnloadReasons.NO_WIDGET
260+
Rokt.UnloadReasons.INIT_FAILED -> MParticle.UnloadReasons.INIT_FAILED
261+
Rokt.UnloadReasons.UNKNOWN_PLACEHOLDER -> MParticle.UnloadReasons.UNKNOWN_PLACEHOLDER
262+
Rokt.UnloadReasons.UNKNOWN -> MParticle.UnloadReasons.UNKNOWN
263+
}
264+
)
208265
}
209266
}
210267

0 commit comments

Comments
 (0)