2 // HMKenzoDockStatus.swift
5 // Created by Hori,Masaki on 2015/01/05.
6 // Copyright (c) 2015年 Hori,Masaki. All rights reserved.
11 class HMKenzoDockStatus: NSObject {
12 init(dockNumber: Int) {
13 assert(0 < dockNumber && dockNumber < 5, "dockNumber is out of range")
15 controller = NSArrayController()
16 self.dockNumber = dockNumber
19 controller.managedObjectContext = HMServerDataStore.defaultManager().managedObjectContext
20 controller.entityName = "KenzoDock"
21 controller.fetchPredicate = NSPredicate(format: "id = \(dockNumber)")
22 controller.automaticallyRearrangesObjects = true
25 controller.addObserver(self, forKeyPath: "selection.state", options: .Initial, context: nil)
28 enum KenzoDockStatus: Int {
36 let controller: NSArrayController
38 dynamic var time: NSNumber? = nil
39 var tasking: Bool = false
40 var didNotify: Bool = false
41 var managedObjectContext: NSManagedObjectContext? = nil
43 override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
45 case "selection.state":
46 if let state = controller.valueForKeyPath("selection.state")?.integerValue {
47 if let status = KenzoDockStatus(rawValue: state) {
60 NSLog("Kenzo Dock status is %ld", state)
64 super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
73 if let compTimeValue = controller.valueForKeyPath("selection.complete_time") as? NSNumber {
74 let compTime: NSTimeInterval = ceil(compTimeValue.doubleValue / 1000.0)
75 let now = NSDate(timeIntervalSinceNow: 0)
76 let diff: NSTimeInterval = compTime - now.timeIntervalSince1970
85 let notification = NSUserNotification()
86 let format = NSLocalizedString("It Will Finish Build at No.%@.", comment: "It Will Finish Build at No.%@.")
87 notification.title = NSString(format: format, NSNumber(integer: dockNumber))
88 notification.informativeText = notification.title
89 if HMUserDefaults.hmStandardDefauls().playFinishKenzoSound {
90 notification.soundName = NSUserNotificationDefaultSoundName
92 NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)