forked from zahlz/SwiftPasscodeLock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasscodeLockPresenter.swift
More file actions
99 lines (69 loc) · 2.98 KB
/
Copy pathPasscodeLockPresenter.swift
File metadata and controls
99 lines (69 loc) · 2.98 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
//
// 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(frame: UIScreen.main.bounds)
fileprivate let passcodeConfiguration: PasscodeLockConfigurationType
open var isPasscodePresented = false
open let passcodeLockVC: PasscodeLockViewController
public init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType, viewController: PasscodeLockViewController) {
mainWindow = window
passcodeConfiguration = configuration
passcodeLockVC = viewController
}
public convenience init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType) {
let passcodeLockVC = PasscodeLockViewController(state: .enter, configuration: configuration)
self.init(mainWindow: window, configuration: configuration, viewController: passcodeLockVC)
}
open func present() {
guard passcodeConfiguration.repository.hasPasscode else { return }
guard !isPasscodePresented else { return }
isPasscodePresented = true
mainWindow?.endEditing(true)
moveWindowsToFront()
passcodeLockWindow.isHidden = false
let userDismissCompletionCallback = passcodeLockVC.dismissCompletionCallback
passcodeLockVC.dismissCompletionCallback = { [weak self] in
userDismissCompletionCallback?()
self?.dismiss()
}
passcodeLockWindow.rootViewController = passcodeLockVC
}
open func dismiss(animated: Bool = true) {
isPasscodePresented = false
if animated {
animatePasscodeLockDismissal()
} else {
passcodeLockWindow.isHidden = true
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.isHidden = true
self?.passcodeLockWindow.rootViewController = nil
self?.passcodeLockWindow.alpha = 1
}
)
}
fileprivate func moveWindowsToFront() {
let windowLevel = UIApplication.shared.windows.last?.windowLevel ?? UIWindowLevelNormal
let maxWinLevel = max(windowLevel, UIWindowLevelNormal)
passcodeLockWindow.windowLevel = maxWinLevel + 1
}
}