CodeVerificationViewController.swift
5.79 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
//
// 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()
private let networkManager = AuthNetworkManager()
override func loadView() {
view = mainView
}
override func viewDidLoad() {
initViewController()
}
private func initViewController() {
mainView.otpView.delegate = self
mainView.backButton.addTarget(self, action: #selector(didDismissVC), for: .touchUpInside)
mainView.verifyButton.addTarget(self, action: #selector(didTapVerifyButton), 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)
}
func networkingVerify() {
guard let verifyUrl = URL(string: Constants.APIRoutes.verifyURL.urlString) else { return }
let verifyModel = verifyModelFilling()
networkManager.verifyRequest(verifyUrl, model: verifyModel!) { response in
print("3️⃣✅\(response.data)")
print("\(response.message)✅")
self.mainView.spinner.startAnimating()
} fail: { error in
print("3️⃣\(error)")
}
}
func networkingLogin() {
guard let loginUrl = URL(string: Constants.APIRoutes.loginURL.urlString) else { return }
guard let loginModel = loginModelFilling() else { return }
self.networkManager.loginRequest(loginUrl, model: loginModel) { response in
guard let message = response.message else { return }
print("4️⃣✅\(message)✅")
} fail: { error in
print("4️⃣⛔️\(error)⛔️")
}
}
func verifyModelFilling() -> VerifyRequestModel? {
let email = "test@interqr.com"
guard let code = mainView.otpView.code else { return nil }
return VerifyRequestModel(emailOrNumber: email, code: code, secondAuthToken: "asdas")
}
func loginModelFilling() -> LoginRequestModel? {
guard let deviceUUID = UIDevice.current.identifierForVendor?.uuidString else {
return nil
}
return LoginRequestModel(deviceUUID: deviceUUID)
}
}
//MARK: - Targets
extension CodeVerificationViewController {
@objc private func didTapVerifyButton() {
errorProcessing()
networkingVerify()
}
private func showHomeVC() {
let viewController = HomeViewController()
navigationController?.pushViewController(viewController, animated: true)
}
private func errorProcessing() {
guard let code = mainView.otpView.code else { return }
if code.count < 4 {
let alert = UIAlertController(title: "❌", message: "Enter all fields", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
} else {
if code != "8327" {
mainView.otpView.isError.toggle()
mainView.errorAlertLabel.isHidden = false
mainView.otpView.textField.becomeFirstResponder()
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.networkingLogin()
self.mainView.spinner.stopAnimating()
self.showHomeVC()
}
}
}
}
@objc private func didDismissVC() {
navigationController?.popViewController(animated: true)
}
@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.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 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.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
}
}
}
//MARK: - OTPView delegate
extension CodeVerificationViewController: OTPViewDelegate {
func didFullCodeCompletion(_ otpView: OTPView) {
print("code -> \(String(describing: otpView.code))")
}
}