-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPasscodeKitRemove.swift
More file actions
executable file
·136 lines (109 loc) · 4.17 KB
/
PasscodeKitRemove.swift
File metadata and controls
executable file
·136 lines (109 loc) · 4.17 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
//
// 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: - PasscodeKitRemove
class PasscodeKitRemove: UIViewController {
private var failedAttempts = 0
private var viewPasscode = UIView()
private var textPasscode: PasscodeKitText!
private var labelInfo = UILabel()
private var labelFailedAttempts = UILabel()
var delegate: PasscodeKitDelegate?
override func viewDidLoad() {
super.viewDidLoad()
title = PasscodeKit.titleRemovePasscode
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 PasscodeKitRemove {
private func setupUI() {
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)
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() {
labelInfo.text = PasscodeKit.textEnterPasscode
failedAttempts = 0
textPasscode.text = ""
textPasscode.becomeFirstResponder()
labelFailedAttempts.isHidden = true
}
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 PasscodeKitRemove {
@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 (PasscodeKit.verify(current)) {
PasscodeKit.remove()
delegate?.passcodeRemoved?()
dismiss(animated: true)
} else {
setupUIFailed()
}
}
}