2 |
efrain |
1 |
//
|
|
|
2 |
// DeleteAccountView.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 8/24/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import SwiftUI
|
|
|
9 |
import AudioToolbox
|
|
|
10 |
import Network
|
|
|
11 |
import Alamofire
|
|
|
12 |
import SwiftyJSON
|
|
|
13 |
import TTGSnackbar
|
|
|
14 |
|
|
|
15 |
struct DeleteAccountView: View {
|
|
|
16 |
@EnvironmentObject private var networkMonitor : NetworkMonitor
|
|
|
17 |
@EnvironmentObject private var appNavigation : AppNavigation
|
|
|
18 |
|
|
|
19 |
@State private var presentAlert : Bool = false
|
|
|
20 |
@State private var titleAlert : String = ""
|
|
|
21 |
@State private var messageAlert : String = ""
|
|
|
22 |
|
|
|
23 |
@State private var showProgressView : Bool = false
|
|
|
24 |
|
|
|
25 |
@State private var code: String = "" {
|
|
|
26 |
didSet {
|
|
|
27 |
if code.count > 8 {
|
|
|
28 |
code = String(code.prefix(8))
|
|
|
29 |
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { return }
|
|
|
30 |
}
|
|
|
31 |
}
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
|
21 |
efrain |
36 |
private var appData = Environment(\.appData).wrappedValue
|
2 |
efrain |
37 |
|
|
|
38 |
|
|
|
39 |
var body: some View {
|
|
|
40 |
ZStack {
|
|
|
41 |
|
|
|
42 |
Color("color_window_background")
|
|
|
43 |
.edgesIgnoringSafeArea(.all)
|
|
|
44 |
|
|
|
45 |
if self.showProgressView {
|
|
|
46 |
ProgressView()
|
|
|
47 |
.progressViewStyle(CircularProgressViewStyle(tint: Color("color_progress_view_foreground")))
|
|
|
48 |
.scaleEffect(3, anchor: .center).zIndex(100000)
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
VStack(spacing: 0) {
|
|
|
52 |
HStack {
|
|
|
53 |
Button(action: {
|
|
|
54 |
withAnimation {
|
|
|
55 |
appNavigation.subpageActive = .profile
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
}, label: {
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
Image(systemName: "chevron.backward")
|
|
|
62 |
.frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
|
|
|
63 |
.aspectRatio(contentMode: .fit)
|
|
|
64 |
.foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
|
|
|
65 |
})
|
|
|
66 |
.padding(.leading, 16)
|
|
|
67 |
|
|
|
68 |
Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : Config.LANG_DELETE_ACCOUNT_TITLE)
|
|
|
69 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
|
|
|
70 |
.foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
|
|
|
71 |
.padding(.leading, 4)
|
|
|
72 |
|
|
|
73 |
Spacer()
|
|
|
74 |
}
|
|
|
75 |
.edgesIgnoringSafeArea(.top)
|
|
|
76 |
.frame(height: 50)
|
|
|
77 |
.background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
HStack {
|
|
|
85 |
|
19 |
efrain |
86 |
if appData.userImage.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
2 |
efrain |
87 |
Image("logo")
|
|
|
88 |
.resizable()
|
|
|
89 |
.aspectRatio(contentMode: .fit)
|
|
|
90 |
.frame(height: Config.PROFILE_IMAGE_SIZE)
|
|
|
91 |
.clipShape(Circle())
|
|
|
92 |
.overlay(Circle().stroke(Color.white, lineWidth: 4))
|
|
|
93 |
.shadow(radius: 10)
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
} else {
|
|
|
98 |
CustomAsyncImageProfile(
|
19 |
efrain |
99 |
url: URL(string: appData.userImage.trimmingCharacters(in: .whitespacesAndNewlines))!,
|
2 |
efrain |
100 |
placeholder: { Text(Config.LANG_COMMON_LOADING).font(.footnote).foregroundColor(.black)},
|
|
|
101 |
image: {
|
|
|
102 |
Image(uiImage: $0)
|
|
|
103 |
.resizable()
|
|
|
104 |
|
|
|
105 |
}
|
|
|
106 |
)
|
|
|
107 |
}
|
|
|
108 |
}.padding(10)
|
|
|
109 |
|
|
|
110 |
VStack(spacing: 5) {
|
|
|
111 |
VStack(spacing: 5) {
|
|
|
112 |
Text("\(appData.userFirstname) \(appData.userLastname)" )
|
|
|
113 |
.bold()
|
|
|
114 |
.font(.title)
|
|
|
115 |
Text("\(appData.userEmail)")
|
|
|
116 |
.font(.body)
|
|
|
117 |
.foregroundColor(Color("color_textview_foreground"))
|
|
|
118 |
}.padding()
|
|
|
119 |
}.background(Color("color_card_view_background"))
|
|
|
120 |
|
4 |
efrain |
121 |
|
|
|
122 |
HStack {
|
2 |
efrain |
123 |
Text(Config.LANG_DELETE_ACCOUNT_MESSAGE)
|
4 |
efrain |
124 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: 12))
|
2 |
efrain |
125 |
.foregroundColor(Color("color_textview_foreground"))
|
4 |
efrain |
126 |
|
2 |
efrain |
127 |
|
4 |
efrain |
128 |
}.padding(.top, 20)
|
|
|
129 |
.padding(.horizontal, 16)
|
|
|
130 |
|
|
|
131 |
|
2 |
efrain |
132 |
HStack {
|
|
|
133 |
Text(Config.LANG_DELETE_ACCOUNT_FIELD_CODE_LABEL)
|
|
|
134 |
.font(Font.custom(Config .FONT_NAME_REGULAR, size: 11))
|
|
|
135 |
.foregroundColor(Color("color_textview_foreground"))
|
|
|
136 |
Spacer()
|
|
|
137 |
}
|
|
|
138 |
.padding(.leading, 16)
|
|
|
139 |
.padding(.top, 10)
|
|
|
140 |
|
|
|
141 |
Group {
|
|
|
142 |
HStack {
|
|
|
143 |
TextField("", text: self.$code)
|
|
|
144 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: 12))
|
|
|
145 |
.textFieldStyle(PlainTextFieldStyle())
|
|
|
146 |
.frame(height: 32)
|
|
|
147 |
.keyboardType(.emailAddress)
|
|
|
148 |
.autocapitalization(.none)
|
|
|
149 |
.padding(.leading, 4)
|
|
|
150 |
Spacer()
|
|
|
151 |
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
.foregroundColor(Color("color_textfield_foreground"))
|
|
|
155 |
.background(Color("color_textfield_background"))
|
|
|
156 |
.overlay(RoundedRectangle(cornerRadius: 5).stroke(
|
|
|
157 |
Color(self.code.count == 8 ? "color_textfield_border" : "color_textfield_border_error" )
|
|
|
158 |
))
|
|
|
159 |
.padding(.leading, 16)
|
|
|
160 |
.padding(.trailing, 16)
|
|
|
161 |
.padding(.top, self.code.count == 0 ? 10 : 2)
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
if self.code.count != 8 {
|
|
|
165 |
HStack {
|
|
|
166 |
Spacer()
|
|
|
167 |
|
4 |
efrain |
168 |
Text(Config.LANG_DELETE_ACCOUNTCODE_IS_WRONG_MESSAGE)
|
2 |
efrain |
169 |
.foregroundColor(.red)
|
|
|
170 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: 11))
|
|
|
171 |
|
|
|
172 |
}
|
|
|
173 |
.padding(.top, 5)
|
|
|
174 |
.padding(.trailing, 16)
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
Button(action: {
|
|
|
178 |
sendCode();
|
|
|
179 |
|
|
|
180 |
}, label: {
|
|
|
181 |
Text(Config.LANG_DELETE_ACCOUNT_BUTTON_SEND)
|
|
|
182 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
|
|
|
183 |
.frame(width: UIScreen.main.bounds.width - 32, height: 35)
|
|
|
184 |
.foregroundColor(Color("color_button_dark_foreground"))
|
|
|
185 |
.background(Color("color_button_dark_background"))
|
|
|
186 |
.border(Color( "color_button_dark_border"), width: Config.BUTTON_BORDER_SIZE)
|
|
|
187 |
.cornerRadius(Config.BUTTON_BORDER_RADIUS)
|
|
|
188 |
|
|
|
189 |
})
|
|
|
190 |
.padding(.top, 16)
|
|
|
191 |
.padding(.leading, 16)
|
|
|
192 |
.padding(.trailing, 16)
|
|
|
193 |
|
|
|
194 |
Button(action: {
|
|
|
195 |
getCode();
|
|
|
196 |
}, label: {
|
|
|
197 |
Text(Config.LANG_DELETE_ACCOUNT_BUTTON_REQUEST_CODE)
|
|
|
198 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
|
|
|
199 |
.frame(width: UIScreen.main.bounds.width - 32, height: 35)
|
|
|
200 |
.foregroundColor(Color("color_button_foreground"))
|
|
|
201 |
.background(Color("color_button_background"))
|
|
|
202 |
.border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE)
|
|
|
203 |
.cornerRadius(Config.BUTTON_BORDER_RADIUS)
|
|
|
204 |
})
|
|
|
205 |
.padding(.top, 16)
|
|
|
206 |
.padding(.leading, 16)
|
|
|
207 |
.padding(.trailing, 16)
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
} .alert(isPresented: $presentAlert) {
|
|
|
211 |
Alert(
|
|
|
212 |
title: Text(self.titleAlert),
|
|
|
213 |
message: Text(self.messageAlert),
|
|
|
214 |
dismissButton: .default(Text(Config.LANG_COMMON_OK))
|
|
|
215 |
)
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
func sendCode() -> Void
|
|
|
222 |
{
|
|
|
223 |
if code.isEmpty || code.count != 8 {
|
4 |
efrain |
224 |
|
2 |
efrain |
225 |
return;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
|
|
|
229 |
|
|
|
230 |
let parameters = [
|
4 |
efrain |
231 |
Constants.POST_DELETE_ACCOUNT_CODE: code
|
2 |
efrain |
232 |
]
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
let headerSecurity : HeaderSecurity = HeaderSecurity()
|
|
|
236 |
let headers: HTTPHeaders = [
|
|
|
237 |
.init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
|
|
|
238 |
.init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
|
|
|
239 |
.init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
|
|
|
240 |
.init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
|
|
|
241 |
.accept(Constants.HTTP_HEADER_ACCEPT)
|
|
|
242 |
]
|
|
|
243 |
|
|
|
244 |
self.showProgressView = true
|
|
|
245 |
AF.request(Config.URL_DELETE_ACCOUNT, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{(response) in
|
|
|
246 |
|
|
|
247 |
self.showProgressView = false
|
|
|
248 |
switch response.result {
|
|
|
249 |
case .success:
|
|
|
250 |
let json = try? JSON(data: response.data!)
|
|
|
251 |
if json?["success"] ?? "" != false {
|
|
|
252 |
|
|
|
253 |
DispatchQueue.main.async() {
|
|
|
254 |
withAnimation {
|
|
|
255 |
appData.userUuid = ""
|
|
|
256 |
appData.userFirstname = ""
|
|
|
257 |
appData.userLastname = ""
|
|
|
258 |
appData.userEmail = ""
|
|
|
259 |
appData.userImage = ""
|
|
|
260 |
appData.refreshContentActionRequired = false
|
|
|
261 |
appData.refreshContentMessage = ""
|
|
|
262 |
appData.refreshContentMessageShowPending = false
|
|
|
263 |
appData.signoutActionRequired = false
|
|
|
264 |
appData.save()
|
|
|
265 |
|
|
|
266 |
appNavigation.pageActive = .goodbyedeleteaccount
|
|
|
267 |
}
|
|
|
268 |
}
|
|
|
269 |
} else {
|
4 |
efrain |
270 |
self.code = ""
|
|
|
271 |
let message = json?["data"]["message"].string ?? ""
|
2 |
efrain |
272 |
if !message.isEmpty {
|
|
|
273 |
self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
|
|
|
274 |
self.messageAlert = message
|
|
|
275 |
self.presentAlert = true
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
return
|
|
|
280 |
|
|
|
281 |
case .failure :
|
|
|
282 |
self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
|
|
|
283 |
self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
|
|
|
284 |
self.presentAlert = true
|
|
|
285 |
|
|
|
286 |
return
|
|
|
287 |
}
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
func getCode()
|
|
|
292 |
{
|
|
|
293 |
|
|
|
294 |
let headerSecurity : HeaderSecurity = HeaderSecurity()
|
|
|
295 |
let headers: HTTPHeaders = [
|
|
|
296 |
.init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
|
|
|
297 |
.init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
|
|
|
298 |
.init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
|
|
|
299 |
.init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
|
|
|
300 |
.accept(Constants.HTTP_HEADER_ACCEPT)
|
|
|
301 |
]
|
|
|
302 |
|
|
|
303 |
self.showProgressView = true
|
|
|
304 |
AF.request(Config.URL_DELETE_ACCOUNT, method: .get, headers: headers).responseJSON{(response) in
|
|
|
305 |
self.showProgressView = false
|
|
|
306 |
switch response.result {
|
|
|
307 |
case .success:
|
|
|
308 |
let json = try? JSON(data: response.data!)
|
|
|
309 |
|
4 |
efrain |
310 |
let message = json?["data"]["message"].string ?? ""
|
2 |
efrain |
311 |
if !message.isEmpty {
|
4 |
efrain |
312 |
|
|
|
313 |
let snackbar = TTGSnackbar(message: message, duration: .long);
|
|
|
314 |
snackbar.show()
|
2 |
efrain |
315 |
}
|
|
|
316 |
|
|
|
317 |
return
|
|
|
318 |
|
|
|
319 |
case .failure :
|
|
|
320 |
self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
|
|
|
321 |
self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
|
|
|
322 |
self.presentAlert = true
|
|
|
323 |
|
|
|
324 |
return
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
}
|
|
|
328 |
}
|
|
|
329 |
|
|
|
330 |
struct DeleteAccountView_Previews: PreviewProvider {
|
|
|
331 |
static var previews: some View {
|
|
|
332 |
DeleteAccountView()
|
|
|
333 |
}
|
|
|
334 |
}
|