Skip to content

Commit c23ab0b

Browse files
authored
Merge pull request #575 from Iterable/jay/MOB-4781-userdefaults-migration
[MOB-4781] userdefaults migration refactor
2 parents 8e0a251 + 0678d8e commit c23ab0b

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

swift-sdk/Internal/IterableUserDefaults.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class IterableUserDefaults {
1010
self.userDefaults = userDefaults
1111
}
1212

13+
// migrated to IterableKeychain
1314
var userId: String? {
1415
get {
1516
string(withKey: .userId)
@@ -18,6 +19,7 @@ class IterableUserDefaults {
1819
}
1920
}
2021

22+
// migrated to IterableKeychain
2123
var email: String? {
2224
get {
2325
string(withKey: .email)
@@ -26,6 +28,7 @@ class IterableUserDefaults {
2628
}
2729
}
2830

31+
// migrated to IterableKeychain
2932
var authToken: String? {
3033
get {
3134
string(withKey: .authToken)
@@ -34,6 +37,7 @@ class IterableUserDefaults {
3437
}
3538
}
3639

40+
// deprecated, not in use anymore
3741
var ddlChecked: Bool {
3842
get {
3943
bool(withKey: .ddlChecked)
@@ -74,15 +78,19 @@ class IterableUserDefaults {
7478
try? save(codable: attributionInfo, withKey: .attributionInfo, andExpiration: expiration)
7579
}
7680

81+
// migrated to IterableKeychain
7782
func getPayload(currentDate: Date) -> [AnyHashable: Any]? {
7883
(try? dict(withKey: .payload, currentDate: currentDate)) ?? nil
7984
}
8085

86+
// migrated to IterableKeychain
8187
func save(payload: [AnyHashable: Any]?, withExpiration expiration: Date?) {
8288
try? save(dict: payload, withKey: .payload, andExpiration: expiration)
8389
}
8490

85-
func getLastPushPayloadAndExpirationPair() -> (payload: [AnyHashable: Any]?, expiration: Date?)? {
91+
// MARK: data migration functions
92+
93+
func getLastPushPayloadExpirationPairForMigration() -> (payload: [AnyHashable: Any]?, expiration: Date?)? {
8694
guard let encodedEnvelope = userDefaults.value(forKey: UserDefaultsKey.payload.value) as? Data else {
8795
return nil
8896
}
@@ -97,7 +105,9 @@ class IterableUserDefaults {
97105
}
98106
}
99107

100-
// create migration function for email, userId, authToken here
108+
func getAuthDataForMigration() -> (email: String?, userId: String?, authToken: String?) {
109+
return (email: email, userId: userId, authToken: authToken)
110+
}
101111

102112
// MARK: Private implementation
103113

swift-sdk/Internal/LocalStorage.swift

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,35 +99,45 @@ struct LocalStorage: LocalStorageProtocol {
9999
private let keychain: IterableKeychain
100100

101101
private func moveAuthDataFromUserDefaultsToKeychain() {
102-
// in the future, use the migration function from IterableUserDefaults like with the last push payload
102+
let (userDefaultEmail, userDefaultUserId, userDefaultAuthToken) = iterableUserDefaults.getAuthDataForMigration()
103103

104-
if let userDefaultAuthToken = iterableUserDefaults.authToken, keychain.authToken == nil {
105-
keychain.authToken = userDefaultAuthToken
106-
iterableUserDefaults.authToken = nil
104+
if let userDefaultEmail = userDefaultEmail {
105+
if keychain.email == nil {
106+
keychain.email = userDefaultEmail
107+
}
107108

108-
ITBInfo("UPDATED: moved authToken from UserDefaults to IterableKeychain")
109-
}
110-
111-
if let userDefaultEmail = iterableUserDefaults.email, keychain.email == nil {
112-
keychain.email = userDefaultEmail
113109
iterableUserDefaults.email = nil
114110

115-
ITBInfo("UPDATED: moved email from UserDefaults to IterableKeychain")
111+
ITBInfo("UPDATED: migrated email from UserDefaults to IterableKeychain")
116112
}
117113

118-
if let userDefaultUserId = iterableUserDefaults.userId, keychain.userId == nil {
119-
keychain.userId = userDefaultUserId
114+
if let userDefaultUserId = userDefaultUserId {
115+
if keychain.userId == nil {
116+
keychain.userId = userDefaultUserId
117+
}
118+
120119
iterableUserDefaults.userId = nil
121120

122-
ITBInfo("UPDATED: moved userId from UserDefaults to IterableKeychain")
121+
ITBInfo("UPDATED: migrated userId from UserDefaults to IterableKeychain")
122+
}
123+
124+
if let userDefaultAuthToken = userDefaultAuthToken {
125+
if keychain.authToken == nil {
126+
keychain.authToken = userDefaultAuthToken
127+
}
128+
129+
iterableUserDefaults.authToken = nil
130+
131+
ITBInfo("UPDATED: migrated authToken from UserDefaults to IterableKeychain")
123132
}
124133
}
125134

126135
private func moveLastPushPayloadFromUserDefaultsToKeychain() {
127-
if let (userDefaultLastPushPayload, expiration) = iterableUserDefaults.getLastPushPayloadAndExpirationPair() {
136+
if let (userDefaultLastPushPayload, expiration) = iterableUserDefaults.getLastPushPayloadExpirationPairForMigration() {
128137
keychain.setLastPushPayload(userDefaultLastPushPayload, withExpiration: expiration)
138+
iterableUserDefaults.save(payload: nil, withExpiration: nil)
129139

130-
ITBInfo("UPDATED: moved lastPushPayload from UserDefaults to IterableKeychain")
140+
ITBInfo("UPDATED: migrated lastPushPayload from UserDefaults to IterableKeychain")
131141
}
132142
}
133143
}

0 commit comments

Comments
 (0)