Skip to content

Commit 6584c0e

Browse files
authored
refactor: Route bulk user attribute sync through onSetAllUserAttributes (#691)
* Delegate onSetAllUserAttributes to setAllUserAttributes in dual-listener kits AppsFlyer and Braze: forward snapshot maps to AttributeListener.setAllUserAttributes. Singular: align override parameter names with UserAttributeListener and delegate. Made-with: Cursor * Move onSetAllUserAttributes to BaseAttributeListener; route sync via one call - Declare onSetAllUserAttributes on BaseAttributeListener; remove from UserAttributeListener - KitManagerImpl: call BaseAttributeListener.onSetAllUserAttributes only (no duplicate setAllUserAttributes) - AttributeListener-only kits: delegate onSetAllUserAttributes to setAllUserAttributes - AttributeListenerTestKit: align setAllUserAttributes maps with Java API Made-with: Cursor * Hoist FilteredMParticleUser in onUserAttributesReceived Compute getInstance(mpid, provider) once before the supportsAttributeLists branch. Made-with: Cursor * Remove setAllUserAttributes from AttributeListener Drop the method from KitIntegration.AttributeListener; full sync is delivered via BaseAttributeListener.onSetAllUserAttributes. Kits keep setAllUserAttributes as ordinary helpers (no override). Update KitManagerImplTest and AttributeListenerTestKit accordingly. Made-with: Cursor * refactor(kits): inline setAllUserAttributes into onSetAllUserAttributes Remove redundant setAllUserAttributes helpers from kits and AttributeListenerTestKit. Update BaseAttributeListener Javadoc in KitIntegration. Made-with: Cursor
1 parent eca7329 commit 6584c0e

18 files changed

Lines changed: 177 additions & 207 deletions

File tree

android-kit-base/src/androidTest/kotlin/com/mparticle/kits/testkits/AttributeListenerTestKit.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ open class AttributeListenerTestKit :
3333
onAttributeReceived?.invoke(attributeKey, attributeValueList)
3434
}
3535

36-
override fun setAllUserAttributes(
36+
override fun onSetAllUserAttributes(
3737
userAttributes: Map<String, String>,
38-
userAttributeLists: Map<String, MutableList<String>>,
38+
userAttributeLists: Map<String, List<String>>,
39+
user: FilteredMParticleUser,
3940
) {
4041
setAllUserAttributes?.invoke(userAttributes, userAttributeLists)
4142
userAttributes.forEach { onAttributeReceived?.invoke(it.key, it.value) }

android-kit-base/src/main/java/com/mparticle/kits/KitIntegration.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,23 @@ void onSetUserAttributeList(
408408
@Nullable String attributeKey,
409409
@Nullable List<String> attributeValueList,
410410
@Nullable FilteredMParticleUser user);
411+
412+
/**
413+
* Called when the full set of user attributes is synchronized for the current user.
414+
*
415+
* @param userAttributes scalar user attributes
416+
* @param userAttributeLists list-valued user attributes when {@link #supportsAttributeLists()} is true;
417+
* otherwise list values may be merged into scalars by the framework
418+
* @param user filtered user context for this kit
419+
*/
420+
void onSetAllUserAttributes(
421+
Map<String, String> userAttributes,
422+
Map<String, List<String>> userAttributeLists,
423+
FilteredMParticleUser user);
411424
}
412425

413426
public interface AttributeListener extends BaseAttributeListener {
414427

415-
void setAllUserAttributes(Map<String, String> userAttributes, Map<String, List<String>> userAttributeLists);
416-
417428
void setUserIdentity(MParticle.IdentityType identityType, String identity);
418429

419430
void removeUserIdentity(MParticle.IdentityType identityType);
@@ -575,9 +586,8 @@ public interface UserAttributeListener extends BaseAttributeListener {
575586

576587
void onSetUserTag(String key, FilteredMParticleUser user);
577588

578-
void onSetAllUserAttributes(Map<String, String> userAttributes, Map<String, List<String>> userAttributeLists, FilteredMParticleUser user);
579-
580589
void onConsentStateUpdated(ConsentState oldState, ConsentState newState, FilteredMParticleUser user);
590+
581591
}
582592

583593
public interface BatchListener {

android-kit-base/src/main/java/com/mparticle/kits/KitManagerImpl.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -599,34 +599,22 @@ public void onUserAttributesReceived(Map<String, String> userAttributes, Map<Str
599599
userAttributeLists = mDataplanFilter.transformUserAttributes(userAttributeLists);
600600
for (KitIntegration provider : providers.values()) {
601601
try {
602-
if ((provider instanceof KitIntegration.AttributeListener || provider instanceof KitIntegration.UserAttributeListener)
602+
if ((provider instanceof KitIntegration.BaseAttributeListener listener)
603603
&& !provider.isDisabled()) {
604604
Map<String, String> filteredAttributeSingles = (Map<String, String>) KitConfiguration.filterAttributes(provider.getConfiguration().getUserAttributeFilters(),
605605
userAttributes);
606606
Map<String, List<String>> filteredAttributeLists = (Map<String, List<String>>) KitConfiguration.filterAttributes(provider.getConfiguration().getUserAttributeFilters(),
607607
userAttributeLists);
608-
boolean supportsAttributeLists = ((KitIntegration.BaseAttributeListener) provider).supportsAttributeLists();
609-
if (provider instanceof KitIntegration.AttributeListener) {
610-
if (supportsAttributeLists) {
611-
((KitIntegration.AttributeListener) provider).setAllUserAttributes(filteredAttributeSingles, filteredAttributeLists);
612-
} else {
613-
Map<String, String> singlesCopy = new HashMap<>(filteredAttributeSingles);
614-
for (Map.Entry<String, List<String>> entry : filteredAttributeLists.entrySet()) {
615-
singlesCopy.put(entry.getKey(), KitUtils.join(entry.getValue()));
616-
}
617-
((KitIntegration.AttributeListener) provider).setAllUserAttributes(singlesCopy, new HashMap<String, List<String>>());
618-
}
619-
}
620-
if (provider instanceof KitIntegration.UserAttributeListener) {
621-
if (supportsAttributeLists) {
622-
((KitIntegration.UserAttributeListener) provider).onSetAllUserAttributes(filteredAttributeSingles, filteredAttributeLists, FilteredMParticleUser.getInstance(mpid, provider));
623-
} else {
624-
Map<String, String> singlesCopy = new HashMap<>(filteredAttributeSingles);
625-
for (Map.Entry<String, List<String>> entry : filteredAttributeLists.entrySet()) {
626-
singlesCopy.put(entry.getKey(), KitUtils.join(entry.getValue()));
627-
}
628-
((KitIntegration.UserAttributeListener) provider).onSetAllUserAttributes(singlesCopy, new HashMap<String, List<String>>(), FilteredMParticleUser.getInstance(mpid, provider));
608+
boolean supportsAttributeLists = listener.supportsAttributeLists();
609+
FilteredMParticleUser filteredUser = FilteredMParticleUser.getInstance(mpid, provider);
610+
if (supportsAttributeLists) {
611+
listener.onSetAllUserAttributes(filteredAttributeSingles, filteredAttributeLists, filteredUser);
612+
} else {
613+
Map<String, String> singlesCopy = new HashMap<>(filteredAttributeSingles);
614+
for (Map.Entry<String, List<String>> entry : filteredAttributeLists.entrySet()) {
615+
singlesCopy.put(entry.getKey(), KitUtils.join(entry.getValue()));
629616
}
617+
listener.onSetAllUserAttributes(singlesCopy, new HashMap<String, List<String>>(), filteredUser);
630618
}
631619
}
632620
} catch (Exception e) {

android-kit-base/src/test/kotlin/com/mparticle/kits/KitManagerImplTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,15 +519,15 @@ class KitManagerImplTest {
519519
attributeList.add("3")
520520
userAttributeLists["test 3"] = attributeList
521521
manager.onUserAttributesReceived(userAttributeSingles, userAttributeLists, 1L)
522-
verify(integration as AttributeListener, Mockito.times(1))
523-
.setAllUserAttributes(userAttributeSingles, userAttributeLists)
522+
verify(integration as BaseAttributeListener, Mockito.times(1))
523+
.onSetAllUserAttributes(eq(userAttributeSingles), eq(userAttributeLists), any())
524524
val userAttributesCombined: MutableMap<String, String> = HashMap()
525525
userAttributesCombined["test"] = "whatever"
526526
userAttributesCombined["test 2"] = "whatever 2"
527527
userAttributesCombined["test 3"] = "1,2,3"
528528
val clearedOutList: Map<String, List<String>> = HashMap()
529-
verify(integration2 as AttributeListener, Mockito.times(1))
530-
.setAllUserAttributes(userAttributesCombined, clearedOutList)
529+
verify(integration2 as BaseAttributeListener, Mockito.times(1))
530+
.onSetAllUserAttributes(eq(userAttributesCombined), eq(clearedOutList), any())
531531
}
532532

533533
@Test

kits/adobe/adobe-5/src/main/kotlin/com/mparticle/kits/AdobeKitBase.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ abstract class AdobeKitBase :
5858

5959
override fun supportsAttributeLists(): Boolean = false
6060

61-
override fun setAllUserAttributes(
62-
map: Map<String, String>,
63-
map1: Map<String, List<String>>,
61+
override fun onSetAllUserAttributes(
62+
userAttributes: Map<String, String>,
63+
userAttributeLists: Map<String, List<String>>,
64+
user: FilteredMParticleUser,
6465
) {
6566
syncIds()
6667
}

kits/adobemedia/adobemedia-5/src/main/kotlin/com/mparticle/kits/AdobeKit.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ open class AdobeKit :
9595

9696
override fun supportsAttributeLists(): Boolean = false
9797

98-
override fun setAllUserAttributes(
99-
map: Map<String, String>,
100-
map1: Map<String, List<String>>,
98+
override fun onSetAllUserAttributes(
99+
userAttributes: Map<String, String>,
100+
userAttributeLists: Map<String, List<String>>,
101+
user: FilteredMParticleUser,
101102
) {
102103
syncIds()
103104
}

kits/appsflyer/appsflyer-6/src/main/kotlin/com/mparticle/kits/AppsFlyerKit.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,11 @@ class AppsFlyerKit :
289289
userAttributeLists: MutableMap<String, MutableList<String>>?,
290290
user: FilteredMParticleUser?,
291291
) {
292+
// No-op: this kit does not implement this feature.
292293
}
293294

294295
override fun supportsAttributeLists(): Boolean = true
295296

296-
override fun setAllUserAttributes(
297-
map: Map<String, String>,
298-
map1: Map<String, List<String>>,
299-
) {
300-
// No-op: this kit does not implement this feature.
301-
}
302-
303297
override fun removeUserIdentity(identityType: MParticle.IdentityType) {
304298
with(instance) {
305299
if (MParticle.IdentityType.CustomerId == identityType) {

kits/apptimize/apptimize-3/src/main/kotlin/com/mparticle/kits/ApptimizeKit.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,14 @@ class ApptimizeKit :
132132
override fun supportsAttributeLists(): Boolean = false
133133

134134
/**
135-
* @param attributeLists is ignored by the Apptimize kit.
135+
* [userAttributeLists] is ignored by the Apptimize kit.
136136
*/
137-
override fun setAllUserAttributes(
138-
attributes: Map<String, String>,
139-
attributeLists: Map<String, List<String>>,
137+
override fun onSetAllUserAttributes(
138+
userAttributes: Map<String, String>,
139+
userAttributeLists: Map<String, List<String>>,
140+
user: FilteredMParticleUser,
140141
) {
141-
for ((key, value) in attributes) {
142+
for ((key, value) in userAttributes) {
142143
Apptimize.setUserAttribute(key, value)
143144
}
144145
}

kits/branch/branch-5/src/main/kotlin/com/mparticle/kits/BranchMetricsKit.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,12 @@ class BranchMetricsKit :
184184

185185
override fun supportsAttributeLists(): Boolean = true
186186

187-
override fun setAllUserAttributes(
188-
map: Map<String, String>,
189-
map1: Map<String, List<String>>,
190-
) {}
187+
override fun onSetAllUserAttributes(
188+
userAttributes: Map<String, String>,
189+
userAttributeLists: Map<String, List<String>>,
190+
user: FilteredMParticleUser,
191+
) {
192+
}
191193

192194
override fun onRemoveUserAttribute(
193195
key: String,

kits/braze/braze-38/src/main/kotlin/com/mparticle/kits/AppboyKit.kt

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,31 @@ open class AppboyKit :
535535
userAttributeLists: MutableMap<String, MutableList<String>>?,
536536
user: FilteredMParticleUser?,
537537
) {
538+
val attributes = userAttributes ?: emptyMap()
539+
val attributeLists = userAttributeLists ?: emptyMap()
540+
if (!kitPreferences.getBoolean(PREF_KEY_HAS_SYNCED_ATTRIBUTES, false)) {
541+
for ((key, value) in attributes) {
542+
applyScalarUserAttribute(key, value)
543+
}
544+
for ((key, value) in attributeLists) {
545+
Braze
546+
.getInstance(context)
547+
.getCurrentUser(
548+
object : IValueCallback<BrazeUser> {
549+
override fun onSuccess(brazeUser: BrazeUser) {
550+
val array = value.toTypedArray<String?>()
551+
brazeUser.setCustomAttributeArray(key, array)
552+
queueDataFlush()
553+
}
554+
555+
override fun onError() {
556+
Logger.warning("unable to set key: " + key + " with User Attribute List: " + value)
557+
}
558+
},
559+
)
560+
}
561+
kitPreferences.edit().putBoolean(PREF_KEY_HAS_SYNCED_ATTRIBUTES, true).apply()
562+
}
538563
}
539564

540565
override fun supportsAttributeLists(): Boolean = true
@@ -672,38 +697,6 @@ open class AppboyKit :
672697
dataFlushRunnable?.let { dataFlushHandler.postDelayed(it, FLUSH_DELAY.toLong()) }
673698
}
674699

675-
/**
676-
* This is called when the Kit is added to the mParticle SDK, typically on app-startup.
677-
*/
678-
override fun setAllUserAttributes(
679-
attributes: Map<String, String>,
680-
attributeLists: Map<String, List<String>>,
681-
) {
682-
if (!kitPreferences.getBoolean(PREF_KEY_HAS_SYNCED_ATTRIBUTES, false)) {
683-
for ((key, value) in attributes) {
684-
applyScalarUserAttribute(key, value)
685-
}
686-
for ((key, value) in attributeLists) {
687-
Braze
688-
.getInstance(context)
689-
.getCurrentUser(
690-
object : IValueCallback<BrazeUser> {
691-
override fun onSuccess(brazeUser: BrazeUser) {
692-
val array = value.toTypedArray<String?>()
693-
brazeUser.setCustomAttributeArray(key, array)
694-
queueDataFlush()
695-
}
696-
697-
override fun onError() {
698-
Logger.warning("unable to set key: " + key + " with User Attribute List: " + value)
699-
}
700-
},
701-
)
702-
}
703-
kitPreferences.edit().putBoolean(PREF_KEY_HAS_SYNCED_ATTRIBUTES, true).apply()
704-
}
705-
}
706-
707700
override fun setUserIdentity(
708701
identityType: IdentityType,
709702
identity: String,

0 commit comments

Comments
 (0)