@@ -2,7 +2,6 @@ package com.leanplum.migration.wrapper
22
33import android.app.Application
44import android.content.Context
5- import android.text.TextUtils
65import com.clevertap.android.sdk.ActivityLifecycleCallback
76import com.clevertap.android.sdk.CleverTapAPI
87import com.clevertap.android.sdk.CleverTapInstanceConfig
@@ -15,14 +14,13 @@ import com.leanplum.migration.MigrationManager
1514import com.leanplum.migration.push.FcmMigrationHandler
1615import com.leanplum.migration.push.HmsMigrationHandler
1716import com.leanplum.migration.push.MiPushMigrationHandler
18- import com.leanplum.utils.StringPreferenceNullable
1917
2018internal class CTWrapper (
2119 private val accountId : String ,
2220 private val accountToken : String ,
2321 private val accountRegion : String ,
24- private val deviceId : String ,
25- private var userId : String?
22+ deviceId : String ,
23+ userId : String?
2624) : IWrapper by StaticMethodsWrapper {
2725
2826 override val fcmHandler: FcmMigrationHandler = FcmMigrationHandler ()
@@ -32,36 +30,7 @@ internal class CTWrapper(
3230 private var cleverTapInstance: CleverTapAPI ? = null
3331 private var instanceCallback: CleverTapInstanceCallback ? = null
3432
35- /* *
36- * Anonymous data will be merged to first user that logs-in, but CT ID will remain the same as
37- * the anonymous' deviceId to allow the merge.
38- */
39- private var firstLoginUserId: String? by StringPreferenceNullable (" ct_first_login_userid" )
40-
41- /* *
42- * Needs to be calculated each time.
43- */
44- private fun cleverTapId (): String {
45- return when (userId) {
46- null -> deviceId
47- deviceId -> deviceId
48- firstLoginUserId -> deviceId
49- else -> " ${deviceId} _${userId} "
50- }
51- }
52-
53- /* *
54- * Needs to be calculated each time.
55- */
56- private fun identity (): String {
57- return when (val userId = userId) {
58- null -> deviceId
59- deviceId -> deviceId
60- else -> userId
61- }
62- }
63-
64- private fun isAnonymous () = userId == deviceId
33+ private var identityManager = IdentityManager (deviceId, userId ? : deviceId)
6534
6635 override fun launch (context : Context , callback : CleverTapInstanceCallback ? ) {
6736 instanceCallback = callback
@@ -74,19 +43,18 @@ internal class CTWrapper(
7443 enableCustomCleverTapId = true
7544 }
7645
77- val cleverTapId = cleverTapId()
78- val identity = identity ()
46+ val cleverTapId = identityManager. cleverTapId()
47+ val profile = identityManager.profile ()
7948 Log .d(" Wrapper: using CleverTapID=__h$cleverTapId " )
8049
8150 cleverTapInstance = CleverTapAPI .instanceWithConfig(context, config, cleverTapId)?.apply {
8251 setLibrary(" Leanplum" )
8352 if (! ActivityLifecycleCallback .registered) {
8453 ActivityLifecycleCallback .register(context.applicationContext as ? Application )
8554 }
86- if (isAnonymous()) {
55+ if (identityManager. isAnonymous()) {
8756 Log .d(" Wrapper: identity not set for anonymous user" )
8857 } else {
89- val profile: Map <String , Any > = mutableMapOf (MigrationConstants .IDENTITY to identity)
9058 Log .d(" Wrapper: will call onUserLogin with $profile and __h$cleverTapId " )
9159 onUserLogin(profile, cleverTapId)
9260 }
@@ -110,25 +78,16 @@ internal class CTWrapper(
11078 }
11179
11280 override fun setUserId (userId : String? ) {
113- if (TextUtils .isEmpty(userId)) return
114- if (this .userId == userId) return
115-
116- val wasAnonymous = isAnonymous()
117- this .userId = userId
118-
119- val identity = identity()
120- val profile: Map <String , Any > = mutableMapOf (MigrationConstants .IDENTITY to identity)
121-
122- if (wasAnonymous) {
123- firstLoginUserId = userId
124- Log .d(" Wrapper: anonymous data will be merged to $firstLoginUserId " )
125- Log .d(" Wrapper: Leanplum.setUserId will call onUserLogin with $profile and __h$deviceId " )
126- cleverTapInstance?.onUserLogin(profile, deviceId)
127- } else {
128- val cleverTapId = cleverTapId()
129- Log .d(" Wrapper: Leanplum.setUserId will call onUserLogin with $profile and __h$cleverTapId " )
130- cleverTapInstance?.onUserLogin(profile, cleverTapId)
131- }
81+ if (userId == null || userId.isEmpty()) return
82+ if (identityManager.getUserId() == userId) return
83+
84+ identityManager.setUserId(userId)
85+
86+ val cleverTapId = identityManager.cleverTapId()
87+ val profile = identityManager.profile()
88+
89+ Log .d(" Wrapper: Leanplum.setUserId will call onUserLogin with $profile and __h$cleverTapId " )
90+ cleverTapInstance?.onUserLogin(profile, cleverTapId)
13291 }
13392
13493 /* *
0 commit comments