-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPasscodeKitChange.swift
More file actions
executable file
·204 lines (166 loc) · 5.97 KB
/
PasscodeKitChange.swift
File metadata and controls
executable file
·204 lines (166 loc) · 5.97 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// Copyright (c) 2023 Related Code - https://relatedcode.com
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
// MARK: - PasscodeState
private enum PasscodeState {
case change1
case change2
case change3
case complete
}
// MARK: - PasscodeKitChange
class PasscodeKitChange: UIViewController {
private var state: PasscodeState!
private var passcode = ""
private var failedAttempts = 0
private var isPasscodeMismatch = false
private var viewPasscode = UIView()
private var textPasscode: PasscodeKitText!
private var labelInfo = UILabel()
private var labelPasscodeMismatch = UILabel()
private var labelFailedAttempts = UILabel()
var delegate: PasscodeKitDelegate?
override func viewDidLoad() {
super.viewDidLoad()
title = PasscodeKit.titleChangePasscode
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(actionCancel))
setupUI()
updateUI()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textPasscode.becomeFirstResponder()
}
@objc private func actionCancel() {
dismiss(animated: true)
}
}
// MARK: -
extension PasscodeKitChange {
private func setupUI() {
state = .change1
view.backgroundColor = PasscodeKit.backgroundColor
viewPasscode.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 120)
view.addSubview(viewPasscode)
labelInfo.textAlignment = .center
labelInfo.textColor = PasscodeKit.textColor
labelInfo.font = UIFont.systemFont(ofSize: 17)
labelInfo.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 30)
viewPasscode.addSubview(labelInfo)
if (textPasscode != nil) && (textPasscode.superview != nil) {
textPasscode.removeFromSuperview()
}
textPasscode = PasscodeKitText()
textPasscode.frame = CGRect(x: 0, y: 45, width: UIScreen.main.bounds.width, height: 30)
textPasscode.addTarget(self, action: #selector(textFieldDidChangeEditing(_:)), for: .editingChanged)
viewPasscode.addSubview(textPasscode)
labelPasscodeMismatch.text = PasscodeKit.textPasscodeMismatch
labelPasscodeMismatch.textAlignment = .center
labelPasscodeMismatch.textColor = PasscodeKit.textColor
labelPasscodeMismatch.font = UIFont.systemFont(ofSize: 15)
labelPasscodeMismatch.frame = CGRect(x: 0, y: 90, width: UIScreen.main.bounds.width, height: 30)
labelPasscodeMismatch.isHidden = true
viewPasscode.addSubview(labelPasscodeMismatch)
labelFailedAttempts.textAlignment = .center
labelFailedAttempts.textColor = PasscodeKit.failedTextColor
labelFailedAttempts.backgroundColor = PasscodeKit.failedBackgroundColor
labelFailedAttempts.font = UIFont.systemFont(ofSize: 15)
labelFailedAttempts.frame = CGRect(x: (UIScreen.main.bounds.width - 225) / 2, y: 90, width: 225, height: 30)
labelFailedAttempts.layer.cornerRadius = 15
labelFailedAttempts.isHidden = true
labelFailedAttempts.clipsToBounds = true
viewPasscode.addSubview(labelFailedAttempts)
}
private func updateUI() {
if (state == .change1) { labelInfo.text = PasscodeKit.textEnterOldPasscode }
if (state == .change2) { labelInfo.text = PasscodeKit.textEnterNewPasscode }
if (state == .change3) { labelInfo.text = PasscodeKit.textVerifyNewPasscode }
if (state == .change3) {
isPasscodeMismatch = false
}
failedAttempts = 0
textPasscode.text = ""
textPasscode.becomeFirstResponder()
labelPasscodeMismatch.isHidden = !isPasscodeMismatch
labelFailedAttempts.isHidden = true
animateViewPasscode()
}
private func animateViewPasscode() {
let originalXPos = viewPasscode.frame.origin.x
viewPasscode.frame.origin.x = originalXPos + (isPasscodeMismatch ? -250 : 250)
UIView.animate(withDuration: 0.15) {
self.viewPasscode.frame.origin.x = originalXPos
}
}
private func setupUIFailed() {
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.09
animation.repeatCount = 2
animation.isRemovedOnCompletion = true
animation.autoreverses = true
animation.fromValue = CGPoint(x: textPasscode.center.x - 10, y: textPasscode.center.y)
animation.toValue = CGPoint(x: textPasscode.center.x + 10, y: textPasscode.center.y)
textPasscode.layer.add(animation, forKey: "position")
failedAttempts += 1
labelFailedAttempts.isHidden = false
labelFailedAttempts.text = String(format: PasscodeKit.textFailedPasscode, failedAttempts)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
self.textPasscode.text = ""
}
if (failedAttempts >= PasscodeKit.allowedFailedAttempts) {
delegate?.passcodeMaximumFailedAttempts?()
}
}
}
// MARK: -
extension PasscodeKitChange {
@objc private func textFieldDidChangeEditing(_ textField: UITextField) {
let current = textField.text ?? ""
if (current.count >= PasscodeKit.passcodeLength) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.actionPasscode(current)
}
}
}
private func actionPasscode(_ current: String) {
if (state == .change1) {
actionVerify(current)
} else if (state == .change2) {
actionChange(current)
} else if (state == .change3) {
actionConfirm(current)
}
}
private func actionVerify(_ current: String) {
if (PasscodeKit.verify(current)) {
state = .change2
updateUI()
} else {
setupUIFailed()
}
}
private func actionChange(_ current: String) {
state = .change3
passcode = current
updateUI()
}
private func actionConfirm(_ current: String) {
if (passcode == current) {
PasscodeKit.update(passcode)
delegate?.passcodeChanged?(passcode)
dismiss(animated: true)
} else {
isPasscodeMismatch = true
state = .change2
updateUI()
}
}
}