AddResidentsViewController.swift 5.49 KB
//
//  AddResidentViewController.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 14.06.2022.
//

import UIKit
import ContactsUI

class AddResidentsViewController: UIViewController, ViewDelegate {
    var myData: ResidentModel?
    
    var savedResidents: [ResidentModel] = []
    
    weak var delegate: ResidentSavable?
    
    var contact = ContactPickerViewController()
    
    var isKeyboardAppear = false
    
    var mainView = AddResidentsView()
    
    override func loadView() {
        view = mainView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initViewController()
    }
    
    func initViewController() {
        mainView.closeButton.addTarget(self, action: #selector(didTapCloseButton), for: .touchUpInside)
        mainView.managerButton.addTarget(self, action: #selector(didTapOnChangeableButtons(_:)), for: .touchUpInside)
        mainView.hiddenButton.addTarget(self, action: #selector(didTapOnChangeableButtons(_:)), for: .touchUpInside)
        mainView.heartButton.addTarget(self, action: #selector(didTapOnChangeableButtons(_:)), for: .touchUpInside)
        mainView.addResidentButton.addTarget(self, action: #selector(didTapResidentButton), for: .touchUpInside)
        mainView.contactListButton.addTarget(self, action: #selector(didTapContactListButton), for: .touchUpInside)
        
        mainView.delegate = self
        gestureSetup(mainView.transparentView)
        
        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)
    }
    
    @objc func viewDidTapImage() {
        print("🕵️‍♂️")
    }
    
    func gestureSetup(_ view: UIView) {
        let tapGesture = UIGestureRecognizer()
        tapGesture.addTarget(self, action: #selector(gestureFired))
        view.addGestureRecognizer(tapGesture)
        view.isUserInteractionEnabled = true
    }
    
    @objc private func gestureFired() {
        print("❌")
        
    }
    
    func reloadView() {
        mainView.mobileNumberTextField.reloadInputViews()
    }
    
    @objc private func didTapCloseButton() {
        dismiss(animated: true, completion: nil)
    }
    
    @objc private func didTapResidentButton() {
        print("✅")
        didReceiveData()
        dismiss(animated: true, completion: nil)
    }
    
    func didReceiveData() {
        mainView.nameTextField.text = myData?.name
        mainView.mobileNumberTextField.text = myData?.mobileNumber
        mainView.managerButton.isSelected = ((myData?.manager) != nil)
        mainView.hiddenButton.isSelected = ((myData?.hidden) != nil)
        mainView.heartButton.isSelected = ((myData?.hidden) != nil)
        
        delegate?.save(myData!)
    }
    
    @objc private func didTapOnChangeableButtons(_ sender: SquereCheckbox) {
        switch sender {
        case mainView.managerButton :
            if mainView.managerButton.isSelected == true {
                mainView.managerButton.isSelected = false
            } else {
                mainView.managerButton.isSelected = true
            }
        case mainView.hiddenButton :
            if mainView.hiddenButton.isSelected == true {
                mainView.hiddenButton.isSelected = false
            } else {
                mainView.hiddenButton.isSelected = true
            }
        case mainView.heartButton :
            if mainView.heartButton.isSelected == true {
                mainView.heartButton.isSelected = false
            } else {
                mainView.heartButton.isSelected = true
            }
        default:
            return
        }
    }
    
    @objc func didTapContactListButton() {
        presentContactPickerVC()
    }
    
    func presentContactPickerVC() {
        let viewController = ContactPickerViewController()
        present(viewController, animated: true)
    }
}

//MARK: - Keyboard observer
extension AddResidentsViewController {
    @objc private 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.centerY.equalTo(self.mainView.transparentView.snp.centerY).offset(-150)
                }
                self.mainView.layoutIfNeeded()
            }
            isKeyboardAppear = true
        }
    }
    
    @objc private 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.centerY.equalTo(self.mainView.transparentView.snp.centerY)
                }
                self.mainView.layoutIfNeeded()
            }
            isKeyboardAppear = false
        }
    }
}