Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 45 | Rev 48 | 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
9
import Network
10
import Alamofire
11
import SwiftyJSON
12
import TTGSnackbar
13
 
14
struct DeleteAccountView: View {
15
    @EnvironmentObject private var networkMonitor : NetworkMonitor
16
    @EnvironmentObject private var appNavigation : AppNavigation
17
 
18
    @State private var presentAlert : Bool = false
19
    @State private var titleAlert : String = ""
20
    @State private var messageAlert : String = ""
21
 
22
    @State private var showProgressView : Bool = false
23
 
24
    @State private var code: String = ""  {
25
        didSet {
26
            if code.count > 8 {
27
                code = String(pcode.prefix(8))
28
                AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { return }
29
            }
30
        }
31
    }
32
 
33
 
34
 
35
    private let appData = AppData.sharedInstance
36
 
37
 
38
    var body: some View {
39
        ZStack {
40
 
41
            Color("color_window_background")
42
                    .edgesIgnoringSafeArea(.all)
43
 
44
            if self.showProgressView {
45
                ProgressView()
46
                    .progressViewStyle(CircularProgressViewStyle(tint: Color("color_progress_view_foreground")))
47
                    .scaleEffect(3, anchor: .center).zIndex(100000)
48
            }
49
 
50
            VStack(spacing: 0) {
51
                HStack {
52
                    Button(action: {
53
                        withAnimation {
54
                            appNavigation.subpageActive = .profile
55
                        }
56
 
57
                    }, label: {
58
 
59
 
60
                        Image(systemName: "chevron.backward")
61
                        .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
62
                        .aspectRatio(contentMode: .fit)
63
                        .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
64
                    })
65
                    .padding(.leading, 16)
66
 
67
                    Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : Config.LANG_DELETE_ACCOUNT_TITLE)
68
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
69
                        .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
70
                        .padding(.leading, 4)
71
 
72
                    Spacer()
73
                }
74
                .edgesIgnoringSafeArea(.top)
75
                .frame(height: 50)
76
                .background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
77
 
78
 
79
                Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
80
 
81
                Text(Config.LANG_DELETE_ACCOUNT_MESSAGE)
82
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_DELETE_ACCOUNT_TEXT))
83
 
84
                TextField("",
85
                    text: self.$code)
86
 
87
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: 12))
88
                .textFieldStyle(PlainTextFieldStyle())
89
                .frame(height: 32)
90
                    .keyboardType(.default)
91
                .autocapitalization(.none)
92
                .foregroundColor(Color("color_textfield_foreground"))
93
                .background(Color("color_textfield_background"))
94
                .padding(.leading, 4)
95
                .frame(width: UIScreen.main.bounds.width - 32, height: 35)
96
 
97
                Button(action: {
98
 
99
 
100
                }, label: {
101
                    Text(Config.LANG_DELETE_ACCOUNT_BUTTON_SEND)
102
                     .font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
103
                     .frame(width: UIScreen.main.bounds.width - 32, height: 35)
104
                        .foregroundColor(Color("color_button_foreground"))
105
                        .background(Color("color_button_background"))
106
                        .border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE)
107
                        .cornerRadius(Config.BUTTON_BORDER_RADIUS)
108
 
109
                })
110
                .padding(.top, 16)
111
                .padding(.leading, 16)
112
                .padding(.trailing, 16)
113
 
114
                Spacer()
115
 
116
                HStack {
117
 
118
                    Button(action: {
119
 
120
 
121
                    }, label: {
122
                        Text(Config.LANG_DELETE_ACCOUNT_BUTTON_REQUEST_CODE)
123
                            .font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
124
                            .foregroundColor(Color("color_button_foreground"))
125
                            .background(Color("color_button_background"))
126
                            .border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE)
127
                            .cornerRadius(Config.BUTTON_BORDER_RADIUS)
128
                            .padding(10)
129
                    })
130
 
131
                    Spacer()
132
 
133
                    Button(action: {
134
 
135
 
136
                    }, label: {
137
                        Text(Config.LANG_DELETE_ACCOUNT_BUTTON_CANCEL)
138
                            .font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
139
                            .foregroundColor(Color("color_button_foreground"))
140
                            .background(Color("color_button_background"))
141
                            .border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE)
142
                            .cornerRadius(Config.BUTTON_BORDER_RADIUS)
143
                            .padding(10)
144
 
145
 
146
 
147
                    })
148
 
149
                }
150
                .padding(.top, 16)
151
                .padding(.leading, 16)
152
                .padding(.trailing, 16)
153
 
154
 
155
            } .alert(isPresented: $presentAlert) {
156
                Alert(
157
                    title: Text(self.titleAlert),
158
                    message: Text(self.messageAlert),
159
                    dismissButton: .default(Text(Config.LANG_COMMON_OK))
160
                )
161
            }
162
        }
163
 
164
    }
165
 
166
    func sendCode() -> Void
167
    {
168
        let parameters = [
169
            Constants.POST_COMMENT_FIELD_ADDED_ON: code
170
        ]
171
 
172
 
173
        let headerSecurity : HeaderSecurity = HeaderSecurity()
174
        let headers: HTTPHeaders = [
175
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
176
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
177
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
178
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
179
            .accept(Constants.HTTP_HEADER_ACCEPT)
180
        ]
181
 
182
        self.showProgressView = true
183
        AF.request(Config.URL_DELETE_ACCOUNT, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{(response) in
184
 
185
            self.showProgressView = false
186
            switch response.result {
187
                case .success:
188
                    let json = try? JSON(data: response.data!)
189
                    if json?["success"] ?? "" != false {
190
 
47 efrain 191
                        DispatchQueue.main.async() {
45 efrain 192
                                withAnimation {
193
                                    appData.userUuid = ""
194
                                    appData.userFirstname = ""
195
                                    appData.userLastname = ""
196
                                    appData.userEmail = ""
197
                                    appData.userImage = ""
198
                                    appData.refreshContentActionRequired = false
199
                                    appData.refreshContentMessage = ""
200
                                    appData.refreshContentMessageShowPending = false
201
                                    appData.signoutActionRequired = false
202
                                    appData.save()
203
 
204
                                    appNavigation.pageActive = .goodbyedeleteaccount
205
                                }
206
                            }
207
                    } else {
208
 
209
                        let message = json?["data"].string ?? ""
210
                        if !message.isEmpty {
211
                            self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
212
                            self.messageAlert = message
213
                            self.presentAlert = true
214
                        }
215
                    }
216
 
217
                    return
218
 
219
                case .failure :
220
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
221
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
222
                    self.presentAlert = true
223
 
224
                    return
225
            }
226
        }
227
    }
228
 
229
    func getCode()
230
    {
231
 
232
        let headerSecurity : HeaderSecurity = HeaderSecurity()
233
        let headers: HTTPHeaders = [
234
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
235
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
236
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
237
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
238
            .accept(Constants.HTTP_HEADER_ACCEPT)
239
        ]
240
 
241
        self.showProgressView = true
242
        AF.request(Config.URL_DELETE_ACCOUNT, method: .get, headers: headers).responseJSON{(response) in
243
            self.showProgressView = false
244
            switch response.result {
245
                case .success:
246
                    let json = try? JSON(data: response.data!)
247
 
248
                    let message = json?["data"].string ?? ""
249
                    if !message.isEmpty {
250
                        self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
251
                        self.messageAlert = message
252
                        self.presentAlert = true
253
                    }
254
 
255
                    return
256
 
257
                case .failure :
258
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
259
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
260
                    self.presentAlert = true
261
 
262
                    return
263
            }
264
        }
265
    }
266
}
267
 
268
struct DeleteAccountView_Previews: PreviewProvider {
269
    static var previews: some View {
270
        DeleteAccountView()
271
    }
272
}