-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathLockdownUser.swift
45 lines (36 loc) · 1.14 KB
/
LockdownUser.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// LockdownUser.swift
// Lockdown
//
// Created by Aliaksandr Dvoineu on 4.05.23.
// Copyright © 2023 Confirmed Inc. All rights reserved.
//
import Foundation
final class LockdownUser {
private(set) var currentSubscription: Subscription?
@CodableUserDefaults(key: "cahedUsedSubscription")
private var cachedUserSubscription: Subscription?
func updateSubscription(to newSubscription: Subscription?) {
currentSubscription = newSubscription
updateCachedSubscription(to: newSubscription)
}
private func updateCachedSubscription(to newSubscription: Subscription?) {
guard let newSubscription else {
return
}
cachedUserSubscription = newSubscription
}
func cachedSubscription() -> Subscription? {
guard let subscription = cachedUserSubscription else {
return nil
}
guard subscription.expirationDate > Date() else {
cachedUserSubscription = nil
return nil
}
return subscription
}
func resetCache() {
cachedUserSubscription = nil
}
}