-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.swift
255 lines (203 loc) · 8.96 KB
/
User.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// User.swift
// Prayer Pulse
//
// Created by mac on 16/08/18.
// Copyright © 2018 mac. All rights reserved.
//
import Foundation
import SwiftyJSON
import SwiftyUserDefaults
let kUserId = "userId"
let kFirstName = "firstName"
let kLastName = "lastName"
let kEmail = "email"
let kPassword = "password"
//let kChurch = "church"
let kProfileImage = "profileImage"
let kDeviceToken = "deviceToken"
let kDeviceType = "deviceType"
let kQuickBloxId = "quickBloxId"
class User {
let id: Int
let firstName: String
let lastName: String
let email: String
let profileImage: String
let latitude: Double
let longitude: Double
// init?(id: Int, firstName: String, lastName: String, email: String, profileImage: String) {
// self.id = id
// self.firstName = firstName
// self.lastName = lastName
// self.email = email
// self.profileImage = profileImage
// }
init?(dictionary: [String:Any]) {
guard let id = dictionary[kUserId] as? Int else {
return nil
}
// Defaults[.userDictionary] = dictionary
//"userDictionary"
// UserDefaults.setUserDictionary(dictionary)
self.id = id
self.firstName = dictionary[kFirstName] as? String ?? ""
self.lastName = dictionary[kLastName] as? String ?? ""
self.email = dictionary[kEmail] as? String ?? ""
self.profileImage = dictionary[kProfileImage] as? String ?? ""
self.latitude = dictionary[kLatitude] as? Double ?? 0.00
self.longitude = dictionary[kLongitude] as? Double ?? 0.00
}
// MARK: Check if user exist
class func checkIfUserExist(withEmail email: String, success withResponse: @escaping (Bool) -> (), failure: @escaping (_ error: String) -> Void) {
let param: [String: Any] = [
kEmail: email
]
ApiService.makeRequest(with: baseURL.checkExistingUser, method: .post, parameter: param, success: { (response) in
let dict = response as? [String:Any] ?? [:]
let isSuccess = dict[kResult] as? Bool ?? false
// if success true -> Email does not exist -> return false -> user can register successfully
// if success false -> Email already registered -> return true -> registration failed
if isSuccess {
withResponse(false)
}else {
withResponse(true)
}
}, failure: { (error) in
failure(error)
}) { (connectionError) in
failure(connectionError)
}
}
// MARK: register user
class func registerUserWith(FirstName fn: String, LastName ln: String, Email email: String, Password pass: String, success withResponse: @escaping (_ boolResult: Bool,_ message: String) -> (), failure: @escaping (_ error: String) -> Void) {
let param: [String: Any] = [
kFirstName: fn,
kLastName: ln,
kEmail: email,
kPassword: pass,
kProfileImage: "",
kDeviceToken: "",
kDeviceType: "1"
]
ApiService.makeRequest(with: baseURL.registrationURL, method: .post, parameter: param, success: { (response) in
print("registraion result \(response)")
let dict = response as? [String:Any] ?? [:]
let isSuccess = dict[kResult] as? Bool ?? false
let message = dict[kMessage] as? String ?? ""
if isSuccess {
let data = dict[kData] as? [String:Any] ?? [:]
// print("print registraion Data \(data)")
let array = User(dictionary: data)
// set userdefaults and assign to class
UserDefaults.setUserDictionary(data)
UserDefaults.setUserLoggedIn()
if let asdf = UserDefaults.getUserDictionary(), let user = User(dictionary: asdf) {
// print("asdfasdf: \(asdf)")
// print("user \(user.id)")
PrayerPulse.sharedInstance.currentUser = user
}
withResponse(isSuccess, message)
}else{
withResponse(isSuccess, message)
}
}, failure: { (error) in
failure(error)
}) { (connectionError) in
failure(connectionError)
}
}
// MARK: Sign In Func
class func logInUserWith(Email email: String, Password pass: String, success withResponse: @escaping (User?) -> (), failure: @escaping (_ error: String) -> Void) {
let param: [String: Any] = [
kEmail: email,
kPassword: pass,
kDeviceToken: "21231",
kDeviceType: 1
]
ApiService.makeRequest(with: baseURL.logIn, method: .post, parameter: param, success: { (response) in
let dict = response as? [String:Any] ?? [:]
let isSuccess = dict[kResult] as? Bool ?? false
let message = dict[kMessage] as? String ?? ""
if isSuccess {
let data = dict[kData] as? [String:Any] ?? [:]
print("print registraion Data \(data)")
let array = User(dictionary: data)
// set userdefaults and assign to class
UserDefaults.setUserDictionary(data)
if let asdf = UserDefaults.getUserDictionary(), let user = User(dictionary: asdf) {
print("asdfasdf: \(asdf)")
print("user \(user.id)")
PrayerPulse.sharedInstance.currentUser = user
}
UserDefaults.setUserLoggedIn()
withResponse(array)
}else{
failure(message)
}
}, failure: { (error) in
failure(error)
}) { (connectionError) in
failure(connectionError)
}
}
// MARK: Update Profile Func
class func updateProfile(FirstName fname: String, LastName lname: String, ProfileImage imgProfile: UIImage?, success withResponse: @escaping (User?, _ message: String) -> (), failure: @escaping (_ error: String) -> Void) {
let param: [String: Any] = [
kUserId: String(PrayerPulse.sharedInstance.currentUser?.id ?? 0),
kFirstName: fname,
kLastName: lname
]
ApiService.callAPIMultipartFormData(baseURL.updateProfile, imgProfile, withRequest: param, withSuccess: { (testt) in
let dict = testt as? [String:Any] ?? [:]
let isSuccess = dict[kResult] as? Bool ?? false
let message = dict[kMessage] as? String ?? ""
if isSuccess {
let data = dict[kData] as? [String:Any] ?? [:]
print("print update user Data \(data)")
let array = User(dictionary: data)
// set userdefaults and assign to class
UserDefaults.setUserDictionary(data)
if let asdf = UserDefaults.getUserDictionary(), let user = User(dictionary: asdf) {
PrayerPulse.sharedInstance.currentUser = user
}
UserDefaults.setUserLoggedIn()
withResponse(array, message)
}else{
failure(message)
}
}, failure: { (failureString) in
failure(failureString)
}) { (connError) in
failure(connError)
}
}
// MARK: Sign OUT Func
class func logOutUserWith(UserID uId: Int, success withResponse: @escaping (_ boolResult: Bool, _ msg: String) -> (), failure: @escaping (_ error: String) -> Void) {
// let param: [String: Any] = [
// kEmail: email,
// kPassword: pass,
// kDeviceToken: "21231",
// kDeviceType: 1
// ]
ApiService.makeRequest(with: baseURL.logOut, method: .post, parameter: [:], success: { (response) in
let dict = response as? [String:Any] ?? [:]
let isSuccess = dict[kResult] as? Bool ?? false
let message = dict[kMessage] as? String ?? ""
if isSuccess {
let data = dict[kData] as? [String:Any] ?? [:]
let message = dict[kMessage] as? String ?? ""
// let result = dict[kResult] as? Bool ?? false
print("logout response Data \(data)")
UserDefaults.setUserLoggedOut()
withResponse(isSuccess, message)
}else{
failure(message)
}
}, failure: { (error) in
failure(error)
}) { (connectionError) in
failure(connectionError)
}
}
}