Advertisement
Guest User

Untitled

a guest
Oct 8th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 10.11 KB | None | 0 0
  1. //
  2. //  LoginViewController.swift
  3. //  digipass
  4. //
  5. //  Created by Сергей Сивагин on 9/13/18.
  6. //  Copyright © 2018 CardPay. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class LoginViewController: BaseViewController {
  12.  
  13.     // MARK: - Private properties
  14.    
  15.     private var progress: ProgressHUD?
  16.     private var isActivated: Bool = false
  17.    
  18.     var isBackImageViewEnabled: Bool = false
  19.    
  20.     // MARK: - IBOutlet properties
  21.    
  22.     @IBOutlet weak var loginTextField: ExtendedTextField!
  23.     @IBOutlet weak var passwordTextField: ExtendedTextField!
  24.     @IBOutlet weak var loginView: UIView!
  25.     @IBOutlet weak var phoneLabel: LinkedLabel!
  26.     @IBOutlet weak var mailLabel: LinkedLabel!
  27.     @IBOutlet weak var showFAQImageView: UIImageView!
  28.     @IBOutlet weak var nextButton: BlueButton!
  29.     @IBOutlet weak var scrollView: UIScrollView!
  30.     @IBOutlet weak var contentView: UIView!
  31.     @IBOutlet weak var backStackView: UIStackView!
  32.     @IBOutlet weak var backImageView: UIImageView!
  33.     @IBOutlet weak var screenTitleLabel: UILabel!
  34.    
  35.     // MARK: - Override methods
  36.    
  37.     override func viewWillAppear(_ animated: Bool) {
  38.         super.viewWillAppear(animated)
  39.         addNavigationBarLogotype()
  40.         isNeedExpireTimeout = false
  41.     }
  42.    
  43.     override func viewDidLoad() {
  44.         super.viewDidLoad()
  45.         ActivationOverseerUtils.shared.delegate = self
  46.         progress = ProgressHUD(with: view)
  47.         progress?.simplyShow()
  48.         progress?.suspendOperation()
  49.         configureCommonView()
  50.         configureLoginTextField()
  51.         configurePasswordTextField()
  52.         configurePhoneLabel()
  53.         configureEmailLabel()
  54.         configureShowFAQImageView()
  55.         configureNextButton()
  56.         setupRecognizer()
  57.         if #available(iOS 11.0, *) {
  58.             scrollView.contentInsetAdjustmentBehavior = .never
  59.         } else {
  60.             automaticallyAdjustsScrollViewInsets = false
  61.         }
  62.         scrollView.contentInset = UIEdgeInsets.zero
  63.     }
  64.    
  65.     override func onKeyboardBegin(_ notification: Notification) {
  66.         guard let info: NSDictionary = notification.userInfo as NSDictionary? else {
  67.             return
  68.         }
  69.         let keyboardSize = info.object(forKey: UIKeyboardFrameBeginUserInfoKey) as? CGRect
  70.         let offset = UIEdgeInsetsMake(0, 0, (keyboardSize?.height)!, 0)
  71.         scrollView.contentInset = offset
  72.     }
  73.    
  74.     override func onKeyboardEnd() {
  75.         scrollView.contentInset = UIEdgeInsets.zero
  76.     }
  77.    
  78.     // MARK: - Private methods
  79.    
  80.     private func configureCommonView() {
  81.         if isBackImageViewEnabled {
  82.             screenTitleLabel.isHidden = true
  83.             backStackView.isHidden = false
  84.            
  85.             let onBackTap = UITapGestureRecognizer(target: self, action: #selector(onBackTapped(_:)))
  86.             backImageView.isUserInteractionEnabled = true
  87.             backImageView.addGestureRecognizer(onBackTap)
  88.         }
  89.     }
  90.  
  91.     private func configureLoginTextField() {
  92.         loginTextField.label = "\(getString(with: "Login")):"
  93.         loginTextField.textField.returnKeyType = .next
  94.         loginTextField.delegate = self
  95.         loginTextField.length = Constants.LoginViewController.loginMaxLength
  96.     }
  97.    
  98.     private func configurePasswordTextField() {
  99.         passwordTextField.label = "\(getString(with: "Password")):"
  100.         passwordTextField.textField.returnKeyType = .done
  101.         passwordTextField.delegate = self
  102.         passwordTextField.isPassword = true
  103.         passwordTextField.addEndView(getShowPasswordView())
  104.         passwordTextField.length = Constants.LoginViewController.passwordMaxLength
  105.     }
  106.    
  107.     private func getShowPasswordView() -> UIView {
  108.         let onViewLongTap = UILongPressGestureRecognizer().apply {
  109.             $0.addTarget(self, action: #selector(showPassword(_:)))
  110.         }
  111.         return UIImageView(frame: CGRect(x: 0, y: 0, width: 32, height: 32)).apply({
  112.             $0.isUserInteractionEnabled = true
  113.             $0.addGestureRecognizer(onViewLongTap)
  114.             $0.image = #imageLiteral(resourceName: "password_field_eye")
  115.             $0.contentMode = .center
  116.         })
  117.     }
  118.    
  119.     private func configurePhoneLabel() {
  120.         phoneLabel.setLinkType(.phoneNumber)
  121.     }
  122.    
  123.     private func configureEmailLabel() {
  124.         mailLabel.setLinkType(.email, with: self)
  125.     }
  126.    
  127.     private func configureShowFAQImageView() {
  128.         let onTapGesture = UITapGestureRecognizer(target: self, action: #selector(showFAQ(_:)))
  129.         showFAQImageView.isUserInteractionEnabled = true
  130.         showFAQImageView.addGestureRecognizer(onTapGesture)
  131.     }
  132.    
  133.     private func configureNextButton() {
  134.         nextButton.setTitle(getString(with: "Next"), for: .normal)
  135.     }
  136.    
  137.     private func setupRecognizer() {
  138.         let onTapGesture = UITapGestureRecognizer(target: self, action: #selector(onHideKeyboardTap))
  139.         loginView.addGestureRecognizer(onTapGesture)
  140.         view.addGestureRecognizer(onTapGesture)
  141.     }
  142.    
  143.     private func showSetPINCodeScreen(for check: Bool = false) {
  144.         isActivated = check
  145.         performSegue(withIdentifier: "SetPINCodeSegue", sender: self)
  146.         progress?.suspendOperation()
  147.     }
  148.    
  149.     private func showSetCompanyNameScreen() {
  150.         let controller = storyboard?.instantiateViewController(withIdentifier: "SetCompanyNameViewController")
  151.         let navigationController = BaseNavigationController(rootViewController: controller!)
  152.         present(navigationController, animated: true, completion: nil)
  153.         progress?.suspendOperation()
  154.     }
  155.    
  156.     private func saveUser() {
  157.         let username = loginTextField.textField.text
  158.         guard let userId = username else {
  159.             return
  160.         }
  161.         DataManager.shared.addUser(userId)
  162.     }
  163.  
  164.     private func isPINEmpty() -> Bool {
  165.         let defaults = UserDefaults.standard
  166.         return defaults.string(forKey: Constants.UserDefaults.userPINKey)?.isEmpty ?? true
  167.     }
  168.    
  169.     private func showBlockedActivationDialog() {
  170.         showDialog(title: "Activation Blocked!", message: "Too many activation attempts. Please, try again in 3 hours.")
  171.     }
  172.    
  173.     private func isUserActivatedOnDevice(_ userId: String) -> Bool {
  174.         guard let result = DataManager.shared.getUsers() else {
  175.             return false
  176.         }
  177.         var isFounded = false
  178.         Array(result).forEach { user in
  179.             if user.userId == userId {
  180.                 isFounded = true
  181.                 return
  182.             }
  183.         }
  184.         return isFounded
  185.     }
  186.    
  187.     // MARK: - OBJC methods
  188.    
  189.     @objc func onHideKeyboardTap() {
  190.         view.endEditing(true)
  191.     }
  192.    
  193.     @objc func showPassword(_ recognizer: UILongPressGestureRecognizer) {
  194.         if recognizer.state == .began {
  195.             passwordTextField.isPassword = false
  196.         }
  197.        
  198.         if recognizer.state == .ended {
  199.             passwordTextField.isPassword = true
  200.         }
  201.     }
  202.    
  203.     @objc func showFAQ(_ recognizer: UITapGestureRecognizer ) {
  204.         onHideKeyboardTap()
  205.     }
  206.    
  207.     @objc func onBackTapped(_ recognizer: UITapGestureRecognizer) {
  208.         navigationController?.popViewController(animated: true)
  209.     }
  210.    
  211.     @IBAction func onNextTap() {
  212.         onHideKeyboardTap()
  213.        
  214.         if ActivationOverseerUtils.shared.isActivationBlocked() {
  215.             showBlockedActivationDialog()
  216.             return
  217.         }
  218.        
  219.         if !isConnectedToNetwork() {
  220.             showDialog(title: getString(with: "No Internet Connection"),
  221.                        message: getString(with: "Please, turn on Wi-Fi or Cellular Data"))
  222.             return
  223.         }
  224.        
  225.         guard let userId = loginTextField.textField.text else {
  226.             return
  227.         }
  228.        
  229.         guard let password = passwordTextField.textField.text else {
  230.             return
  231.         }
  232.        
  233.         if isUserActivatedOnDevice(userId) {
  234.             loginTextField.error = getString(with: "This user is activated")
  235.             return
  236.         }
  237.        
  238.         progress?.launchOperation {
  239.             OrchestrationManager.shared.login(username: userId, password: password, delegate: self)
  240.         }
  241.     }
  242. }
  243.  
  244. // MARK: - ExtendedTextFieldDelegate
  245.  
  246. extension LoginViewController: ExtendedTextFieldDelegate {
  247.    
  248.     func extendedTextFieldShouldReturn(_ sender: ExtendedTextField) {
  249.         switch sender {
  250.         case loginTextField:
  251.             passwordTextField.textField.becomeFirstResponder()
  252.         case passwordTextField:
  253.             passwordTextField.textField.resignFirstResponder()
  254.         default:
  255.             break
  256.         }
  257.     }
  258. }
  259.  
  260. // MARK: - ActivationDelegate
  261.  
  262. extension LoginViewController: ActivationDelegate {
  263.    
  264.     func activationDidCompleted(_ sender: ActivationUtils) {
  265.         saveUser()
  266.         if isPINEmpty() {
  267.             showSetPINCodeScreen()
  268.         } else {
  269.             showSetCompanyNameScreen()
  270.         }
  271.     }
  272.    
  273.     func activationDidAborted(_ sender: ActivationUtils) {
  274.         progress?.suspendOperation()
  275.     }
  276.    
  277.     func activation(_ sender: ActivationUtils, notCompletedWith error: String) {
  278.         switch error {
  279.         case LoginError.emptyPassword.rawValue:
  280.             passwordTextField.error = getString(with: "Password should not be empty")
  281.         case LoginError.invalidPassword.rawValue:
  282.             ActivationOverseerUtils.shared.addActivationAttempt()
  283.             passwordTextField.error = getString(with: "Invalid password")
  284.         case LoginError.emptyLogin.rawValue:
  285.             loginTextField.error = getString(with: "Login should not be empty")
  286.         case LoginError.worngPasswordLength.rawValue:
  287.             passwordTextField.error = getString(with: "Password has a wrong length")
  288.         default:
  289.             loginTextField.error = getString(with: "Unknown error")
  290.         }
  291.         progress?.suspendOperation()
  292.     }
  293. }
  294.  
  295. // MARK: - ActivationOverseerUtils
  296.  
  297. extension LoginViewController: ActivationOverseerUtilsDelegate {
  298.    
  299.     func onActivationBlock(_ sender: ActivationOverseerUtils) {
  300.         showBlockedActivationDialog()
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement