Skip to content

Commit

Permalink
Update OSIamFetchReadyCondition to sharedInstance to waist on sub update
Browse files Browse the repository at this point in the history
Motivation: if we have a subscription update enqueued, we need to consider it. Otherwise we should just consider the userUpdateTokenSet.
  • Loading branch information
rgomezp committed Oct 10, 2024
1 parent c712ce2 commit a30f51b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ - (void)getInAppMessagesFromServer:(NSString *)subscriptionId {
return;
}

OSIamFetchReadyCondition *condition = [[OSIamFetchReadyCondition alloc] initWithId:onesignalId];
OSIamFetchReadyCondition *condition = [OSIamFetchReadyCondition sharedInstance];
[condition setOneSignalId:onesignalId];
NSString *rywToken = [consistencyManager registerCondition:condition forId:onesignalId];

NSNumber *sessionDuration = @([OSSessionManager getTimeFocusedElapsed]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
//
// OSIamFetchReadyCondition.swift
// OneSignalOSCore
//
// Created by Rodrigo Gomez-Palacio on 9/10/24.
// Copyright © 2024 Hiptic. All rights reserved.
//

import Foundation

@objc public class OSIamFetchReadyCondition: NSObject, OSCondition {
// the id used to index the token map (e.g. onesignalId)
private let id: String
private var id: String
private var hasSubscriptionUpdate: Bool = false

// Singleton shared instance
@objc public static let sharedInstance = OSIamFetchReadyCondition(id: "")

// Private initializer to prevent external instantiation
private init(id: String) {
self.id = id
}

@objc public init(id: String) {
// Method to set the ID
@objc public func setOneSignalId(_ id: String) {
self.id = id
}

// Expose the constant to Objective-C
@objc public static let CONDITIONID: String = "OSIamFetchReadyCondition"

// class-level constant for the ID
public static let CONDITIONID = "OSIamFetchReadyCondition"
public func hasSubscriptionUpdate(value: Bool) {
hasSubscriptionUpdate = value
}

public var conditionId: String {
return OSIamFetchReadyCondition.CONDITIONID
return OSIamFetchReadyCondition.CONDITIONID
}

// Determine if the condition is met based on the indexed tokens
public func isMet(indexedTokens: [String: [NSNumber: String]]) -> Bool {
guard let tokenMap = indexedTokens[id] else { return false }

let userUpdateTokenSet = tokenMap[NSNumber(value: OSIamFetchOffsetKey.userUpdate.rawValue)] != nil
let subscriptionUpdateTokenSet = tokenMap[NSNumber(value: OSIamFetchOffsetKey.subscriptionUpdate.rawValue)] != nil

if (hasSubscriptionUpdate) {
return userUpdateTokenSet && subscriptionUpdateTokenSet
}
return userUpdateTokenSet
}

// Get the newest token for this condition
public func getNewestToken(indexedTokens: [String: [NSNumber: String]]) -> String? {
guard let tokenMap = indexedTokens[id] else { return nil }

// Get the token strings for userUpdate and subscriptionUpdate
let userUpdateToken = tokenMap[NSNumber(value: OSIamFetchOffsetKey.userUpdate.rawValue)]
let subscriptionUpdateToken = tokenMap[NSNumber(value: OSIamFetchOffsetKey.subscriptionUpdate.rawValue)]

self.hasSubscriptionUpdate(value: false)
// Compare the strings lexicographically and return the maximum
let newestToken = [userUpdateToken, subscriptionUpdateToken].compactMap { $0 }.max()

return newestToken
return [userUpdateToken, subscriptionUpdateToken].compactMap { $0 }.max()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class OSSubscriptionModelStoreListener: OSModelStoreListener {
}

func getUpdateModelDelta(_ args: OSModelChangedArgs) -> OSDelta? {
OSIamFetchReadyCondition.sharedInstance.hasSubscriptionUpdate(value: true)
return OSDelta(
name: OS_UPDATE_SUBSCRIPTION_DELTA,
identityModelId: OneSignalUserManagerImpl.sharedInstance.user.identityModel.modelId,
Expand Down

0 comments on commit a30f51b

Please sign in to comment.