OSDN Git Service

Localizableを使ってローカライズするように変更
[kcd/KCD.git] / KCD / TimeSignalNotifier.swift
index 16b15c2..7ffbcd0 100644 (file)
@@ -9,32 +9,41 @@
 import Cocoa
 
 extension Selector {
+    
     static let fire = #selector(TimeSignalNotifier.fire(_:))
 }
 
-class TimeSignalNotifier: NSObject {
-    let udController: NSUserDefaultsController = NSUserDefaultsController.shared()
+final class TimeSignalNotifier: NSObject {
+    
+    let udController: NSUserDefaultsController = NSUserDefaultsController.shared
     
     override init() {
+        
         super.init()
+        
         registerTimer()
-        self.bind(#keyPath(notifyTimeBeforeTimeSignal),
-                  to: udController,
-                  withKeyPath: "values.notifyTimeBeforeTimeSignal")
+        bind(NSBindingName(#keyPath(notifyTimeBeforeTimeSignal)),
+             to: udController,
+             withKeyPath: "values.notifyTimeBeforeTimeSignal")
     }
+    
     deinit {
-        self.unbind(#keyPath(notifyTimeBeforeTimeSignal))
+        
+        unbind(NSBindingName(#keyPath(notifyTimeBeforeTimeSignal)))
     }
     
-    dynamic var notifyTimeBeforeTimeSignal: Int = 0 {
+    @objc dynamic var notifyTimeBeforeTimeSignal: Int = 0 {
+        
         didSet { registerTimer() }
     }
     var timer: Timer?
     
-    func fire(_ timer: Timer) {
+    @objc func fire(_ timer: Timer) {
+        
         defer { registerTimer() }
-        if !UserDefaults.standard.notifyTimeSignal { return }
-                
+        
+        guard UserDefaults.standard[.notifyTimeSignal] else { return }
+        
         let now = Date()
         let cal = Calendar.current
         let minutes = cal.component(.minute, from: now)
@@ -43,17 +52,21 @@ class TimeSignalNotifier: NSObject {
         
         let notification = NSUserNotification()
         let hour = cal.component(.hour, from: now)
-        let format = NSLocalizedString("It is soon %zd o'clock.",
-                                       comment: "It is soon %zd o'clock.")
+        let format = LocalizedStrings.timerSIgnalMessage.string
         notification.title = String(format: format, hour + 1)
         notification.informativeText = notification.title
-        if UserDefaults.standard.playNotifyTimeSignalSound {
+        
+        if UserDefaults.standard[.playNotifyTimeSignalSound] {
+            
             notification.soundName = NSUserNotificationDefaultSoundName
+            
         }
+        
         NSUserNotificationCenter.default.deliver(notification)
     }
     
     private func registerTimer() {
+        
         timer?.invalidate()
         
         let now = Date()
@@ -61,11 +74,17 @@ class TimeSignalNotifier: NSObject {
         var comp = cal.dateComponents([.year, .month, .day, .hour], from: now)
         let minutes = cal.component(.minute, from: now)
         if minutes + notifyTimeBeforeTimeSignal >= 60 {
+            
             comp.hour = comp.hour.map { $0 + 1 }
+            
         }
         comp.minute = 60 - notifyTimeBeforeTimeSignal
-        guard let notifyDate = cal.date(from: comp)
-            else { return print("Can not create notify date") }
+        guard let notifyDate = cal.date(from: comp) else {
+            
+            print("Can not create notify date")
+            return
+        }
+        
         timer = Timer.scheduledTimer(timeInterval: notifyDate.timeIntervalSinceNow,
                                      target: self,
                                      selector: .fire,