This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
AutoLockSupport.swift
64 lines (52 loc) · 2.16 KB
/
AutoLockSupport.swift
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import RxSwift
/// Return the CLOCK_MONOTONIC_RAW clock as a TimeInterval. If for some reason the time could not be read, zero is returned.
func getRawMonotonicClock() -> TimeInterval {
var uptime = timespec()
if clock_gettime(CLOCK_MONOTONIC_RAW, &uptime) != 0 {
return 0
}
return TimeInterval(uptime.tv_sec)
}
class AutoLockSupport {
static let shared = AutoLockSupport()
private let userDefaultStore: UserDefaultStore
private let userDefaults: UserDefaults
private let disposeBag = DisposeBag()
var lockCurrentlyRequired: Bool {
let autoLockTimerDate = userDefaults.double(forKey: UserDefaultKey.autoLockTimerDate.rawValue)
let currentSystemTime = getRawMonotonicClock()
return autoLockTimerDate <= currentSystemTime
}
init(userDefaultStore: UserDefaultStore = UserDefaultStore.shared,
userDefaults: UserDefaults = UserDefaults(suiteName: Constant.app.group) ?? .standard) {
self.userDefaultStore = userDefaultStore
self.userDefaults = userDefaults
}
func storeNextAutolockTime() {
userDefaultStore.autoLockTime
.take(1)
.subscribe(onNext: { [weak self] autoLockSetting in self?.updateNextLockTime(autoLockSetting)
})
.disposed(by: self.disposeBag)
}
func backDateNextLockTime() {
storeAutoLockTimerDate(dateTime: 0)
}
func forwardDateNextLockTime() {
storeAutoLockTimerDate(dateTime: Double.greatestFiniteMagnitude)
}
private func updateNextLockTime(_ autoLockTime: Setting.AutoLock) {
if (autoLockTime == Setting.AutoLock.Never) {
forwardDateNextLockTime()
} else {
storeAutoLockTimerDate(dateTime: getRawMonotonicClock() + Double(autoLockTime.seconds))
}
}
private func storeAutoLockTimerDate(dateTime: Double) {
userDefaults.set(dateTime, forKey: UserDefaultKey.autoLockTimerDate.rawValue)
}
}