OSDN Git Service

HMMasterMissionCommandクラスをSwiftで書き換え
[kcd/KCD.git] / KCD / HMKenzoDockStatus.swift
1 //
2 //  HMKenzoDockStatus.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2015/01/05.
6 //  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 class HMKenzoDockStatus: NSObject {
12         init(dockNumber: Int) {
13                 assert(0 < dockNumber && dockNumber < 5, "dockNumber is out of range")
14                 
15                 controller = NSArrayController()
16                 self.dockNumber = dockNumber
17                 super.init()
18                 
19                 controller.managedObjectContext = HMServerDataStore.defaultManager().managedObjectContext
20                 controller.entityName = "KenzoDock"
21                 controller.fetchPredicate = NSPredicate(format: "id = \(dockNumber)")
22                 controller.automaticallyRearrangesObjects = true
23                 controller.fetch(nil)
24                 
25                 controller.addObserver(self, forKeyPath: "selection.state", options: .Initial, context: nil)
26         }
27         
28         enum KenzoDockStatus: Int {
29                 case noShip = 0
30                 case hasShip = 2
31                 case complete = 3
32                 case notOpen = -1
33         }
34         
35         let dockNumber: Int
36         let controller: NSArrayController
37         
38         dynamic var time: NSNumber? = nil
39         var tasking: Bool = false
40         var didNotify: Bool = false
41         var managedObjectContext: NSManagedObjectContext? = nil
42         
43         override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
44                 switch keyPath {
45                 case "selection.state":
46                         if let state = controller.valueForKeyPath("selection.state")?.integerValue {
47                                 if let status = KenzoDockStatus(rawValue: state) {
48                                         switch status {
49                                         case .noShip:
50                                                 fallthrough
51                                         case .notOpen:
52                                                 tasking = false
53                                                 didNotify = false
54                                         case .hasShip:
55                                                 fallthrough
56                                         case .complete:
57                                                 tasking = true
58                                         }
59                                 } else {
60                                         NSLog("Kenzo Dock status is %ld", state)
61                                 }
62                         }
63                 default:
64                         super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
65                 }
66         }
67         
68         func update() {
69                 if !tasking {
70                         time = nil
71                         return
72                 }
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
77                         if diff < 0 {
78                                 time = 0
79                         } else {
80                                 time = diff
81                         }
82                         
83                         if !didNotify {
84                                 if diff <= 0 {
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
91                                         }
92                                         NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)
93                                         didNotify = true
94                                 }
95                         }
96                 } else {
97                         time = nil
98                 }
99         }
100 }