forked from DominikBucher12/SwiftPasscodeLock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasscodeLockPresenter.swift
More file actions
108 lines (74 loc) · 3.23 KB
/
Copy pathPasscodeLockPresenter.swift
File metadata and controls
108 lines (74 loc) · 3.23 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
//
// PasscodeLockPresenter.swift
// PasscodeLock
//
// Created by Yanko Dimitrov on 8/29/15.
// Copyright © 2015 Yanko Dimitrov. All rights reserved.
//
import UIKit
open class PasscodeLockPresenter {
fileprivate var mainWindow: UIWindow?
fileprivate lazy var passcodeLockWindow: UIWindow = {
let window = UIWindow(frame: UIScreen.main.bounds)
window.windowLevel = 0
window.makeKeyAndVisible()
return window
}()
fileprivate let passcodeConfiguration: PasscodeLockConfigurationType
open var isPasscodePresented = false
open let passcodeLockVC: PasscodeLockViewController
public init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType, viewController: PasscodeLockViewController) {
mainWindow = window
mainWindow?.windowLevel = 1
passcodeConfiguration = configuration
passcodeLockVC = viewController
}
public convenience init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType) {
let passcodeLockVC = PasscodeLockViewController(state: .enterPasscode, configuration: configuration)
self.init(mainWindow: window, configuration: configuration, viewController: passcodeLockVC)
}
open func presentPasscodeLock() {
guard passcodeConfiguration.repository.hasPasscode else { return }
guard !isPasscodePresented else { return }
isPasscodePresented = true
passcodeLockWindow.windowLevel = 2
passcodeLockWindow.isHidden = false
mainWindow?.windowLevel = 1
mainWindow?.endEditing(true)
let passcodeLockVC = PasscodeLockViewController(state: .enterPasscode, configuration: passcodeConfiguration)
let userDismissCompletionCallback = passcodeLockVC.dismissCompletionCallback
passcodeLockVC.dismissCompletionCallback = { [weak self] in
userDismissCompletionCallback?()
self?.dismissPasscodeLock()
}
passcodeLockWindow.rootViewController = passcodeLockVC
}
open func dismissPasscodeLock(animated: Bool = true) {
isPasscodePresented = false
mainWindow?.windowLevel = 1
mainWindow?.makeKeyAndVisible()
if animated {
animatePasscodeLockDismissal()
} else {
passcodeLockWindow.windowLevel = 0
passcodeLockWindow.rootViewController = nil
}
}
internal func animatePasscodeLockDismissal() {
UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: UIViewAnimationOptions(),
animations: { [weak self] in
self?.passcodeLockWindow.alpha = 0
},
completion: { [weak self] _ in
self?.passcodeLockWindow.windowLevel = 0
self?.passcodeLockWindow.rootViewController = nil
self?.passcodeLockWindow.alpha = 1
}
)
}
}