OSDN Git Service

guard の書き方を統一した
[kcd/KCD.git] / KCD / KenzoDockStatus.swift
1 //
2 //  KenzoDockStatus.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/22.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 private enum DockState: Int {
12     
13     case empty = 0
14     case hasShip = 2
15     case completed = 3
16     case notOpen = -1
17 }
18
19 final class KenzoDockStatus: NSObject {
20     
21     private let number: Int
22     private let controller: NSArrayController
23     private var isTasking = false
24     private var didNotify = false
25     private var realTime: TimeInterval = 0.0 {
26         
27         didSet { time = realTime as NSNumber }
28     }
29     
30     @objc dynamic var time: NSNumber?
31     @objc dynamic var state: NSNumber? {
32         
33         didSet { updateState() }
34     }
35     @objc dynamic var completeTime: NSNumber?
36     
37     init?(number: Int) {
38         
39         guard case 1...4 = number else { return nil }
40         
41         self.number = number
42         controller = NSArrayController()
43         
44         super.init()
45         
46         controller.managedObjectContext = ServerDataStore.default.context
47         controller.entityName = KenzoDock.entityName
48         controller.fetchPredicate = NSPredicate(format: "id = %ld", number)
49         controller.automaticallyRearrangesObjects = true
50         controller.fetch(nil)
51         
52         bind(NSBindingName(#keyPath(state)), to: controller, withKeyPath: "selection.state")
53         bind(NSBindingName(#keyPath(completeTime)), to: controller, withKeyPath: "selection.complete_time")
54     }
55     
56     private func updateState() {
57         
58         guard let state = state as? Int,
59             let s = DockState(rawValue: state) else {
60                 
61                 print("unknown State")
62                 return
63         }
64         
65         switch s {
66         case .empty, .notOpen:
67             isTasking = false
68             didNotify = false
69             
70         case .hasShip, .completed:
71             isTasking = true
72         }
73     }
74     
75     func update() {
76         
77         if !isTasking {
78             
79             time = nil
80             return
81         }
82         guard let completeTime = completeTime as? Int else {
83             
84             time = nil
85             return
86         }
87         
88         let compTime = TimeInterval(Int(completeTime / 1_000))
89         let diff = compTime - Date().timeIntervalSince1970
90         
91         realTime = diff < 0 ? 0 : diff
92         
93         if didNotify { return }
94         if diff > 0 { return }
95         
96         let notification = NSUserNotification()
97         let format = NSLocalizedString("It Will Finish Build at No.%@.", comment: "It Will Finish Build at No.%@.")
98         notification.title = String(format: format, number as NSNumber)
99         notification.informativeText = notification.title
100         
101         if UserDefaults.standard[.playFinishKenzoSound] {
102             
103             notification.soundName = NSUserNotificationDefaultSoundName
104         }
105         
106         NSUserNotificationCenter.default.deliver(notification)
107         
108         didNotify = true
109     }
110     
111 }