OSDN Git Service

不要となっていたプロパティを削除
[kcd/KCD.git] / KCD / PeriodicNotifier.swift
index 4d89add..6a55939 100644 (file)
@@ -28,9 +28,8 @@ final class PeriodicNotifier: NSObject {
         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) {
@@ -38,11 +37,10 @@ final class PeriodicNotifier: NSObject {
         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
         
@@ -53,15 +51,16 @@ final class PeriodicNotifier: NSObject {
             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,