2 // KenzoDockStatus.swift
5 // Created by Hori,Masaki on 2017/01/22.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 fileprivate enum DockState: Int {
18 class KenzoDockStatus: NSObject {
19 private let number: Int
20 private let controller: NSArrayController
21 private var isTasking = false
22 private var didNotify = false
23 private var realTime: TimeInterval = 0.0 {
24 didSet { time = realTime as NSNumber }
26 dynamic var time: NSNumber?
27 dynamic var state: NSNumber? {
29 guard let state = state as? Int,
30 let s = DockState(rawValue: state)
31 else { return print("unknown State") }
33 case .empty, .notOpen:
36 case .hasShip, .completed:
43 guard 1...4 ~= number else { return nil }
45 controller = NSArrayController()
47 controller.managedObjectContext = ServerDataStore.default.managedObjectContext
48 controller.entityName = KenzoDock.entityName
49 controller.fetchPredicate = NSPredicate(format: "id = %ld", number)
50 controller.automaticallyRearrangesObjects = true
53 bind("state", to: controller, withKeyPath: "selection.state")
61 guard let t = controller.value(forKeyPath: "selection.complete_time") as? Int
66 let compTime = TimeInterval(Int(t / 1_000))
68 let diff = compTime - now.timeIntervalSince1970
70 realTime = diff < 0 ? 0 : diff
72 if didNotify { return }
73 if diff > 0 { return }
75 let notification = NSUserNotification()
76 let format = NSLocalizedString("It Will Finish Build at No.%@.", comment: "It Will Finish Build at No.%@.")
77 notification.title = String(format: format, number as NSNumber)
78 notification.informativeText = notification.title
79 if UserDefaults.standard.playFinishKenzoSound {
80 notification.soundName = NSUserNotificationDefaultSoundName
82 NSUserNotificationCenter.default.deliver(notification)