// // FillSignUpCodeController.swift // GMERemittance // // Created by Sujal on 12/11/17. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation import UIKit class FillSignUpCodeController: UIViewController { @IBOutlet var labelEnterCodeHeader: UILabel! @IBOutlet weak var buttonSubmitCode: UIButton! private var fillsignupcodeviewmodel = FillCodeViewModel() private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() var deviceORpassword: Int? //1 for device, 2 for password var redirectToOTP: String? @IBOutlet weak var textFieldCode: UITextField! @IBAction func cancelAction(_ sender: Any) { // navigationController?.popViewController(animated: true) } @IBAction func requestCode(_ sender: Any) { if deviceORpassword == 1 { fillsignupcodeviewmodel.setRecoveryType(recoveryType: "device") } else if deviceORpassword == 2 { fillsignupcodeviewmodel.setRecoveryType(recoveryType: "password") } fillsignupcodeviewmodel.requestCode() //self.popUpMessageError(value: 11, message: "Please enter the code sent to your email") } @IBAction func submitCode(_ sender: Any) { let signupcode: String = textFieldCode.text! fillsignupcodeviewmodel.setCode(code: signupcode) switch fillsignupcodeviewmodel.checkCodeLength() { case .Valid: disableUserInteractions() showActivityIndicator(activityIndicator: activityIndicator) if deviceORpassword == 1 { fillsignupcodeviewmodel.authenticate() } else if deviceORpassword == 2 { fillsignupcodeviewmodel.enablePasswordChange() } else { self.dismissActivityIndicator(activityIndicator: self.activityIndicator) } case .InValid(let error): self.popUpMessageError(value: 11, message: error) } } override func viewDidLoad() { super.viewDidLoad() setUpNavBar(id: 100, title: "") let userId = UserDefaults.standard.object(forKey: "com.gmeremit.username") as! String if let _ = Int(userId) { labelEnterCodeHeader.text = "Enter Four-Digit Code send via sms" } else { labelEnterCodeHeader.text = "Enter Four-Digit Code send via email" } if self.redirectToOTP != nil{ self.popUpMessageInfo(value: 16, title: "Alert", message: self.redirectToOTP!) } if deviceORpassword == 1 { buttonSubmitCode.setTitle("Done", for: .normal) } else if deviceORpassword == 2 { buttonSubmitCode.setTitle("Recovery Password", for: .normal) } self.hideKeyboardWhenTappedAround() textFieldCode.delegate = self fillsignupcodeviewmodel.setUserId(userId: UserDefaults.standard.object(forKey: "com.gmeremit.username") as! String) fillsignupcodeviewmodel.fillCodeConnectionTimeOut.value = nil /** connection timeout */ fillsignupcodeviewmodel.fillCodeConnectionTimeOut.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 20) } fillsignupcodeviewmodel.internetConnection.value = nil /** Internet check */ fillsignupcodeviewmodel.internetConnection.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 15) } /** Update authenticate code */ fillsignupcodeviewmodel.authenticated.bind { [unowned self] in guard $0 != nil else { return } self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.enableUserInteractions() guard $0! else { self.popUpMessageError(value: 10, message: self.fillsignupcodeviewmodel.getErrorMessage()) return } if self.deviceORpassword == 1 { self.performSegue(withIdentifier: "loginNormalMode", sender: nil) } else if self.deviceORpassword == 2 { self.performSegue(withIdentifier: "changePassword", sender: nil) } } } } extension FillSignUpCodeController: UITextFieldDelegate { func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { guard let code = textField.text else { return true } let length = code.count + string.count - range.length return length <= 4 } }