/
ContentViewModel.swift
225 lines (212 loc) · 6.88 KB
/
ContentViewModel.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// ContentViewModel.swift
// ParseMigrateKeychain
//
// Created by Corey Baker on 9/11/22.
//
import Foundation
import ParseSwift
import Parse
class ContentViewModel: ObservableObject {
@Published var objCUserObjectId = ""
@Published var objCInstallationId = ""
@Published var objCSDKLoggedIn = false {
willSet {
guard newValue else {
return
}
errorMessage = ""
}
}
@Published var swiftUserObjectId = ""
@Published var swiftInstallationId = ""
@Published var swiftSDKLoggedIn = false
@Published var errorMessage = "" {
willSet {
DispatchQueue.main.async {
self.objectWillChange.send()
}
}
}
var objCUser: PFUser? {
willSet {
DispatchQueue.main.async {
self.objCUserObjectId = newValue?.objectId ?? ""
guard newValue != nil else {
self.objCSDKLoggedIn = false
return
}
self.objCSDKLoggedIn = true
}
}
}
var objCInstallation: PFInstallation? {
willSet {
DispatchQueue.main.async {
self.objCInstallationId = newValue?.installationId ?? ""
}
}
}
var swiftUser: User? {
willSet {
DispatchQueue.main.async {
self.swiftUserObjectId = newValue?.objectId ?? ""
guard newValue != nil else {
self.swiftSDKLoggedIn = false
return
}
self.swiftSDKLoggedIn = true
}
}
}
var swiftInstallation: Installation? {
willSet {
DispatchQueue.main.async {
self.swiftInstallationId = newValue?.installationId ?? ""
}
}
}
init() {
setupSDKs()
updateAllProperties()
}
// MARK: Helper Methods
func setupSDKs() {
let applicationId = "applicationId"
let clientKey = "clientKey"
let serverURLString = "http://localhost:1337/1"
// Swift SDK setup
let swiftConfiguration = ParseConfiguration(applicationId: applicationId,
clientKey: clientKey,
serverURL: URL(string: serverURLString)!)
ParseSwift.initialize(configuration: swiftConfiguration)
// Objective-C SDK setup
let objCConfiguration = ParseClientConfiguration { config in
config.applicationId = applicationId
config.clientKey = clientKey
config.server = serverURLString
}
Parse.initialize(with: objCConfiguration)
}
func updateAllProperties() {
if let currentUser = PFUser.current() {
objCUserObjectId = currentUser.objectId ?? ""
objCSDKLoggedIn = true
} else {
objCSDKLoggedIn = false
}
if let currentInstallation = PFInstallation.current() {
objCInstallationId = currentInstallation.installationId
}
if let currentUser = User.current {
swiftUserObjectId = currentUser.objectId ?? ""
swiftSDKLoggedIn = true
} else {
swiftSDKLoggedIn = false
}
if let currentInstallation = Installation.current {
swiftInstallationId = currentInstallation.installationId ?? ""
}
}
@MainActor
func loginToSwiftSDK() async {
do {
self.swiftUser = try await User.loginUsingObjCKeychain()
} catch {
guard let parseError = error as? ParseError else {
self.errorMessage = error.localizedDescription
return
}
self.errorMessage = parseError.message
}
}
// MARK: Intents
@MainActor
func signUpUsingObjcSDK(_ username: String, password: String) {
guard PFUser.current() == nil else {
self.errorMessage = "User is already logged in"
return
}
let newUser = PFUser()
newUser.username = username
newUser.password = password
newUser.signUpInBackground { (user, error) in
guard error == nil else {
self.errorMessage = error?.localizedDescription ?? "Could not sign up"
return
}
self.objCUser = PFUser.current()
Task {
await self.loginToSwiftSDK()
}
}
}
@MainActor
func loginUsingObjcSDK(_ username: String, password: String) {
guard PFUser.current() == nil else {
self.errorMessage = "User is already logged in"
return
}
PFUser.logInWithUsername(inBackground: username, password: password) { (user, error) in
guard error == nil else {
self.errorMessage = error?.localizedDescription ?? "Could not log in"
return
}
self.objCUser = user
Task {
await self.loginToSwiftSDK()
}
}
}
@MainActor
func saveInstallationUsingObjcSDK() {
let dummyChannels = ["global"]
let installation = PFInstallation.current()
installation?.channels = dummyChannels
installation?.saveInBackground { (success, error) in
guard success,
error == nil,
let currentInstallation = PFInstallation.current(),
let objectId = currentInstallation.objectId else {
self.errorMessage = error?.localizedDescription ?? "Could not save installation"
return
}
self.objCInstallation = currentInstallation
Task {
do {
self.swiftInstallation = try await Installation.become(objectId)
} catch {
guard let parseError = error as? ParseError else {
self.errorMessage = error.localizedDescription
return
}
self.errorMessage = parseError.message
}
}
}
}
@MainActor
func logoutObjCUser() {
PFUser.logOutInBackground { error in
self.updateAllProperties()
guard error == nil else {
self.errorMessage = error?.localizedDescription ?? "Could not log out"
return
}
}
}
@MainActor
func logoutSwiftUser() async {
do {
try await User.logout()
self.updateAllProperties()
} catch {
self.updateAllProperties()
guard let parseError = error as? ParseError else {
self.errorMessage = error.localizedDescription
return
}
self.errorMessage = parseError.message
}
}
}