OSDN Git Service

UAをVersion/10.0.3 Safari/602.4.8に変更
[kcd/KCD.git] / KCD / NyukyoDockStatus.swift
1 //
2 //  NyukyoDockStatus.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/23.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 fileprivate enum DockState: Int {
12     case empty = 0
13     case hasShip = 1
14 }
15
16 class NyukyoDockStatus: NSObject {
17     private let number: Int
18     private let controller: NSArrayController
19     private var didNotify = false
20     private var realTime: TimeInterval = 0.0 {
21         didSet { time = realTime as NSNumber }
22     }
23     dynamic var name: String?
24     dynamic var time: NSNumber?
25     dynamic var state: NSNumber? {
26         didSet { updateState() }
27     }
28     
29     init?(number: Int) {
30         guard 1...4 ~= number else { return nil }
31         self.number = number
32         controller = NSArrayController()
33         super.init()
34         controller.managedObjectContext = ServerDataStore.default.context
35         controller.entityName = NyukyoDock.entityName
36         controller.fetchPredicate = NSPredicate(format: "id = %ld", number)
37         controller.automaticallyRearrangesObjects = true
38         controller.fetch(nil)
39         
40         bind("state", to: controller, withKeyPath: "selection.state")
41     }
42     
43     private func updateState() {
44         guard let state = state as? Int,
45             let stat = DockState(rawValue: state)
46             else { return print("unknown State") }
47         if stat == .empty {
48             didNotify = false
49             name = nil
50             time = nil
51             return
52         }
53         
54         guard let si = controller.value(forKeyPath: "selection.ship_id") as? Int,
55             si != 0
56             else { return }
57         guard let ship = ServerDataStore.default.ship(byId: si)
58             else {
59                 name = "Unknown"
60                 DispatchQueue(label: "NyukyoDockStatus")
61                     .asyncAfter(deadline: .now() + 0.33) {
62                         self.updateState()
63                 }
64                 return
65         }
66         name = ship.name
67     }
68     
69     func update() {
70         if name == nil {
71             time = nil
72             return
73         }
74         guard let t = controller.value(forKeyPath: "selection.complete_time") as? Int
75             else {
76                 name = nil
77                 time = nil
78                 return
79         }
80         let compTime = TimeInterval(Int(t / 1_000))
81         let now = Date()
82         let diff = compTime - now.timeIntervalSince1970
83         
84         realTime = diff < 0 ? 0 : diff
85         
86         if didNotify { return }
87         if diff >= 1 * 60 { return }
88         
89         let notification = NSUserNotification()
90         let format = NSLocalizedString("%@ Will Finish Docking.", comment: "%@ Will Finish Docking.")
91         notification.title = String(format: format, name!)
92         notification.informativeText = notification.title
93         if UserDefaults.standard.playFinishNyukyoSound {
94             notification.soundName = NSUserNotificationDefaultSoundName
95         }
96         NSUserNotificationCenter.default.deliver(notification)
97         didNotify = true
98     }
99 }