2 // KenzoDockStatus.swift
5 // Created by Hori,Masaki on 2017/01/22.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 private enum DockState: Int {
19 final class KenzoDockStatus: NSObject {
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 {
27 didSet { time = realTime as NSNumber }
30 @objc dynamic var time: NSNumber?
31 @objc dynamic var state: NSNumber? {
33 didSet { updateState() }
35 @objc dynamic var completeTime: NSNumber?
39 guard case 1...4 = number else { return nil }
45 controller.managedObjectContext = ServerDataStore.default.context
46 controller.entityName = KenzoDock.entityName
47 controller.fetchPredicate = NSPredicate(#keyPath(KenzoDock.id), equal: number)
48 controller.automaticallyRearrangesObjects = true
51 bind(NSBindingName(#keyPath(state)), to: controller, withKeyPath: "selection.state")
52 bind(NSBindingName(#keyPath(completeTime)), to: controller, withKeyPath: "selection.complete_time")
55 private func updateState() {
57 guard let state = state as? Int,
58 let stat = DockState(rawValue: state) else {
60 return Logger.shared.log("unknown State")
64 case .empty, .notOpen:
68 case .hasShip, .completed:
80 guard let completeTime = completeTime as? Int else {
86 let compTime = TimeInterval(Int(completeTime / 1_000))
87 let diff = compTime - Date().timeIntervalSince1970
89 realTime = max(0, diff)
91 if didNotify { return }
92 if diff > 0 { return }
94 let notification = NSUserNotification()
95 let format = LocalizedStrings.buildingWillFinish.string
96 notification.title = String(format: format, number as NSNumber)
97 notification.informativeText = notification.title
99 if UserDefaults.standard[.playFinishKenzoSound] {
101 notification.soundName = NSUserNotificationDefaultSoundName
104 NSUserNotificationCenter.default.deliver(notification)