Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

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