// // FillRecoveryCodeViewController.swift // GMERemittance // // Created by Sujal on 12/20/17. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation import UIKit class FillRecoveryCodeViewController: UIViewController { private var fillrecoverycodeviewmodel = FillRecoveryCodeViewModel() private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() @IBOutlet weak var textFieldRecoveryCode: UITextField! @IBAction func gotoPreviousView(_ sender: Any) { _ = navigationController?.popViewController(animated: true) } @IBAction func submitRecoveryCode(_ sender: Any) { let recoverycode: String = textFieldRecoveryCode.text! fillrecoverycodeviewmodel.setCode(code: recoverycode) switch fillrecoverycodeviewmodel.checkCodeLength() { case .Valid: disableUserInteractions() showActivityIndicator(activityIndicator: activityIndicator) fillrecoverycodeviewmodel.resetUser() case .InValid(let error): self.popUpMessageError(value: 11, message: error) } } override func viewDidLoad() { super.viewDidLoad() self.hideKeyboardWhenTappedAround() textFieldRecoveryCode.delegate = self fillrecoverycodeviewmodel.authenticated.value = nil fillrecoverycodeviewmodel.internetConnection.value = nil fillrecoverycodeviewmodel.fillCodeConnectionTimeOut.value = nil /** connection timeout */ fillrecoverycodeviewmodel.fillCodeConnectionTimeOut.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 20) } /** Internet Check */ fillrecoverycodeviewmodel.internetConnection.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 15) } /** Check the authenticated code */ fillrecoverycodeviewmodel.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.fillrecoverycodeviewmodel.getErrorMessage()) return } self.performSegue(withIdentifier: "changePassword", sender: nil) } } } extension FillRecoveryCodeViewController: UITextFieldDelegate { func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { guard let code = textField.text else { return true } let length = code.characters.count + string.characters.count - range.length return length <= 4 } }