OSDN Git Service

関数名を変更
[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         
43         super.init()
44         
45         controller.managedObjectContext = ServerDataStore.default.context
46         controller.entityName = KenzoDock.entityName
47         controller.fetchPredicate = NSPredicate(#keyPath(KenzoDock.id), equal: number)
48         controller.automaticallyRearrangesObjects = true
49         controller.fetch(nil)
50         
51         bind(NSBindingName(#keyPath(state)), to: controller, withKeyPath: "selection.state")
52         bind(NSBindingName(#keyPath(completeTime)), to: controller, withKeyPath: "selection.complete_time")
53     }
54     
55     private func updateState() {
56         
57         guard let state = state as? Int,
58             let stat = DockState(rawValue: state) else {
59                 
60                 return Logger.shared.log("unknown State")
61         }
62         
63         switch stat {
64         case .empty, .notOpen:
65             isTasking = false
66             didNotify = false
67             
68         case .hasShip, .completed:
69             isTasking = true
70         }
71     }
72     
73     func update() {
74         
75         if !isTasking {
76             
77             time = nil
78             return
79         }
80         guard let completeTime = completeTime as? Int else {
81             
82             time = nil
83             return
84         }
85         
86         let compTime = TimeInterval(Int(completeTime / 1_000))
87         let diff = compTime - Date().timeIntervalSince1970
88         
89         realTime = max(0, diff)
90         
91         if didNotify { return }
92         if diff > 0 { return }
93         
94         let notification = NSUserNotification()
95         let format = LocalizedStrings.buildingWillFinish.string
96         notification.title = String(format: format, number as NSNumber)
97         notification.informativeText = notification.title
98         
99         if UserDefaults.standard[.playFinishKenzoSound] {
100             
101             notification.soundName = NSUserNotificationDefaultSoundName
102         }
103         
104         NSUserNotificationCenter.default.deliver(notification)
105         
106         didNotify = true
107     }
108     
109 }