forked from velikanov/SwiftPasscodeLock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasscodeLockViewController.swift
More file actions
281 lines (189 loc) · 8.91 KB
/
PasscodeLockViewController.swift
File metadata and controls
281 lines (189 loc) · 8.91 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// PasscodeLockViewController.swift
// PasscodeLock
//
// Created by Yanko Dimitrov on 8/28/15.
// Copyright © 2015 Yanko Dimitrov. All rights reserved.
//
import UIKit
open class PasscodeLockViewController: UIViewController, PasscodeLockTypeDelegate {
public enum LockState {
case enterPasscode
case setPasscode
case changePasscode
case removePasscode
func getState() -> PasscodeLockStateType {
switch self {
case .enterPasscode: return EnterPasscodeState()
case .setPasscode: return SetPasscodeState()
case .changePasscode: return ChangePasscodeState()
case .removePasscode: return EnterPasscodeState()
}
}
}
@IBOutlet open weak var titleLabel: UILabel?
@IBOutlet open weak var descriptionLabel: UILabel?
@IBOutlet open var placeholders: [PasscodeSignPlaceholderView] = [PasscodeSignPlaceholderView]()
@IBOutlet open weak var cancelButton: UIButton?
@IBOutlet open weak var deleteSignButton: UIButton?
@IBOutlet open weak var biometricAuthButton: UIButton!
@IBOutlet open weak var placeholdersX: NSLayoutConstraint?
open var successCallback: ((_ lock: PasscodeLockType) -> Void)?
open var dismissCompletionCallback: (()->Void)?
open var animateOnDismiss: Bool
open var notificationCenter: NotificationCenter?
internal let passcodeConfiguration: PasscodeLockConfigurationType
internal var passcodeLock: PasscodeLockType
internal var isPlaceholdersAnimationCompleted = true
fileprivate var shouldTryToAuthenticateWithBiometrics = true
// MARK: - Initializers
public init(state: PasscodeLockStateType, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true, nibName: String = "PasscodeLockView", bundle: Bundle? = nil) {
self.animateOnDismiss = animateOnDismiss
passcodeConfiguration = configuration
passcodeLock = PasscodeLock(state: state, configuration: configuration)
let bundleToUse = bundle ?? bundleForResource(nibName, ofType: "nib")
super.init(nibName: nibName, bundle: bundleToUse)
passcodeLock.delegate = self
notificationCenter = NotificationCenter.default
}
public convenience init(state: LockState, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true) {
self.init(state: state.getState(), configuration: configuration, animateOnDismiss: animateOnDismiss)
}
public required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
clearEvents()
}
// MARK: - View
open override func viewDidLoad() {
super.viewDidLoad()
deleteSignButton?.isEnabled = false
setupEvents()
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updatePasscodeView()
}
open override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if shouldTryToAuthenticateWithBiometrics && passcodeConfiguration.shouldRequestBiometricAuthImmediately {
authenticateWithBiometrics()
}
}
internal func updatePasscodeView() {
titleLabel?.text = passcodeLock.state.title
descriptionLabel?.text = passcodeLock.state.description
cancelButton?.isHidden = !passcodeConfiguration.allowsCancellation
biometricAuthButton?.isHidden = !passcodeLock.isBiometricAuthAllowed
}
// MARK: - Events
fileprivate func setupEvents() {
notificationCenter?.addObserver(self, selector: #selector(PasscodeLockViewController.appWillEnterForegroundHandler(_:)), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
notificationCenter?.addObserver(self, selector: #selector(PasscodeLockViewController.appDidEnterBackgroundHandler(_:)), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}
fileprivate func clearEvents() {
notificationCenter?.removeObserver(self, name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
notificationCenter?.removeObserver(self, name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}
@objc open func appWillEnterForegroundHandler(_ notification: Notification) {
if passcodeConfiguration.shouldRequestBiometricAuthImmediately {
authenticateWithBiometrics()
}
}
@objc open func appDidEnterBackgroundHandler(_ notification: Notification) {
shouldTryToAuthenticateWithBiometrics = false
}
// MARK: - Actions
@IBAction func passcodeSignButtonTap(_ sender: PasscodeSignButton) {
guard isPlaceholdersAnimationCompleted else { return }
passcodeLock.addSign(sender.passcodeSign)
}
@IBAction func cancelButtonTap(_ sender: UIButton) {
dismissPasscodeLock(passcodeLock)
}
@IBAction func deleteSignButtonTap(_ sender: UIButton) {
passcodeLock.removeSign()
}
@IBAction func biometricAuthButtonTap(_ sender: UIButton) {
passcodeLock.authenticateWithBiometrics()
}
open func authenticateWithBiometrics() {
guard passcodeConfiguration.repository.hasPasscode else { return }
if passcodeLock.isBiometricAuthAllowed {
passcodeLock.authenticateWithBiometrics()
}
}
internal func dismissPasscodeLock(_ lock: PasscodeLockType, completionHandler: (() -> Void)? = nil) {
// if presented as modal
if presentingViewController?.presentedViewController == self {
dismiss(animated: animateOnDismiss, completion: { [weak self] in
self?.dismissCompletionCallback?()
completionHandler?()
})
return
// if pushed in a navigation controller
} else if navigationController != nil {
navigationController?.popViewController(animated: animateOnDismiss)
}
dismissCompletionCallback?()
completionHandler?()
}
// MARK: - Animations
internal func animateWrongPassword() {
deleteSignButton?.isEnabled = false
isPlaceholdersAnimationCompleted = false
animatePlaceholders(placeholders, toState: .error)
placeholdersX?.constant = -40
view.layoutIfNeeded()
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 0.2,
initialSpringVelocity: 0,
options: [],
animations: {
self.placeholdersX?.constant = 0
self.view.layoutIfNeeded()
},
completion: { completed in
self.isPlaceholdersAnimationCompleted = true
self.animatePlaceholders(self.placeholders, toState: .inactive)
})
}
internal func animatePlaceholders(_ placeholders: [PasscodeSignPlaceholderView], toState state: PasscodeSignPlaceholderView.State) {
for placeholder in placeholders {
placeholder.animateState(state)
}
}
fileprivate func animatePlacehodlerAtIndex(_ index: Int, toState state: PasscodeSignPlaceholderView.State) {
guard index < placeholders.count && index >= 0 else { return }
placeholders[index].animateState(state)
}
// MARK: - PasscodeLockDelegate
open func passcodeLockDidSucceed(_ lock: PasscodeLockType) {
deleteSignButton?.isEnabled = true
animatePlaceholders(placeholders, toState: .inactive)
dismissPasscodeLock(lock, completionHandler: { [weak self] in
self?.successCallback?(lock)
})
}
open func passcodeLockDidFail(_ lock: PasscodeLockType) {
animateWrongPassword()
}
open func passcodeLockDidChangeState(_ lock: PasscodeLockType) {
updatePasscodeView()
animatePlaceholders(placeholders, toState: .inactive)
deleteSignButton?.isEnabled = false
}
open func passcodeLock(_ lock: PasscodeLockType, addedSignAtIndex index: Int) {
animatePlacehodlerAtIndex(index, toState: .active)
deleteSignButton?.isEnabled = true
}
open func passcodeLock(_ lock: PasscodeLockType, removedSignAtIndex index: Int) {
animatePlacehodlerAtIndex(index, toState: .inactive)
if index == 0 {
deleteSignButton?.isEnabled = false
}
}
}