forked from yankodimitrov/SwiftPasscodeLock
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathPasscodeLockViewController.swift
More file actions
281 lines (189 loc) · 8.88 KB
/
PasscodeLockViewController.swift
File metadata and controls
281 lines (189 loc) · 8.88 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
public class PasscodeLockViewController: UIViewController, PasscodeLockTypeDelegate {
public enum LockState {
case EnterPasscode(allowCancellation: Bool)
case SetPasscode
case ChangePasscode
case RemovePasscode
func getState() -> PasscodeLockStateType {
switch self {
case .EnterPasscode(let allowCancellation): return EnterPasscodeState(allowCancellation: allowCancellation)
case .SetPasscode: return SetPasscodeState()
case .ChangePasscode: return ChangePasscodeState()
case .RemovePasscode: return EnterPasscodeState(allowCancellation: true)
}
}
}
@IBOutlet public weak var titleLabel: UILabel?
@IBOutlet public weak var descriptionLabel: UILabel?
@IBOutlet public var placeholders: [PasscodeSignPlaceholderView] = [PasscodeSignPlaceholderView]()
@IBOutlet public weak var cancelButton: UIButton?
@IBOutlet public weak var deleteSignButton: UIButton?
@IBOutlet public weak var touchIDButton: UIButton?
@IBOutlet public weak var placeholdersX: NSLayoutConstraint?
public var successCallback: ((lock: PasscodeLockType) -> Void)?
public var dismissCompletionCallback: (()->Void)?
public var animateOnDismiss: Bool
public var notificationCenter: NSNotificationCenter?
internal let passcodeConfiguration: PasscodeLockConfigurationType
internal let passcodeLock: PasscodeLockType
internal var isPlaceholdersAnimationCompleted = true
private var shouldTryToAuthenticateWithBiometrics = true
// MARK: - Initializers
public init(state: PasscodeLockStateType, configuration: PasscodeLockConfigurationType, animateOnDismiss: Bool = true, nibName: String = "PasscodeLockView", bundle: NSBundle? = 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 = NSNotificationCenter.defaultCenter()
}
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
public override func viewDidLoad() {
super.viewDidLoad()
deleteSignButton?.enabled = false
setupEvents()
}
public override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
updatePasscodeView()
}
public override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if shouldTryToAuthenticateWithBiometrics && passcodeConfiguration.shouldRequestTouchIDImmediately {
authenticateWithBiometrics()
}
}
internal func updatePasscodeView() {
titleLabel?.text = passcodeLock.state.title
descriptionLabel?.text = passcodeLock.state.description
cancelButton?.hidden = !passcodeLock.state.isCancellableAction
touchIDButton?.hidden = !passcodeLock.isTouchIDAllowed
}
// MARK: - Events
private func setupEvents() {
notificationCenter?.addObserver(self, selector: "appWillEnterForegroundHandler:", name: UIApplicationWillEnterForegroundNotification, object: nil)
notificationCenter?.addObserver(self, selector: "appDidEnterBackgroundHandler:", name: UIApplicationDidEnterBackgroundNotification, object: nil)
}
private func clearEvents() {
notificationCenter?.removeObserver(self, name: UIApplicationWillEnterForegroundNotification, object: nil)
notificationCenter?.removeObserver(self, name: UIApplicationDidEnterBackgroundNotification, object: nil)
}
public func appWillEnterForegroundHandler(notification: NSNotification) {
if passcodeConfiguration.shouldRequestTouchIDImmediately {
authenticateWithBiometrics()
}
}
public func appDidEnterBackgroundHandler(notification: NSNotification) {
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 touchIDButtonTap(sender: UIButton) {
passcodeLock.authenticateWithBiometrics()
}
public func authenticateWithBiometrics() {
guard passcodeConfiguration.repository.hasPasscode else { return }
if passcodeLock.isTouchIDAllowed {
passcodeLock.authenticateWithBiometrics()
}
}
internal func dismissPasscodeLock(lock: PasscodeLockType, completionHandler: (() -> Void)? = nil) {
// if presented as modal
if presentingViewController?.presentedViewController == self {
dismissViewControllerAnimated(animateOnDismiss, completion: { [weak self] _ in
self?.dismissCompletionCallback?()
completionHandler?()
})
return
// if pushed in a navigation controller
} else if navigationController != nil {
navigationController?.popViewControllerAnimated(animateOnDismiss)
}
dismissCompletionCallback?()
completionHandler?()
}
// MARK: - Animations
internal func animateWrongPassword() {
deleteSignButton?.enabled = false
isPlaceholdersAnimationCompleted = false
animatePlaceholders(placeholders, toState: .Error)
placeholdersX?.constant = -40
view.layoutIfNeeded()
UIView.animateWithDuration(
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)
}
}
private func animatePlacehodlerAtIndex(index: Int, toState state: PasscodeSignPlaceholderView.State) {
guard index < placeholders.count && index >= 0 else { return }
placeholders[index].animateState(state)
}
// MARK: - PasscodeLockDelegate
public func passcodeLockDidSucceed(lock: PasscodeLockType) {
deleteSignButton?.enabled = true
animatePlaceholders(placeholders, toState: .Inactive)
dismissPasscodeLock(lock, completionHandler: { [weak self] _ in
self?.successCallback?(lock: lock)
})
}
public func passcodeLockDidFail(lock: PasscodeLockType) {
animateWrongPassword()
}
public func passcodeLockDidChangeState(lock: PasscodeLockType) {
updatePasscodeView()
animatePlaceholders(placeholders, toState: .Inactive)
deleteSignButton?.enabled = false
}
public func passcodeLock(lock: PasscodeLockType, addedSignAtIndex index: Int) {
animatePlacehodlerAtIndex(index, toState: .Active)
deleteSignButton?.enabled = true
}
public func passcodeLock(lock: PasscodeLockType, removedSignAtIndex index: Int) {
animatePlacehodlerAtIndex(index, toState: .Inactive)
if index == 0 {
deleteSignButton?.enabled = false
}
}
}