// // RegisterViewController.swift // GMERemittance // // Created by gme_2 on 10/09/2018. //Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import Localize_Swift class RegisterViewController: UIViewController { struct StringConstants { let userIdTitle = "userid_title_text".localized() let useridPlaceholder = "userid_placeholder_text".localized() let useridError = "userid_error_text".localized() let passwordTitle = "password_text".localized() let passwordPlaceholder = "password_policy_error".localized() let passwordError = "password_policy_error".localized() let confirmPasswordTitle = "confirm_password_text".localized() let confirmPasswordPlaceholder = "confirm_password_text".localized() let dobTitlePlaceholder = "dob_text".localized() let alreadyHaveAccountText = "already_have_account_text".localized() let loginText = "login_text".localized() let registerText = "register_text".localized() let registerHeader = "register_title_text".localized() let registerSubtitle = "register_subtitle_text".localized() let mobileNumber = "mobile_number_text".localized() let mobilePlaceholder = "010xxxxxxxx"//"mobile_number_placeholder_text".localized() let nativeCountry = "native_country_text".localized() let nativeCountryPlaceholder = "native_country_placeholder_text".localized() let nativeCountryError = "kyc_native_country_error".localized() let mobileNumberError = "kyc_mobile_invalid_number_error".localized() } // MARK: IBOutlets @IBOutlet weak var idTextField: ValidationTextField! @IBOutlet weak var passwordTextField: ValidationTextField! @IBOutlet weak var nativeCountryTextField: ValidationTextField! @IBOutlet weak var mobileTextField: ValidationTextField! // header labels @IBOutlet weak var headerLabel: UILabel! @IBOutlet weak var alreadyHaveAccountLabel: UILabel! // buttons @IBOutlet weak var loginButton: UIButton! @IBOutlet weak var registerButton: UIButton! // MARK: Properties var presenter: RegisterModuleInterface? private var encryptedPassword: String = "" private lazy var countries = StaticModels().nativeCountries private var validDic = [ "id": false, "password": false, "country": false, "mobile": false ] private var selectedNativeCountry: NativeCountryModel? { didSet { nativeCountryTextField.text = selectedNativeCountry?.text nativeCountryTextField.sendActions(for: .editingChanged) } } private var isValid = false { didSet { registerButton.isEnabled = isValid registerButton.backgroundColor = isValid ? .themeRed : .lightGray } } private var isNotDuplicate = true { didSet { idTextField.sendActions(for: .editingChanged) } } // MARK: VC's Life cycle override func viewDidLoad() { super.viewDidLoad() self.setup() idTextField.delegate = self } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.setupPicturedNavBar() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.navigationItem.title = "" } // MARK: IBActions @IBAction func login(_ sender: UIButton) { self.presenter?.login() } @IBAction func register(_ sender: UIButton) { let model = RegisterRequestModel() model.username = idTextField.text model.encryptedPassword = encryptedPassword model.nativeCountry = selectedNativeCountry?.id model.mobileNumber = mobileTextField.text self.presenter?.register(model: model) } // MARK: Other Functions private func setup() { // all setup should be done here setupPicturedNavBar() configureLanguage() passwordTextField.delegate = self nativeCountryTextField.delegate = self setCustomTextFields() removeNonASCII( textFields: [ idTextField, passwordTextField, nativeCountryTextField, mobileTextField ] ) registerButton.layer.cornerRadius = 5 } private func setCustomTextFields() { passwordTextField.inputView = UIView() passwordTextField.keyboardToolbar.isHidden = true nativeCountryTextField.inputView = UIView() nativeCountryTextField.keyboardToolbar.isHidden = true } private func configureLanguage() { headerLabel.text = StringConstants().registerHeader alreadyHaveAccountLabel.text = StringConstants().alreadyHaveAccountText idTextField.titleText = StringConstants().userIdTitle idTextField.placeholder = StringConstants().useridPlaceholder idTextField.errorMessage = StringConstants().useridError idTextField.validCondition = {3 < $0.count && $0.count < 51 && self.isNotDuplicate} passwordTextField.titleText = StringConstants().passwordTitle passwordTextField.placeholder = StringConstants().passwordPlaceholder passwordTextField.errorMessage = StringConstants().passwordError passwordTextField.validCondition = { $0.count > 5 } nativeCountryTextField.titleText = StringConstants().nativeCountry nativeCountryTextField.placeholder = StringConstants().nativeCountryPlaceholder nativeCountryTextField.errorMessage = StringConstants().nativeCountryError nativeCountryTextField.validCondition = { !$0.isEmpty } mobileTextField.titleText = StringConstants().mobileNumber mobileTextField.placeholder = StringConstants().mobilePlaceholder mobileTextField.errorMessage = StringConstants().mobileNumberError mobileTextField.validCondition = { $0.count > 9 && $0.count < 12} loginButton.setTitle(StringConstants().loginText, for: .normal) registerButton.setTitle(StringConstants().registerText, for: .normal) } private func showCountryPickerview() { TablePresenterWireframe().openWith( tag: 0, delegate: self, model: countries, source: self ) } private func removeNonASCII(textFields: [UITextField]) { textFields.forEach { $0.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged) } } @objc private func editingChanged(_ textField: UITextField) { guard let tf = textField as? ValidationTextField else {return} switch tf { case idTextField: textField.removeNonASCII() idTextField.filterForUserIDFormat() idTextField.errorMessage = StringConstants().useridError validDic["id"] = tf.isValid case passwordTextField: validDic["password"] = tf.isValid case nativeCountryTextField: textField.removeNonASCII() validDic["country"] = tf.isValid case mobileTextField: textField.removeNonASCII() validDic["mobile"] = tf.isValid default: break } isValid = validDic.allSatisfy { $0.value } } } // MARK: RegisterViewInterface extension RegisterViewController: RegisterViewInterface { func showLoading() { self.showProgressHud() } func hideLoading() { self.hideProgressHud() } func show(error: String) { self.alert(type: .error, message: error) } func show(message: String) { self.alertWithOk( type: .success, message: message, title: "Success", okTitle: "Ok" ) { GMEDB.shared.user.set(self.selectedNativeCountry?.code, .countryCode) GMEDB.shared.user.set(self.mobileTextField.text, .mobileNumber) guard let id = self.idTextField.text else { return } GMEDB.shared.user.set(id, .userId) KeyChain.shared.save(data: id, key: .temporaryID) KeyChain.shared.save(data: self.encryptedPassword, key: .temporaryPW) self.presenter?.showKyc() } } func isNotDuplicate(_ bool: Bool, _ message: String?) { isNotDuplicate = bool idTextField.errorMessage = bool ? StringConstants().useridError : message ?? "duplicated id" } } // MARK: - UITextFieldDelegate extension RegisterViewController: UITextFieldDelegate { func textFieldDidBeginEditing(_ textField: UITextField) { switch textField { case idTextField: isNotDuplicate = true case passwordTextField: let secureKeypad = SecureKeypad(target: self) secureKeypad.delegate = self secureKeypad.present(animated: true) case nativeCountryTextField: showCountryPickerview() default: () } } func textFieldDidEndEditing(_ textField: UITextField) { if textField == idTextField { let text = textField.text ?? "" if text.count > 3 && text.count < 51 { presenter?.isDuplicate(userName: text) } } } } // MARK: - SecureKeypadDelegate extension RegisterViewController: SecureKeypadDelegate { func didComplete(_ encryptedString: String, garbagePassword: String, length: Int) { encryptedPassword = encryptedString passwordTextField.text = garbagePassword passwordTextField.sendActions(for: .editingChanged) } } // MARK: - TablePresenterDelegate extension RegisterViewController: TablePresenterDelegate { func tablePresenterView(_ viewController: TablePresenterViewController) -> TablePresenterConfiguration { return TablePresenterConfiguration( presenterTitle: "select_country_text".localized(), closeButtonTitle: "cancel_text".localized(), notFoundTitle: "no_result_found_text".localized(), searchBarPlaceHolder: "search_country_text".localized() ) } func tablePresenterView( _ viewController: TablePresenterViewController, didSelectModel model: TablePresenterProtocol? ) { selectedNativeCountry = (model as? NativeCountryModel) ?? selectedNativeCountry } }