Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 49 | Rev 51 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
45 efrain 1
//
2
//  DeleteAccountView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 8/24/22.
6
//
7
 
8
import SwiftUI
48 efrain 9
import AudioToolbox
45 efrain 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 {
48 efrain 28
                code = String(code.prefix(8))
45 efrain 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
 
49 efrain 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
 
120
 
121
                }.background(Color("color_card_view_background"))
122
 
45 efrain 123
                Text(Config.LANG_DELETE_ACCOUNT_MESSAGE)
50 efrain 124
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: 16))
125
                    .foregroundColor(Color("color_textview_foreground"))
126
                    .padding(.horizontal, 16)
127
                    .padding(.top, 20)
45 efrain 128
 
49 efrain 129
                HStack {
130
                    Text(Config.LANG_DELETE_ACCOUNT_FIELD_CODE_LABEL)
131
                    .font(Font.custom(Config  .FONT_NAME_REGULAR, size: 11))
132
                    .foregroundColor(Color("color_textview_foreground"))
133
                    Spacer()
134
                }
135
                .padding(.leading, 16)
136
                .padding(.top, 10)
137
 
50 efrain 138
 
139
                Group {
140
                    HStack {
141
 
142
 
143
 
144
                        TextField("",
145
                           text: self.$code
146
                        ).keyboardType(.default)
147
                        .autocapitalization(.none)
148
                        //.foregroundColor(Color("color_textfield_foreground"))
149
                        //.background(Color("color_textfield_background"))
150
                        .padding(.leading, 4)
151
                        Spacer()
152
 
153
                    }
154
                }
155
 
45 efrain 156
                .foregroundColor(Color("color_textfield_foreground"))
157
                .background(Color("color_textfield_background"))
50 efrain 158
                .overlay(RoundedRectangle(cornerRadius: 5).stroke(
159
                    Color(self.code.count == 8 ? "color_textfield_border" : "color_textfield_border_error" )
160
                ))
161
                .padding(.leading, 16)
162
                .padding(.trailing, 16)
163
                .padding(.top, self.code.count == 8 ? 10 : 2)
45 efrain 164
 
50 efrain 165
 
166
                if self.code.count  != 8 {
167
                    HStack {
168
                        Spacer()
169
 
170
                        Text(Config.LANG_SIGNIN_ERROR_EMAIL_FIELD)
171
                        .foregroundColor(.red)
172
                            .font(Font.custom(Config.FONT_NAME_REGULAR, size: 11))
173
 
174
                    }
175
                    .padding(.top, 5)
176
                    .padding(.trailing, 16)
177
                }
178
 
179
 
180
 
45 efrain 181
                Button(action: {
49 efrain 182
                    sendCode();
183
 
45 efrain 184
                }, label: {
185
                    Text(Config.LANG_DELETE_ACCOUNT_BUTTON_SEND)
49 efrain 186
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
187
                    .frame(width: UIScreen.main.bounds.width - 32, height: 35)
50 efrain 188
                    .foregroundColor(Color("color_button_dark_foreground"))
189
                    .background(Color("color_button_dark_background"))
190
                    .border(Color( "color_button_dark_border"), width: Config.BUTTON_BORDER_SIZE)
49 efrain 191
                    .cornerRadius(Config.BUTTON_BORDER_RADIUS)
45 efrain 192
 
193
                })
194
                .padding(.top, 16)
195
                .padding(.leading, 16)
196
                .padding(.trailing, 16)
197
 
49 efrain 198
 
45 efrain 199
 
49 efrain 200
                Button(action: {
201
                    getCode();
202
                }, label: {
203
                    Text(Config.LANG_DELETE_ACCOUNT_BUTTON_REQUEST_CODE)
204
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
205
                    .frame(width: UIScreen.main.bounds.width - 32, height: 35)
206
                    .foregroundColor(Color("color_button_foreground"))
207
                    .background(Color("color_button_background"))
208
                    .border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE)
209
                    .cornerRadius(Config.BUTTON_BORDER_RADIUS)
210
                })
211
                .padding(.top, 16)
212
                .padding(.leading, 16)
213
                .padding(.trailing, 16)
45 efrain 214
 
49 efrain 215
                Spacer()
50 efrain 216
                    /*
49 efrain 217
                Button(action: {
45 efrain 218
 
49 efrain 219
                    withAnimation {
220
                        appNavigation.subpageActive = .profile
221
                    }
222
 
223
                }, label: {
224
                    Text(Config.LANG_DELETE_ACCOUNT_BUTTON_CANCEL)
225
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
226
                    .frame(width: UIScreen.main.bounds.width - 32, height: 35)
227
                    .foregroundColor(Color("color_button_foreground"))
228
                    .background(Color("color_button_background"))
229
                    .border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE)
230
                    .cornerRadius(Config.BUTTON_BORDER_RADIUS)
231
                }) .padding(.top, 16)
45 efrain 232
                .padding(.leading, 16)
233
                .padding(.trailing, 16)
234
 
50 efrain 235
                */
45 efrain 236
            } .alert(isPresented: $presentAlert) {
237
                Alert(
238
                    title: Text(self.titleAlert),
239
                    message: Text(self.messageAlert),
240
                    dismissButton: .default(Text(Config.LANG_COMMON_OK))
241
                )
242
            }
243
        }
244
 
245
    }
246
 
247
    func sendCode() -> Void
248
    {
49 efrain 249
        if code.isEmpty || code.count != 8 {
250
            self.titleAlert = Config.LANG_ERROR_ACCOUNT_DELETE_CODE_IS_EMPTY_TITLE
251
            self.messageAlert = Config.LANG_ERROR_ACCOUNT_DELETE_CODE_IS_EMPTY_MESSAGE
252
            self.presentAlert = true
253
            return;
254
        }
255
 
256
 
257
 
45 efrain 258
        let parameters = [
259
            Constants.POST_COMMENT_FIELD_ADDED_ON: code
260
        ]
261
 
262
 
263
        let headerSecurity : HeaderSecurity = HeaderSecurity()
264
        let headers: HTTPHeaders = [
265
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
266
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
267
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
268
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
269
            .accept(Constants.HTTP_HEADER_ACCEPT)
270
        ]
271
 
272
        self.showProgressView = true
273
        AF.request(Config.URL_DELETE_ACCOUNT, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{(response) in
274
 
275
            self.showProgressView = false
276
            switch response.result {
277
                case .success:
278
                    let json = try? JSON(data: response.data!)
279
                    if json?["success"] ?? "" != false {
280
 
47 efrain 281
                        DispatchQueue.main.async() {
45 efrain 282
                                withAnimation {
283
                                    appData.userUuid = ""
284
                                    appData.userFirstname = ""
285
                                    appData.userLastname = ""
286
                                    appData.userEmail = ""
287
                                    appData.userImage = ""
288
                                    appData.refreshContentActionRequired = false
289
                                    appData.refreshContentMessage = ""
290
                                    appData.refreshContentMessageShowPending = false
291
                                    appData.signoutActionRequired = false
292
                                    appData.save()
293
 
294
                                    appNavigation.pageActive = .goodbyedeleteaccount
295
                                }
296
                            }
297
                    } else {
298
 
299
                        let message = json?["data"].string ?? ""
300
                        if !message.isEmpty {
301
                            self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
302
                            self.messageAlert = message
303
                            self.presentAlert = true
304
                        }
305
                    }
306
 
307
                    return
308
 
309
                case .failure :
310
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
311
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
312
                    self.presentAlert = true
313
 
314
                    return
315
            }
316
        }
317
    }
318
 
319
    func getCode()
320
    {
321
 
322
        let headerSecurity : HeaderSecurity = HeaderSecurity()
323
        let headers: HTTPHeaders = [
324
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
325
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
326
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
327
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
328
            .accept(Constants.HTTP_HEADER_ACCEPT)
329
        ]
330
 
331
        self.showProgressView = true
332
        AF.request(Config.URL_DELETE_ACCOUNT, method: .get, headers: headers).responseJSON{(response) in
333
            self.showProgressView = false
334
            switch response.result {
335
                case .success:
336
                    let json = try? JSON(data: response.data!)
337
 
338
                    let message = json?["data"].string ?? ""
339
                    if !message.isEmpty {
340
                        self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
341
                        self.messageAlert = message
342
                        self.presentAlert = true
343
                    }
344
 
345
                    return
346
 
347
                case .failure :
348
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
349
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
350
                    self.presentAlert = true
351
 
352
                    return
353
            }
354
        }
355
    }
356
}
357
 
358
struct DeleteAccountView_Previews: PreviewProvider {
359
    static var previews: some View {
360
        DeleteAccountView()
361
    }
362
}