Rev 2 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
//// DeleteAccountView.swift// twogetskills//// Created by Efrain Yanez Recanatini on 8/24/22.//import SwiftUIimport AudioToolboximport Networkimport Alamofireimport SwiftyJSONimport TTGSnackbarstruct DeleteAccountView: View {@EnvironmentObject private var networkMonitor : NetworkMonitor@EnvironmentObject private var appNavigation : AppNavigation@State private var presentAlert : Bool = false@State private var titleAlert : String = ""@State private var messageAlert : String = ""@State private var showProgressView : Bool = false@State private var code: String = "" {didSet {if code.count > 8 {code = String(code.prefix(8))AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { return }}}}private let appData = AppData.sharedInstancevar body: some View {ZStack {Color("color_window_background").edgesIgnoringSafeArea(.all)if self.showProgressView {ProgressView().progressViewStyle(CircularProgressViewStyle(tint: Color("color_progress_view_foreground"))).scaleEffect(3, anchor: .center).zIndex(100000)}VStack(spacing: 0) {HStack {Button(action: {withAnimation {appNavigation.subpageActive = .profile}}, label: {Image(systemName: "chevron.backward").frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/).aspectRatio(contentMode: .fit).foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))}).padding(.leading, 16)Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : Config.LANG_DELETE_ACCOUNT_TITLE).font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 )).foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground")).padding(.leading, 4)Spacer()}.edgesIgnoringSafeArea(.top).frame(height: 50).background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))HStack {if appData.userImage.isEmpty {Image("logo").resizable().aspectRatio(contentMode: .fit).frame(height: Config.PROFILE_IMAGE_SIZE).clipShape(Circle()).overlay(Circle().stroke(Color.white, lineWidth: 4)).shadow(radius: 10)} else {CustomAsyncImageProfile(url: URL(string: appData.userImage)!,placeholder: { Text(Config.LANG_COMMON_LOADING).font(.footnote).foregroundColor(.black)},image: {Image(uiImage: $0).resizable()})}}.padding(10)VStack(spacing: 5) {VStack(spacing: 5) {Text("\(appData.userFirstname) \(appData.userLastname)" ).bold().font(.title)Text("\(appData.userEmail)").font(.body).foregroundColor(Color("color_textview_foreground"))}.padding()}.background(Color("color_card_view_background"))HStack {Text(Config.LANG_DELETE_ACCOUNT_MESSAGE).font(Font.custom(Config.FONT_NAME_REGULAR, size: 12)).foregroundColor(Color("color_textview_foreground"))}.padding(.top, 20).padding(.horizontal, 16)HStack {Text(Config.LANG_DELETE_ACCOUNT_FIELD_CODE_LABEL).font(Font.custom(Config .FONT_NAME_REGULAR, size: 11)).foregroundColor(Color("color_textview_foreground"))Spacer()}.padding(.leading, 16).padding(.top, 10)Group {HStack {TextField("", text: self.$code).font(Font.custom(Config.FONT_NAME_REGULAR, size: 12)).textFieldStyle(PlainTextFieldStyle()).frame(height: 32).keyboardType(.emailAddress).autocapitalization(.none).padding(.leading, 4)Spacer()}}.foregroundColor(Color("color_textfield_foreground")).background(Color("color_textfield_background")).overlay(RoundedRectangle(cornerRadius: 5).stroke(Color(self.code.count == 8 ? "color_textfield_border" : "color_textfield_border_error" ))).padding(.leading, 16).padding(.trailing, 16).padding(.top, self.code.count == 0 ? 10 : 2)if self.code.count != 8 {HStack {Spacer()Text(Config.LANG_DELETE_ACCOUNTCODE_IS_WRONG_MESSAGE).foregroundColor(.red).font(Font.custom(Config.FONT_NAME_REGULAR, size: 11))}.padding(.top, 5).padding(.trailing, 16)}Button(action: {sendCode();}, label: {Text(Config.LANG_DELETE_ACCOUNT_BUTTON_SEND).font(Font.custom(Config.FONT_NAME_REGULAR, size: 13)).frame(width: UIScreen.main.bounds.width - 32, height: 35).foregroundColor(Color("color_button_dark_foreground")).background(Color("color_button_dark_background")).border(Color( "color_button_dark_border"), width: Config.BUTTON_BORDER_SIZE).cornerRadius(Config.BUTTON_BORDER_RADIUS)}).padding(.top, 16).padding(.leading, 16).padding(.trailing, 16)Button(action: {getCode();}, label: {Text(Config.LANG_DELETE_ACCOUNT_BUTTON_REQUEST_CODE).font(Font.custom(Config.FONT_NAME_REGULAR, size: 13)).frame(width: UIScreen.main.bounds.width - 32, height: 35).foregroundColor(Color("color_button_foreground")).background(Color("color_button_background")).border(Color("color_button_border"), width: Config.BUTTON_BORDER_SIZE).cornerRadius(Config.BUTTON_BORDER_RADIUS)}).padding(.top, 16).padding(.leading, 16).padding(.trailing, 16)} .alert(isPresented: $presentAlert) {Alert(title: Text(self.titleAlert),message: Text(self.messageAlert),dismissButton: .default(Text(Config.LANG_COMMON_OK)))}}}func sendCode() -> Void{if code.isEmpty || code.count != 8 {return;}let parameters = [Constants.POST_DELETE_ACCOUNT_CODE: code]let headerSecurity : HeaderSecurity = HeaderSecurity()let headers: HTTPHeaders = [.init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),.init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),.init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),.init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),.accept(Constants.HTTP_HEADER_ACCEPT)]self.showProgressView = trueAF.request(Config.URL_DELETE_ACCOUNT, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{(response) inself.showProgressView = falseswitch response.result {case .success:let json = try? JSON(data: response.data!)if json?["success"] ?? "" != false {DispatchQueue.main.async() {withAnimation {appData.userUuid = ""appData.userFirstname = ""appData.userLastname = ""appData.userEmail = ""appData.userImage = ""appData.refreshContentActionRequired = falseappData.refreshContentMessage = ""appData.refreshContentMessageShowPending = falseappData.signoutActionRequired = falseappData.save()appNavigation.pageActive = .goodbyedeleteaccount}}} else {self.code = ""let message = json?["data"]["message"].string ?? ""if !message.isEmpty {self.titleAlert = Config.LANG_ERROR_GENERIC_TITLEself.messageAlert = messageself.presentAlert = true}}returncase .failure :self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLEself.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGEself.presentAlert = truereturn}}}func getCode(){let headerSecurity : HeaderSecurity = HeaderSecurity()let headers: HTTPHeaders = [.init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),.init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),.init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),.init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),.accept(Constants.HTTP_HEADER_ACCEPT)]self.showProgressView = trueAF.request(Config.URL_DELETE_ACCOUNT, method: .get, headers: headers).responseJSON{(response) inself.showProgressView = falseswitch response.result {case .success:let json = try? JSON(data: response.data!)let message = json?["data"]["message"].string ?? ""if !message.isEmpty {let snackbar = TTGSnackbar(message: message, duration: .long);snackbar.show()}returncase .failure :self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLEself.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGEself.presentAlert = truereturn}}}}struct DeleteAccountView_Previews: PreviewProvider {static var previews: some View {DeleteAccountView()}}