Skip to content

Commit

Permalink
Have an Identity Model Repo for models still being used
Browse files Browse the repository at this point in the history
* Introduce OSIdentityModelRepo managed by the User Manager where a dictionary of identity models live
* This will hold identity models added when users change
* When requests are uncached, their identity models will also be added as they are still being used.
* This is a lightweight option over modifying the Identity Model Store (which is an option if we wish to use the model store to manage all identity models)
  • Loading branch information
nan-li committed Apr 25, 2024
1 parent f6bbc99 commit e358a4f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
4 changes: 4 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
3C14E39F2AFAE39B006ED053 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C14E39E2AFAE39B006ED053 /* PrivacyInfo.xcprivacy */; };
3C14E3A12AFAE461006ED053 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C14E3A02AFAE461006ED053 /* PrivacyInfo.xcprivacy */; };
3C14E3A42AFAE54C006ED053 /* OneSignalSwiftInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC08AFF2947D4E900C81DA3 /* OneSignalSwiftInterface.swift */; };
3C277D7E2BD76E0000857606 /* OSIdentityModelRepo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C277D7D2BD76E0000857606 /* OSIdentityModelRepo.swift */; };
3C2C7DC8288F3C020020F9AE /* OSSubscriptionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */; };
3C2D8A5928B4C4E300BE41F6 /* OSDelta.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2D8A5828B4C4E300BE41F6 /* OSDelta.swift */; };
3C44673E296D099D0039A49E /* OneSignalMobileProvision.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */; };
Expand Down Expand Up @@ -954,6 +955,7 @@
3C11518C289AF5E800565C41 /* OSModelChangedHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSModelChangedHandler.swift; sourceTree = "<group>"; };
3C14E39E2AFAE39B006ED053 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
3C14E3A02AFAE461006ED053 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
3C277D7D2BD76E0000857606 /* OSIdentityModelRepo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSIdentityModelRepo.swift; sourceTree = "<group>"; };
3C2C7DC2288E007E0020F9AE /* UnitTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UnitTests-Bridging-Header.h"; sourceTree = "<group>"; };
3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSSubscriptionModel.swift; sourceTree = "<group>"; };
3C2D8A5828B4C4E300BE41F6 /* OSDelta.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSDelta.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1956,6 +1958,7 @@
DE69E1A9282ED8790090BB3D /* UnitTestApp-Bridging-Header.h */,
3C0EF49D28A1DBCB00E5434B /* OSUserInternalImpl.swift */,
DE69E1AA282ED8790090BB3D /* OneSignalUserManagerImpl.swift */,
3C277D7D2BD76E0000857606 /* OSIdentityModelRepo.swift */,
3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */,
3CE92279289FA88B001B1062 /* OSIdentityModelStoreListener.swift */,
3CF8629D28A183F900776CA4 /* OSIdentityModel.swift */,
Expand Down Expand Up @@ -3488,6 +3491,7 @@
3CF862A028A1964F00776CA4 /* OSPropertiesModel.swift in Sources */,
3C8E6E0128AC0BA10031E48A /* OSIdentityOperationExecutor.swift in Sources */,
3CF862A228A197D200776CA4 /* OSPropertiesModelStoreListener.swift in Sources */,
3C277D7E2BD76E0000857606 /* OSIdentityModelRepo.swift in Sources */,
3C9AD6C12B22886600BC1540 /* OSRequestUpdateSubscription.swift in Sources */,
3C0EF49E28A1DBCB00E5434B /* OSUserInternalImpl.swift in Sources */,
3C8E6DFF28AB09AE0031E48A /* OSPropertyOperationExecutor.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Modified MIT License
Copyright 2024 OneSignal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import Foundation

/**
This class stores all Identity Models that are being used during an app session.
Its purpose is to manage the instances for all referencing objects.
The models are built up on each new cold start, so no caching occurs.
When are Identity Models added to this repo?
1. When the User Manager starts, and the Identity Model is loaded from cache.
2. When users switch and new Identity Models are created.
3. Identity Models are added when requests are uncached.
*/
class OSIdentityModelRepo {
let lock = NSLock()
var models: [String : OSIdentityModel] = [:]

func add(model: OSIdentityModel) {
lock.withLock {
models[model.modelId] = model
}
}

func get(modelId: String) -> OSIdentityModel? {
lock.withLock {
return models[modelId]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {

@objc public let pushSubscriptionImpl: OSPushSubscriptionImpl

var identityModelRepo = OSIdentityModelRepo()

private var hasCalledStart = false

private var jwtExpiredHandler: OSJwtExpiredHandler?
Expand Down Expand Up @@ -211,12 +213,14 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
let pushSubscription = pushSubscriptionModelStore.getModels()[OS_PUSH_SUBSCRIPTION_MODEL_KEY] {
hasCachedUser = true
_user = OSUserInternalImpl(identityModel: identityModel, propertiesModel: propertiesModel, pushSubscriptionModel: pushSubscription)
addIdentityModelToRepo(identityModel)
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignalUserManager.start called, loaded the user from cache.")
}

// TODO: Update the push sub model with any new state from NotificationsManager

// Setup the executors
// The OSUserExecutor has to run first, before other executors
OSUserExecutor.start()
OSOperationRepo.sharedInstance.start()

Expand Down Expand Up @@ -253,6 +257,14 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
}
}

func addIdentityModelToRepo(_ model: OSIdentityModel) {
self.identityModelRepo.add(model: model)
}

func getIdentityModel(_ modelId: String) -> OSIdentityModel? {
return identityModelRepo.get(modelId: modelId)
}

@objc
public func login(externalId: String, token: String?) {
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
Expand Down Expand Up @@ -429,6 +441,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {

let identityModel = OSIdentityModel(aliases: aliases, changeNotifier: OSEventProducer())
self.identityModelStore.add(id: OS_IDENTITY_MODEL_KEY, model: identityModel, hydrating: false)
self.addIdentityModelToRepo(identityModel)

let propertiesModel = OSPropertiesModel(changeNotifier: OSEventProducer())
self.propertiesModelStore.add(id: OS_PROPERTIES_MODEL_KEY, model: propertiesModel, hydrating: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class OneSignalUserMocks: NSObject {
public static func resetStaticUserExecutor() {
OSUserExecutor.userRequestQueue.removeAll()
OSUserExecutor.transferSubscriptionRequestQueue.removeAll()
OSUserExecutor.identityModels.removeAll()
}

/**
Expand All @@ -51,6 +50,8 @@ public class OneSignalUserMocks: NSObject {
This is adapting as more data needs to be considered and reset...
*/
public static func resetUserManager() {
OneSignalUserManagerImpl.sharedInstance.identityModelRepo.reset()

OneSignalUserManagerImpl.sharedInstance.identityModelStore.clearModelsFromStore()
OneSignalUserManagerImpl.sharedInstance.propertiesModelStore.clearModelsFromStore()
OneSignalUserManagerImpl.sharedInstance.subscriptionModelStore.clearModelsFromStore()
Expand All @@ -72,3 +73,9 @@ public class OneSignalUserMocks: NSObject {
OSOperationRepo.sharedInstance.addExecutor(subscriptionExecutor)
}
}

extension OSIdentityModelRepo {
func reset() {
self.models = [:]
}
}

0 comments on commit e358a4f

Please sign in to comment.