summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/UserDefaultsInteractor.swift
blob: 786611e44888491b4906ca3e8936db307ee58614 (plain)
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
46
47
48
49
50
51
52
53
54
//
//  UserDefaultsInteractor.swift
//  MullvadVPN
//
//  Created by pronebird on 15/05/2019.
//  Copyright © 2019 Amagicom AB. All rights reserved.
//

import Foundation

/// The application group identifier used for sharing application preferences between processes
private let kApplicationGroupIdentifier = "group.net.mullvad.MullvadVPN"

/// The UserDefaults keys used to store the application preferences
private enum UserDefaultsKeys: String {
    case accountToken, accountExpiry
}

/// The interactor class that provides a convenient interface for accessing the Mullvad VPN
/// preferences stored in the UserDefaults store.
class UserDefaultsInteractor {
    let userDefaults: UserDefaults

    /// The shared instance of UserDefaultsInteractor initialized with the application group
    /// preferences
    static let sharedApplicationGroupInteractor: UserDefaultsInteractor = {
        let userDefaults = UserDefaults(suiteName: kApplicationGroupIdentifier)!

        return UserDefaultsInteractor(userDefaults: userDefaults)
    }()

    init(userDefaults: UserDefaults) {
        self.userDefaults = userDefaults
    }

    var accountToken: String? {
        get {
            return userDefaults.string(forKey: UserDefaultsKeys.accountToken.rawValue)
        }
        set {
            userDefaults.set(newValue, forKey: UserDefaultsKeys.accountToken.rawValue)
        }
    }

    var accountExpiry: Date? {
        get {
            return userDefaults.object(forKey: UserDefaultsKeys.accountExpiry.rawValue) as? Date
        }
        set {
            userDefaults.set(newValue, forKey: UserDefaultsKeys.accountExpiry.rawValue)
        }
    }

}