// // SignUpViewController.swift // GMERemittance // // Created by Kushal on 11/30/17. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit class SignUpViewController: UIViewController { var isScrolled: Bool = false private var activeTextField: UITextField? private var signupviewmodel = SignUpViewModel() private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() @IBOutlet weak var textFieldUsername: UITextField! @IBOutlet weak var textFieldPassword: UITextField! @IBOutlet weak var textFieldPasswordConfirm: UITextField! @IBOutlet weak var viewContent: UIView! @IBAction func initialSignUp(_ sender: Any) { activeTextField?.resignFirstResponder() if viewScrolled { performScroll(direction: 1) isScrolled = !isScrolled } signupviewmodel.signUpConnectionTimeOut.value = nil /** connection timeout */ signupviewmodel.signUpConnectionTimeOut.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 20) } disableUserInteractions() showActivityIndicator(activityIndicator: activityIndicator) signupviewmodel.setUserCredentials(username: textFieldUsername.text!, password: textFieldPassword.text!, confirmpassword: textFieldPasswordConfirm.text!) switch signupviewmodel.validateUser() { case .Valid: signupviewmodel.register() case .InValid(let error): enableUserInteractions() dismissActivityIndicator(activityIndicator: activityIndicator) self.popUpMessageError(value: 11, message: error) } } @objc func appMovedToForeground() { activeTextField?.resignFirstResponder() if isScrolled { isScrolled = !isScrolled } } override func viewDidLoad() { super.viewDidLoad() setUpNavBar(id: 100, title: "") NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil) registerTapListener() self.view.clipsToBounds = true textFieldUsername.autocorrectionType = .no textFieldPassword.autocorrectionType = .no textFieldPasswordConfirm.autocorrectionType = .no textFieldUsername.delegate = self textFieldPassword.delegate = self textFieldPasswordConfirm.delegate = self textFieldUsername.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor textFieldUsername.layer.borderWidth = 1.0 textFieldUsername.layer.cornerRadius = 5 textFieldPassword.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor textFieldPassword.layer.borderWidth = 1.0 textFieldPassword.layer.cornerRadius = 5 textFieldPasswordConfirm.layer.borderColor = UIColor(red:0.91, green:0.93, blue:0.95, alpha:1.0).cgColor textFieldPasswordConfirm.layer.borderWidth = 1.0 textFieldPasswordConfirm.layer.cornerRadius = 5 /** Internet Check */ signupviewmodel.internetConnection.bind { [unowned self] in guard $0 != nil else { return } self.enableUserInteractions() self.dismissActivityIndicator(activityIndicator: self.activityIndicator) self.popUpMessage(value: 15) } /** Update user registration info */ signupviewmodel.registered.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.signupviewmodel.getErrorMessage()) return } self.performSegue(withIdentifier: "fillOTP", sender: nil) } } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "fillOTP" { let fillcodeViewController = segue.destination as! FillSignUpCodeController fillcodeViewController.deviceORpassword = 1 } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } // UITextFieldDelegate and Scroll Protocol goes here extension SignUpViewController: UITextFieldDelegate { func textFieldDidBeginEditing(_ textField: UITextField) { activeTextField = textField checkScroll() } func textFieldDidEndEditing(_ textField: UITextField) { checkScroll() } /** Curser move to next textfield when return button click in keyboad */ func textFieldShouldReturn(_ textField: UITextField) -> Bool { if textField == textFieldUsername { textFieldPassword.becomeFirstResponder() } else if textField == textFieldPassword { textFieldPasswordConfirm.becomeFirstResponder() } else { textFieldPasswordConfirm.resignFirstResponder() } return true } } extension SignUpViewController: ScrollableProtocol { /** - Returns offset value to animate */ var offset: CGFloat { get { return viewContent.frame.origin.y - 5 } } /** - Returns Bool value to scroll */ var viewScrolled: Bool { get { return isScrolled } } /** Check if textfield is active and required scroll */ func checkScroll() { if !viewScrolled { performScroll(direction: 0) isScrolled = !isScrolled } else { performScroll(direction: 1) isScrolled = !isScrolled } } /** Dissmiss keyboard when tap outside to textfield */ func registerTapListener() { let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(clearKeyboard)) view.addGestureRecognizer(tap) } /** Dissmiss keyboard */ @objc func clearKeyboard() { activeTextField?.resignFirstResponder() if viewScrolled { performScroll(direction: 1) isScrolled = !isScrolled } } /** Animate scroll UI in case when textfield is active and also if inactive. */ func performScroll(direction: Int) { if direction == 0 { UIView.animate(withDuration: 0.3, animations: { self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.offset * -1) }) } else if direction == 1 { UIView.animate(withDuration: 0.3, animations: { self.view.frame = self.view.frame.offsetBy(dx: 0, dy: self.offset) }) } } }