VerificationViewController.swift
6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// 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
}
}