Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Rev 4 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
36
    private let appData = AppData.sharedInstance
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
 
86
                        if appData.userImage.isEmpty {
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(
99
                                url: URL(string: appData.userImage)!,
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
 
121
                Group {
122
                    VStack(spacing: 0)
123
                    {
124
                        Text(Config.LANG_DELETE_ACCOUNT_MESSAGE)
125
                        .font(Font.custom(Config.FONT_NAME_REGULAR, size: 16))
126
                        .foregroundColor(Color("color_textview_foreground"))
127
                        .padding(.horizontal, 16)
128
                        .padding(.top, 20)
129
                    }
130
                }.padding(.top, 30)
131
 
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
 
168
                        Text(Config.LANG_ERROR_ACCOUNT_DELETE_CODE_IS_EMPTY_TITLE)
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 {
224
            self.titleAlert = Config.LANG_ERROR_ACCOUNT_DELETE_CODE_IS_EMPTY_TITLE
225
            self.messageAlert = Config.LANG_ERROR_ACCOUNT_DELETE_CODE_IS_EMPTY_MESSAGE
226
            self.presentAlert = true
227
            return;
228
        }
229
 
230
 
231
 
232
        let parameters = [
233
            Constants.POST_COMMENT_FIELD_ADDED_ON: code
234
        ]
235
 
236
 
237
        let headerSecurity : HeaderSecurity = HeaderSecurity()
238
        let headers: HTTPHeaders = [
239
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
240
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
241
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
242
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
243
            .accept(Constants.HTTP_HEADER_ACCEPT)
244
        ]
245
 
246
        self.showProgressView = true
247
        AF.request(Config.URL_DELETE_ACCOUNT, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{(response) in
248
 
249
            self.showProgressView = false
250
            switch response.result {
251
                case .success:
252
                    let json = try? JSON(data: response.data!)
253
                    if json?["success"] ?? "" != false {
254
 
255
                        DispatchQueue.main.async() {
256
                                withAnimation {
257
                                    appData.userUuid = ""
258
                                    appData.userFirstname = ""
259
                                    appData.userLastname = ""
260
                                    appData.userEmail = ""
261
                                    appData.userImage = ""
262
                                    appData.refreshContentActionRequired = false
263
                                    appData.refreshContentMessage = ""
264
                                    appData.refreshContentMessageShowPending = false
265
                                    appData.signoutActionRequired = false
266
                                    appData.save()
267
 
268
                                    appNavigation.pageActive = .goodbyedeleteaccount
269
                                }
270
                            }
271
                    } else {
272
 
273
                        let message = json?["data"].string ?? ""
274
                        if !message.isEmpty {
275
                            self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
276
                            self.messageAlert = message
277
                            self.presentAlert = true
278
                        }
279
                    }
280
 
281
                    return
282
 
283
                case .failure :
284
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
285
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
286
                    self.presentAlert = true
287
 
288
                    return
289
            }
290
        }
291
    }
292
 
293
    func getCode()
294
    {
295
 
296
        let headerSecurity : HeaderSecurity = HeaderSecurity()
297
        let headers: HTTPHeaders = [
298
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
299
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
300
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
301
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
302
            .accept(Constants.HTTP_HEADER_ACCEPT)
303
        ]
304
 
305
        self.showProgressView = true
306
        AF.request(Config.URL_DELETE_ACCOUNT, method: .get, headers: headers).responseJSON{(response) in
307
            self.showProgressView = false
308
            switch response.result {
309
                case .success:
310
                    let json = try? JSON(data: response.data!)
311
 
312
                    let message = json?["data"].string ?? ""
313
                    if !message.isEmpty {
314
                        self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
315
                        self.messageAlert = message
316
                        self.presentAlert = true
317
                    }
318
 
319
                    return
320
 
321
                case .failure :
322
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
323
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
324
                    self.presentAlert = true
325
 
326
                    return
327
            }
328
        }
329
    }
330
}
331
 
332
struct DeleteAccountView_Previews: PreviewProvider {
333
    static var previews: some View {
334
        DeleteAccountView()
335
    }
336
}