-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserManager.swift
69 lines (57 loc) · 1.7 KB
/
UserManager.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import Foundation
import Batch.User
private let onboardingAttemptedKey = "onboardingAttempted"
private let firstNameKey = "accountFirstName"
private let usernameKey = "accountUsername"
class UserManager {
var isLoggedIn: Bool {
get {
return username != nil
}
}
var onboardingAttempted: Bool {
get {
return UserDefaults.standard.bool(forKey: onboardingAttemptedKey)
}
set {
UserDefaults.standard.setValue(newValue, forKey: onboardingAttemptedKey)
}
}
var firstName: String? {
get {
return UserDefaults.standard.string(forKey: firstNameKey)
}
set {
UserDefaults.standard.setValue(newValue, forKey: firstNameKey)
}
}
var username: String? {
get {
return UserDefaults.standard.string(forKey: usernameKey)
}
set {
UserDefaults.standard.setValue(newValue, forKey: usernameKey)
}
}
func login(firstName: String, username: String) {
self.firstName = firstName
self.username = username
syncBatchUserInfo()
}
func logout() {
firstName = nil
username = nil
}
func syncBatchUserInfo() {
let username = self.username
BatchProfile.identify(username)
BatchProfile.editor { editor in
if let firstName = self.firstName {
try! editor.set(attribute: firstName, forKey: "firstname")
} else {
try! editor.removeAttribute(key: "firstname")
}
}
SubscriptionManager().syncDataWithBatch()
}
}