6 // Created by Hori,Masaki on 2017/01/23.
7 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 fileprivate enum State: Int {
19 class MissionStatus: NSObject {
20 private let number: Int
21 private let controller: NSArrayController
22 private var didNotify = false
23 private var realTime: TimeInterval = 0.0 {
24 didSet { time = realTime as NSNumber }
26 dynamic var name: String?
27 dynamic var time: NSNumber?
28 dynamic var state: NSNumber?
29 dynamic var missionId: NSNumber? {
30 didSet { updateState() }
34 guard (2...4).contains(number) else { return nil }
36 controller = NSArrayController()
38 controller.managedObjectContext = ServerDataStore.default.managedObjectContext
39 controller.entityName = Deck.entityName
40 controller.fetchPredicate = NSPredicate(format: "id = %ld", number)
41 controller.automaticallyRearrangesObjects = true
44 bind("state", to: controller, withKeyPath: "selection.mission_0")
45 bind("missionId", to: controller, withKeyPath: "selection.mission_1")
48 private func updateState() {
49 guard let stat = State(rawValue: state as! Int)
50 else { return print("unknown State") }
51 if stat == .none || stat == .finish {
52 if stat == .none { didNotify = false }
58 guard let missionId = self.missionId as? Int
60 guard let mission = ServerDataStore.default.masterMission(by: missionId)
63 DispatchQueue(label: "MissionStatus")
64 .asyncAfter(deadline: .now() + 0.33) {
77 guard let t = controller.value(forKeyPath: "selection.mission_2") as? Int
83 let compTime = TimeInterval(Int(t / 1_000))
85 let diff = compTime - now.timeIntervalSince1970
87 if diff < 0 { realTime = 0 }
88 else { realTime = diff }
90 if didNotify { return }
91 if diff >= 1 * 60 { return }
93 guard let fleetName = controller.value(forKeyPath: "selection.name") as? String
95 let notification = NSUserNotification()
96 let format = NSLocalizedString("%@ Will Return From Mission.", comment: "%@ Will Return From Mission.")
97 notification.title = String(format: format, fleetName)
98 let txtFormat = NSLocalizedString("%@ Will Return From %@.", comment: "%@ Will Return From %@.")
99 notification.informativeText = String(format: txtFormat, fleetName, name!)
100 if UserDefaults.standard.playFinishMissionSound {
101 notification.soundName = NSUserNotificationDefaultSoundName
103 NSUserNotificationCenter.default.deliver(notification)