CodeVerificationViewController.swift 5.26 KB
//
//  CodeVerificationViewController.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 03.06.2022.
//

import UIKit

class CodeVerificationViewController: UIViewController {
    
    var otp: String = ""
    var isKeyboardAppear = false
    private let mainView = CodeVerificationView()
    
    override func loadView() {
        view = mainView
    }
    
    override func viewDidLoad() {
        initViewController()
    }
    
    private func initViewController() {
        mainView.textField.delegate = self
        mainView.backButton.addTarget(self, action: #selector(didDismissVC), for: .touchUpInside)
        mainView.verifyButton.addTarget(self, action: #selector(didVerifyCode), for: .touchUpInside)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
    }
}

extension CodeVerificationViewController {
    
    @objc func didDismissVC() {
        navigationController?.popViewController(animated: true)
    }
    
    @objc func didVerifyCode() {
        if mainView.fourthDigitLabel.text == nil {
            let alert = UIAlertController(title: "❌❌❌", message: "Some fields are empty, please fill in all", preferredStyle: .alert)
            let _: Void = alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
            present(alert, animated: true, completion: nil)
            
        } else {
        mainView.firstDigitLabel.backgroundColor = UIColor(red: 1, green: 0.929, blue: 0.941, alpha: 1)
        mainView.firstDigitLabel.textColor = UIColor(red: 1, green: 0, blue: 0.18, alpha: 1)
        
        mainView.secondDigitLabel.backgroundColor = UIColor(red: 1, green: 0.929, blue: 0.941, alpha: 1)
        mainView.secondDigitLabel.textColor = UIColor(red: 1, green: 0, blue: 0.18, alpha: 1)
        
        mainView.thirdDigitLabel.backgroundColor = UIColor(red: 1, green: 0.929, blue: 0.941, alpha: 1)
        mainView.thirdDigitLabel.textColor = UIColor(red: 1, green: 0, blue: 0.18, alpha: 1)
        
        mainView.fourthDigitLabel.backgroundColor = UIColor(red: 1, green: 0.929, blue: 0.941, alpha: 1)
        mainView.fourthDigitLabel.textColor = UIColor(red: 1, green: 0, blue: 0.18, alpha: 1)
        mainView.errorAlertLabel.isHidden = false
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                let vc = HomeViewController()
                vc.navigationItem.hidesBackButton = true
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
    
    
    @objc func keyboardWillShow(notification: NSNotification) {
        let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double

        if !isKeyboardAppear {
            
            UIView.animate(withDuration: duration ?? 0) {
                self.mainView.containerView.snp.updateConstraints { make in
                    make.top.equalTo(self.mainView.backButton.snp.bottom).offset(97)
                }
                self.mainView.shieldImage.snp.updateConstraints {
                    $0.right.equalTo(self.mainView.snp.right).offset(-6)
                }
                self.mainView.layoutIfNeeded()
            }
            
            isKeyboardAppear = true
        }
    }
    
    @objc func keyboardWillHide(notification: NSNotification) {
        let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double

        if isKeyboardAppear {
            
            UIView.animate(withDuration: duration ?? 0) {
                self.mainView.containerView.snp.updateConstraints { make in
                    make.top.equalTo(self.mainView.backButton.snp.bottom).offset(227)
                }
                self.mainView.shieldImage.snp.updateConstraints {
                    $0.right.equalTo(self.mainView.snp.right).offset(-101)
                }
                self.mainView.layoutIfNeeded()
            }
            
            isKeyboardAppear = false
        }
    }
    
}

extension CodeVerificationViewController: UITextFieldDelegate {
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        otp += string
        
        otp.enumerated().forEach { index,item in
            switch index {
            case 0:
                mainView.firstDigitLabel.text = String(item)
            case 1:
                mainView.secondDigitLabel.text = String(item)
            case 2:
                mainView.thirdDigitLabel.text = String(item)
            case 3:
                mainView.fourthDigitLabel.text = String(item)
                mainView.textField.endEditing(true)
            default:
                mainView.textField.endEditing(true)
            }
        }
        
        return true
    }
    
}