let nc = NotificationCenter.default
nc.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: nil, using: notify)
nc.addObserver(forName: .NSSystemClockDidChange, object: nil, queue: nil, using: notify)
- nc.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: nil, using: notify)
-
- notifyIfNeeded(nil)
+ NSWorkspace.shared.notificationCenter
+ .addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: nil, using: notify)
}
private func notify(_ notification: Notification) {
notifyIfNeeded(nil)
}
- @objc fileprivate func notifyIfNeeded(_ timer: Timer?) {
+ @objc private func notifyIfNeeded(_ timer: Timer?) {
let now = Date(timeIntervalSinceNow: 0.0)
- let unit: Set<Calendar.Component> = [.era, .year, .month, .day]
- var currentDay = Calendar.current.dateComponents(unit, from: now)
+ var currentDay = Calendar.current.dateComponents([.era, .year, .month, .day], from: now)
currentDay.hour = hour
currentDay.minute = minutes
NotificationCenter.default.post(name: .Periodic, object: self)
}
- if let v = timer?.isValid, v {
+ timer?.invalidate()
+
+ guard let nextNotifyDate = Calendar.current.date(from: currentDay) else {
- timer?.invalidate()
+ Logger.shared.log("Can not create time of notify")
+
+ return
}
- guard let nextNotifyDate = Calendar.current.date(from: currentDay) else { fatalError("Can not create time of notify") }
-
- let nextNotifyTime = nextNotifyDate.timeIntervalSinceNow + 0.1
- Timer.scheduledTimer(timeInterval: nextNotifyTime,
+ Timer.scheduledTimer(timeInterval: nextNotifyDate.timeIntervalSinceNow + 0.1,
target: self,
selector: #selector(PeriodicNotifier.notifyIfNeeded(_:)),
userInfo: nil,