OSDN Git Service

guard の書き方を統一した
[kcd/KCD.git] / KCD / TimeSignalNotifier.swift
1 //
2 //  TimeSignalNotifier.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/21.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 extension Selector {
12     
13     static let fire = #selector(TimeSignalNotifier.fire(_:))
14 }
15
16 final class TimeSignalNotifier: NSObject {
17     
18     let udController: NSUserDefaultsController = NSUserDefaultsController.shared
19     
20     override init() {
21         
22         super.init()
23         
24         registerTimer()
25         bind(NSBindingName(#keyPath(notifyTimeBeforeTimeSignal)),
26              to: udController,
27              withKeyPath: "values.notifyTimeBeforeTimeSignal")
28     }
29     
30     deinit {
31         
32         unbind(NSBindingName(#keyPath(notifyTimeBeforeTimeSignal)))
33     }
34     
35     @objc dynamic var notifyTimeBeforeTimeSignal: Int = 0 {
36         
37         didSet { registerTimer() }
38     }
39     var timer: Timer?
40     
41     @objc func fire(_ timer: Timer) {
42         
43         defer { registerTimer() }
44         
45         guard UserDefaults.standard[.notifyTimeSignal] else { return }
46         
47         let now = Date()
48         let cal = Calendar.current
49         let minutes = cal.component(.minute, from: now)
50         
51         if (59 - minutes) > notifyTimeBeforeTimeSignal { return }
52         
53         let notification = NSUserNotification()
54         let hour = cal.component(.hour, from: now)
55         let format = NSLocalizedString("It is soon %zd o'clock.", comment: "It is soon %zd o'clock.")
56         notification.title = String(format: format, hour + 1)
57         notification.informativeText = notification.title
58         
59         if UserDefaults.standard[.playNotifyTimeSignalSound] {
60             
61             notification.soundName = NSUserNotificationDefaultSoundName
62             
63         }
64         
65         NSUserNotificationCenter.default.deliver(notification)
66     }
67     
68     private func registerTimer() {
69         
70         timer?.invalidate()
71         
72         let now = Date()
73         let cal = Calendar.current
74         var comp = cal.dateComponents([.year, .month, .day, .hour], from: now)
75         let minutes = cal.component(.minute, from: now)
76         if minutes + notifyTimeBeforeTimeSignal >= 60 {
77             
78             comp.hour = comp.hour.map { $0 + 1 }
79             
80         }
81         comp.minute = 60 - notifyTimeBeforeTimeSignal
82         guard let notifyDate = cal.date(from: comp) else {
83             
84             print("Can not create notify date")
85             return
86         }
87         
88         timer = Timer.scheduledTimer(timeInterval: notifyDate.timeIntervalSinceNow,
89                                      target: self,
90                                      selector: .fire,
91                                      userInfo: nil,
92                                      repeats: false)
93     }
94 }