VerificationViewController.swift 6.94 KB
//
//  ViewController.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 30.05.2022.
//

import UIKit

class VerificationViewController: UIViewController {
    
    var isKeyboardAppear = false
    private let mainView = VerificationView()
    
    lazy var phonePickerModels: [PhonePickerModel] = {
        var models: [PhonePickerModel] = []
        
        for (code, value) in PhoneService.phones
        {
            models.append(.init(code: code, countryCode: value.1, title: value.0, icon: value.2))
        }
        return models
    }()
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    override func loadView() {
        view = mainView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        initViewController()
    }
    
    
    private func initViewController() {
        mainView.phonePickerView.phoneNumberPicker.delegate = self
        mainView.phonePickerView.phoneNumberPicker.dataSource = self
        mainView.phoneNumberButton.addTarget(self, action: #selector(didCheckboxTapped(_:)), for: .touchUpInside)
        mainView.emailButton.addTarget(self, action: #selector(didCheckboxTapped(_:)), for: .touchUpInside)
        mainView.showPickerButton.addTarget(self, action: #selector(didshowPickerButtonTapped(_:)), for: .touchUpInside)
        mainView.phonePickerView.selectButton.addTarget(self, action: #selector(didshowPickerButtonTapped(_:)), for: .touchUpInside)
        mainView.phonePickerView.titleButton.addTarget(self, action: #selector(didshowPickerButtonTapped(_:)), for: .touchDragInside)
        mainView.continueButton.addTarget(self, action: #selector(didShowCodeVerificationVC), for: .touchUpInside)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
}

//MARK: helpers and handlers
extension VerificationViewController {
    
}

//MARK: targets
extension VerificationViewController {
    
    @objc private func didCheckboxTapped(_ sender: Checkbox) {
        switch sender {
        case mainView.emailButton :
            mainView.emailTextField.isHidden = false
            mainView.phoneTextField.isHidden = true
            mainView.phoneNumberButton.isSelected = false
            mainView.emailButton.isSelected = true
            mainView.verificationDescriptionLabel.text = "Verification code will be sent to your e-mail"
            mainView.enterChosenAuthenticatorLabel.text = "Enter your email address"
            mainView.emailTextField.text = ""
            mainView.emailTextField.placeholder = "Example: john.doe@gmail.com"
            mainView.showPickerButton.isHidden = true
        case mainView.phoneNumberButton:
            mainView.emailTextField.isHidden = true
            mainView.phoneTextField.isHidden = false
            mainView.phoneNumberButton.isSelected = true
            mainView.emailButton.isSelected = false
            mainView.phoneNumberButton.image(for: .normal)
            mainView.verificationDescriptionLabel.text = "Verification code will be sent to\nyour phone number"
            mainView.enterChosenAuthenticatorLabel.text = "Enter your phone number"
            mainView.emailTextField.text = "+1"
            mainView.showPickerButton.isHidden = false
        default :
            return
        }
    }
    
    @objc private func didshowPickerButtonTapped(_ sender: UIButton) {
        mainView.isShow ? mainView.showPickerView() : mainView.hidePickerView()
        mainView.isShow.toggle()
    }
    
    @objc private func didShowCodeVerificationVC() {
        let vc = CodeVerificationViewController()
        vc.navigationItem.hidesBackButton = true
        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.chooseLabel.snp.updateConstraints { make in
                    make.top.equalTo(self.mainView.shieldImage.snp.bottom).offset(0)
                }
                self.mainView.dividingLineView.snp.updateConstraints {
                    $0.top.equalTo(self.mainView.emailButton.snp.bottom).offset(10)
                }
                self.mainView.enterChosenAuthenticatorLabel.snp.updateConstraints {
                    $0.top.equalTo(self.mainView.dividingLineView.snp.bottom).offset(5)
                }
                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.chooseLabel.snp.updateConstraints { make in
                    make.top.equalTo(self.mainView.shieldImage.snp.bottom).offset(52)
                }
                self.mainView.dividingLineView.snp.updateConstraints {
                    $0.top.equalTo(self.mainView.emailButton.snp.bottom).offset(41)
                }
                self.mainView.enterChosenAuthenticatorLabel.snp.updateConstraints {
                    $0.top.equalTo(self.mainView.dividingLineView.snp.bottom).offset(39)
                }
                self.mainView.layoutIfNeeded()
            }
            isKeyboardAppear = false
        }
    }
}

extension VerificationViewController: UIPickerViewDataSource, UIPickerViewDelegate {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return phonePickerModels.count
        
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        60
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let model = phonePickerModels[row]
        return PickerView.create(icon: model.icon, title: model.title, code: model.countryCode)
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        pickerView.subviews[1].backgroundColor = .clear
        let model = phonePickerModels[row]
        mainView.phoneTextField.text = "\(model.countryCode)"
        mainView.flagPickerView.image = model.icon
    }
}