-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update OSIamFetchReadyCondition to sharedInstance to waist on sub update
Motivation: if we have a subscription update enqueued, we need to consider it. Otherwise we should just consider the userUpdateTokenSet.
- Loading branch information
Showing
3 changed files
with
29 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 26 additions & 17 deletions
43
...K/OneSignalSDK/OneSignalOSCore/Source/Consistency/IamFetch/OSIamFetchReadyCondition.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters