Skip to content

Commit 319c2c8

Browse files
committed
assume func. of expiration value and add notes
1 parent 703484c commit 319c2c8

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

swift-sdk/Constants.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ enum Const {
6464
static let userId = "itbl_userid"
6565
static let authToken = "itbl_auth_token"
6666
static let lastPushPayload = "itbl_last_push_payload"
67+
static let lastPushPayloadExpiration = "itbl_last_push_payload_expiration"
6768
}
6869
}
6970

swift-sdk/Internal/IterableKeychain.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class IterableKeychain {
2525

2626
wrapper.set(data, forKey: Const.Keychain.Key.email)
2727
}
28-
2928
}
3029

3130
var userId: String? {
@@ -52,6 +51,7 @@ class IterableKeychain {
5251

5352
return data.flatMap { String(data: $0, encoding: .utf8) }
5453
}
54+
5555
set {
5656
guard let token = newValue,
5757
let data = token.data(using: .utf8) else {
@@ -64,15 +64,18 @@ class IterableKeychain {
6464
}
6565

6666
func getLastPushPayload(currentDate: Date) -> [AnyHashable: Any]? {
67-
guard let data = wrapper.data(forKey: Const.Keychain.Key.lastPushPayload) else {
67+
if isLastPushPayloadExpired(currentDate: currentDate) {
68+
wrapper.removeValue(forKey: Const.Keychain.Key.lastPushPayload)
69+
wrapper.removeValue(forKey: Const.Keychain.Key.lastPushPayloadExpiration)
70+
6871
return nil
6972
}
7073

71-
let lastPushPayload = (try? JSONSerialization.jsonObject(with: data)) as? [AnyHashable: Any]
72-
73-
// check for expiration here
74+
if let data = wrapper.data(forKey: Const.Keychain.Key.lastPushPayload) {
75+
return (try? JSONSerialization.jsonObject(with: data)) as? [AnyHashable: Any]
76+
}
7477

75-
return lastPushPayload
78+
return nil
7679
}
7780

7881
func setLastPushPayload(_ payload: [AnyHashable: Any]?, withExpiration expiration: Date?) {
@@ -94,4 +97,14 @@ class IterableKeychain {
9497
// MARK: - PRIVATE/INTERNAL
9598

9699
private let wrapper: KeychainWrapper
100+
101+
private func isLastPushPayloadExpired(currentDate: Date) -> Bool {
102+
// get expiration here
103+
104+
guard let expiration = wrapper.data(forKey: Const.Keychain.Key.lastPushPayloadExpiration) as? Date else {
105+
return false
106+
}
107+
108+
return !(expiration.timeIntervalSinceReferenceDate > currentDate.timeIntervalSinceReferenceDate)
109+
}
97110
}

0 commit comments

Comments
 (0)