Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 47 | Rev 49 | 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
 
82
                Text(Config.LANG_DELETE_ACCOUNT_MESSAGE)
83
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_DELETE_ACCOUNT_TEXT))
84
 
85
                TextField("",
86
                    text: self.$code)
87
 
88
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: 12))
89
                .textFieldStyle(PlainTextFieldStyle())
90
                .frame(height: 32)
91
                    .keyboardType(.default)
92
                .autocapitalization(.none)
93
                .foregroundColor(Color("color_textfield_foreground"))
94
                .background(Color("color_textfield_background"))
95
                .padding(.leading, 4)
96
                .frame(width: UIScreen.main.bounds.width - 32, height: 35)
97
 
98
                Button(action: {
99
 
100
 
101
                }, label: {
102
                    Text(Config.LANG_DELETE_ACCOUNT_BUTTON_SEND)
103
                     .font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
104
                     .frame(width: UIScreen.main.bounds.width - 32, height: 35)
105
                        .foregroundColor(Color("color_button_foreground"))
106
                        .background(Color("color_button_background"))
107
                        .border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE)
108
                        .cornerRadius(Config.BUTTON_BORDER_RADIUS)
109
 
110
                })
111
                .padding(.top, 16)
112
                .padding(.leading, 16)
113
                .padding(.trailing, 16)
114
 
115
                Spacer()
116
 
117
                HStack {
118
 
119
                    Button(action: {
120
 
121
 
122
                    }, label: {
123
                        Text(Config.LANG_DELETE_ACCOUNT_BUTTON_REQUEST_CODE)
124
                            .font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
125
                            .foregroundColor(Color("color_button_foreground"))
126
                            .background(Color("color_button_background"))
127
                            .border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE)
128
                            .cornerRadius(Config.BUTTON_BORDER_RADIUS)
129
                            .padding(10)
130
                    })
131
 
132
                    Spacer()
133
 
134
                    Button(action: {
135
 
136
 
137
                    }, label: {
138
                        Text(Config.LANG_DELETE_ACCOUNT_BUTTON_CANCEL)
139
                            .font(Font.custom(Config.FONT_NAME_REGULAR, size: 13))
140
                            .foregroundColor(Color("color_button_foreground"))
141
                            .background(Color("color_button_background"))
142
                            .border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE)
143
                            .cornerRadius(Config.BUTTON_BORDER_RADIUS)
144
                            .padding(10)
145
 
146
 
147
 
148
                    })
149
 
150
                }
151
                .padding(.top, 16)
152
                .padding(.leading, 16)
153
                .padding(.trailing, 16)
154
 
155
 
156
            } .alert(isPresented: $presentAlert) {
157
                Alert(
158
                    title: Text(self.titleAlert),
159
                    message: Text(self.messageAlert),
160
                    dismissButton: .default(Text(Config.LANG_COMMON_OK))
161
                )
162
            }
163
        }
164
 
165
    }
166
 
167
    func sendCode() -> Void
168
    {
169
        let parameters = [
170
            Constants.POST_COMMENT_FIELD_ADDED_ON: code
171
        ]
172
 
173
 
174
        let headerSecurity : HeaderSecurity = HeaderSecurity()
175
        let headers: HTTPHeaders = [
176
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
177
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
178
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
179
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
180
            .accept(Constants.HTTP_HEADER_ACCEPT)
181
        ]
182
 
183
        self.showProgressView = true
184
        AF.request(Config.URL_DELETE_ACCOUNT, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{(response) in
185
 
186
            self.showProgressView = false
187
            switch response.result {
188
                case .success:
189
                    let json = try? JSON(data: response.data!)
190
                    if json?["success"] ?? "" != false {
191
 
47 efrain 192
                        DispatchQueue.main.async() {
45 efrain 193
                                withAnimation {
194
                                    appData.userUuid = ""
195
                                    appData.userFirstname = ""
196
                                    appData.userLastname = ""
197
                                    appData.userEmail = ""
198
                                    appData.userImage = ""
199
                                    appData.refreshContentActionRequired = false
200
                                    appData.refreshContentMessage = ""
201
                                    appData.refreshContentMessageShowPending = false
202
                                    appData.signoutActionRequired = false
203
                                    appData.save()
204
 
205
                                    appNavigation.pageActive = .goodbyedeleteaccount
206
                                }
207
                            }
208
                    } else {
209
 
210
                        let message = json?["data"].string ?? ""
211
                        if !message.isEmpty {
212
                            self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
213
                            self.messageAlert = message
214
                            self.presentAlert = true
215
                        }
216
                    }
217
 
218
                    return
219
 
220
                case .failure :
221
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
222
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
223
                    self.presentAlert = true
224
 
225
                    return
226
            }
227
        }
228
    }
229
 
230
    func getCode()
231
    {
232
 
233
        let headerSecurity : HeaderSecurity = HeaderSecurity()
234
        let headers: HTTPHeaders = [
235
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
236
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
237
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
238
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
239
            .accept(Constants.HTTP_HEADER_ACCEPT)
240
        ]
241
 
242
        self.showProgressView = true
243
        AF.request(Config.URL_DELETE_ACCOUNT, method: .get, headers: headers).responseJSON{(response) in
244
            self.showProgressView = false
245
            switch response.result {
246
                case .success:
247
                    let json = try? JSON(data: response.data!)
248
 
249
                    let message = json?["data"].string ?? ""
250
                    if !message.isEmpty {
251
                        self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
252
                        self.messageAlert = message
253
                        self.presentAlert = true
254
                    }
255
 
256
                    return
257
 
258
                case .failure :
259
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
260
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
261
                    self.presentAlert = true
262
 
263
                    return
264
            }
265
        }
266
    }
267
}
268
 
269
struct DeleteAccountView_Previews: PreviewProvider {
270
    static var previews: some View {
271
        DeleteAccountView()
272
    }
273
}