Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect for timezone changes and update the user #1542

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ class OSPropertiesModel: OSModel {
}
}

var timezoneId = TimeZone.current.identifier
var timezoneId: String? = TimeZone.current.identifier {
didSet {
guard timezoneId != oldValue else {
return
}
self.set(property: "timezone_id", newValue: timezoneId)
}
}

var tags: [String: String] = [:]
private let tagsLock = NSRecursiveLock()
Expand All @@ -90,13 +97,15 @@ class OSPropertiesModel: OSModel {
super.encode(with: coder)
coder.encode(language, forKey: "language")
coder.encode(tags, forKey: "tags")
coder.encode(timezoneId, forKey: "timezoneId")
// ... and more
}
}

required init?(coder: NSCoder) {
super.init(coder: coder)
language = coder.decodeObject(forKey: "language") as? String
timezoneId = coder.decodeObject(forKey: "timezoneId") as? String
guard let tags = coder.decodeObject(forKey: "tags") as? [String: String] else {
// log error
return
Expand All @@ -106,6 +115,10 @@ class OSPropertiesModel: OSModel {
// ... and more
}

func update() {
timezoneId = TimeZone.current.identifier
}

/**
Called to clear the model's data in preparation for hydration via a fetch user call.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protocol OSUserInternal {
var pushSubscriptionModel: OSSubscriptionModel { get }
var identityModel: OSIdentityModel { get }
var propertiesModel: OSPropertiesModel { get }
func update()
// Aliases
func addAliases(_ aliases: [String: String])
func removeAliases(_ labels: [String])
Expand Down Expand Up @@ -72,6 +73,11 @@ class OSUserInternalImpl: NSObject, OSUserInternal {
self.pushSubscriptionModel = pushSubscriptionModel
}

func update() {
self.pushSubscriptionModel.update()
self.propertiesModel.update()
}

// MARK: - Aliases

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
subscriptionModelStoreListener.start()
pushSubscriptionModelStoreListener.start()
if hasCachedUser {
_user?.pushSubscriptionModel.update()
_user?.update()
}
}

Expand Down Expand Up @@ -294,6 +294,18 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {

// 3. Make the request
userExecutor!.fetchIdentityBySubscription(newUser)

// 4. Send any updates to sync the local state
updateLegacyPlayer(newUser)
}

/**
Migrating a legacy player does not result in a Create User call, but certain local data should be sent manually.
*/
private func updateLegacyPlayer(_ user: OSUserInternal) {
if let timezoneId = user.propertiesModel.timezoneId {
updatePropertiesDeltas(property: .timezone_id, value: timezoneId)
}
}

private func createNewUser(externalId: String?, token: String?) -> OSUserInternal {
Expand Down Expand Up @@ -567,6 +579,7 @@ extension OneSignalUserManagerImpl {
/// It enqueues an OSDelta to the Operation Repo.
///
/// - Parameter property:Expected inputs are `.session_time"`, `.session_count"`, and `.purchases"`.
/// May be `.timezone_id` or others if the SDK is sending an update for a legacy player.
func updatePropertiesDeltas(property: OSPropertiesSupportedProperty, value: Any, flush: Bool = false) {
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "updatePropertiesDeltas") else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum OSPropertiesSupportedProperty: String {
case language
case location
case tags
case timezone_id
// Created manually by User Manager, not through Models
case session_count
case session_time
Expand Down
Loading