@@ -248,32 +248,12 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
248248 */
249249 private func createUserFromLegacyPlayer( _ playerId: String ) {
250250 // 1. Create the Push Subscription Model
251- let sharedUserDefaults = OneSignalUserDefaults . initShared ( )
252- let reachable = OSNotificationsManager . currentPermissionState. reachable
253- let pushToken = sharedUserDefaults. getSavedString ( forKey: OSUD_PUSH_TOKEN, defaultValue: nil )
254- let pushSubscriptionModel = OSSubscriptionModel (
255- type: . push,
256- address: pushToken,
257- subscriptionId: playerId,
258- reachable: reachable,
259- isDisabled: false , // TODO: Get from cache or something if there?
260- changeNotifier: OSEventProducer ( ) )
261-
262- // 2. Add pushSubscription to store
263- pushSubscriptionModelStore. add ( id: OS_PUSH_SUBSCRIPTION_MODEL_KEY, model: pushSubscriptionModel, hydrating: false )
264-
265- // 3. Set the internal user
266-
267- let identityModel = OSIdentityModel ( aliases: nil , changeNotifier: OSEventProducer ( ) )
268- self . identityModelStore. add ( id: OS_IDENTITY_MODEL_KEY, model: identityModel, hydrating: false )
251+ let pushSubscriptionModel = createDefaultPushSubscription ( subscriptionId: playerId)
269252
270- let propertiesModel = OSPropertiesModel ( changeNotifier : OSEventProducer ( ) )
271- self . propertiesModelStore . add ( id : OS_PROPERTIES_MODEL_KEY , model : propertiesModel , hydrating : false )
253+ // 2. Set the internal user
254+ let newUser = setNewInternalUser ( externalId : nil , pushSubscriptionModel : pushSubscriptionModel )
272255
273- let newUser = OSUserInternalImpl ( identityModel: identityModel, propertiesModel: propertiesModel, pushSubscriptionModel: pushSubscriptionModel)
274- _user = newUser
275-
276- // 4. Do the request
256+ // 3. Make the request
277257 OSUserExecutor . fetchIdentityBySubscription ( newUser)
278258 }
279259
@@ -331,6 +311,13 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
331311 )
332312 }
333313
314+ /**
315+ Returns if the OSIdentityModel passed in belongs to the current user. This method is used in deciding whether or not to hydrate via a server response, for example.
316+ */
317+ func isCurrentUser( _ identityModel: OSIdentityModel ) -> Bool {
318+ return self . identityModelStore. getModel ( modelId: identityModel. modelId) != nil
319+ }
320+
334321 /**
335322 Clears the existing user's data in preparation for hydration via a fetch user call.
336323 */
@@ -420,7 +407,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
420407
421408 // TODO: We will have to save subscription_id and push_token to user defaults when we get them
422409
423- let pushSubscription = pushSubscriptionModel ?? createDefaultPushSubscription ( )
410+ let pushSubscription = pushSubscriptionModel ?? createDefaultPushSubscription ( subscriptionId : nil )
424411
425412 // Add pushSubscription to store if not present
426413 if !pushSubscriptionModelStore. getModels ( ) . keys. contains ( OS_PUSH_SUBSCRIPTION_MODEL_KEY) {
@@ -431,11 +418,14 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
431418 return self . user
432419 }
433420
434- func createDefaultPushSubscription( ) -> OSSubscriptionModel {
421+ /**
422+ Creates a default Push Subscription Model using the optionally passed in subscriptionId. An scenario where the subscriptionId will be passed in is when we are converting a legacy player's information from 3.x into a Push Subscription Model.
423+ */
424+ func createDefaultPushSubscription( subscriptionId: String ? ) -> OSSubscriptionModel {
435425 let sharedUserDefaults = OneSignalUserDefaults . initShared ( )
436426 let reachable = OSNotificationsManager . currentPermissionState. reachable
437427 let token = sharedUserDefaults. getSavedString ( forKey: OSUD_PUSH_TOKEN, defaultValue: nil )
438- let subscriptionId = sharedUserDefaults. getSavedString ( forKey: OSUD_PUSH_SUBSCRIPTION_ID, defaultValue: nil )
428+ let subscriptionId = subscriptionId ?? sharedUserDefaults. getSavedString ( forKey: OSUD_PUSH_SUBSCRIPTION_ID, defaultValue: nil )
439429
440430 return OSSubscriptionModel ( type: . push,
441431 address: token,
0 commit comments