OSDN Git Service

value(forKeyPath:)を使用せずbind(_:…)を使用するようにした
authormasakih <masakih@users.sourceforge.jp>
Tue, 28 Mar 2017 16:21:52 +0000 (01:21 +0900)
committermasakih <masakih@users.sourceforge.jp>
Tue, 28 Mar 2017 16:21:52 +0000 (01:21 +0900)
KCD/KenzoDockStatus.swift
KCD/MissionStatus.swift
KCD/NyukyoDockStatus.swift

index a8d9796..d4d067f 100644 (file)
@@ -25,19 +25,9 @@ class KenzoDockStatus: NSObject {
     }
     dynamic var time: NSNumber?
     dynamic var state: NSNumber? {
-        didSet {
-            guard let state = state as? Int,
-                let s = DockState(rawValue: state)
-                else { return print("unknown State") }
-            switch s {
-            case .empty, .notOpen:
-                isTasking = false
-                didNotify = false
-            case .hasShip, .completed:
-                isTasking = true
-            }
-        }
+        didSet { updateState() }
     }
+    dynamic var completeTime: NSNumber?
     
     init?(number: Int) {
         guard 1...4 ~= number else { return nil }
@@ -50,7 +40,21 @@ class KenzoDockStatus: NSObject {
         controller.automaticallyRearrangesObjects = true
         controller.fetch(nil)
         
-        bind("state", to: controller, withKeyPath: "selection.state")
+        bind(#keyPath(state), to: controller, withKeyPath: "selection.state")
+        bind(#keyPath(completeTime), to: controller, withKeyPath: "selection.complete_time")
+    }
+    
+    private func updateState() {
+        guard let state = state as? Int,
+            let s = DockState(rawValue: state)
+            else { return print("unknown State") }
+        switch s {
+        case .empty, .notOpen:
+            isTasking = false
+            didNotify = false
+        case .hasShip, .completed:
+            isTasking = true
+        }
     }
     
     func update() {
@@ -58,14 +62,13 @@ class KenzoDockStatus: NSObject {
             time = nil
             return
         }
-        guard let t = controller.value(forKeyPath: "selection.complete_time") as? Int
+        guard let completeTime = completeTime as? Int
             else {
                 time = nil
                 return
         }
-        let compTime = TimeInterval(Int(t / 1_000))
-        let now = Date()
-        let diff = compTime - now.timeIntervalSince1970
+        let compTime = TimeInterval(Int(completeTime / 1_000))
+        let diff = compTime - Date().timeIntervalSince1970
         
         realTime = diff < 0 ? 0 : diff
         
index 81bf415..7e09049 100644 (file)
@@ -29,6 +29,8 @@ class MissionStatus: NSObject {
     dynamic var missionId: NSNumber? {
         didSet { updateState() }
     }
+    dynamic var milliseconds: NSNumber?
+    dynamic var fleetName: String?
     
     init?(number: Int) {
         guard 2...4 ~= number else { return nil }
@@ -41,8 +43,16 @@ class MissionStatus: NSObject {
         controller.automaticallyRearrangesObjects = true
         controller.fetch(nil)
         
-        bind("state", to: controller, withKeyPath: "selection.mission_0")
-        bind("missionId", to: controller, withKeyPath: "selection.mission_1")
+        bind(#keyPath(state), to: controller, withKeyPath: "selection.mission_0")
+        bind(#keyPath(missionId), to: controller, withKeyPath: "selection.mission_1")
+        bind(#keyPath(milliseconds), to: controller, withKeyPath: "selection.mission_2")
+        bind(#keyPath(fleetName), to: controller, withKeyPath: "selection.name")
+    }
+    deinit {
+        unbind(#keyPath(state))
+        unbind(#keyPath(missionId))
+        unbind(#keyPath(milliseconds))
+        unbind(#keyPath(fleetName))
     }
     
     private func updateState() {
@@ -75,22 +85,21 @@ class MissionStatus: NSObject {
             time = nil
             return
         }
-        guard let t = controller.value(forKeyPath: "selection.mission_2") as? Int
+        guard let milliSeconds = milliseconds as? Int
             else {
                 name = nil
                 time = nil
                 return
         }
-        let compTime = TimeInterval(Int(t / 1_000))
-        let now = Date()
-        let diff = compTime - now.timeIntervalSince1970
+        let compTime = TimeInterval(Int(milliSeconds / 1_000))
+        let diff = compTime - Date().timeIntervalSince1970
         
         realTime = diff < 0 ? 0 : diff
         
         if didNotify { return }
         if diff >= 1 * 60 { return }
         
-        guard let fleetName = controller.value(forKeyPath: "selection.name") as? String
+        guard let fleetName = fleetName
             else { return }
         let notification = NSUserNotification()
         let format = NSLocalizedString("%@ Will Return From Mission.", comment: "%@ Will Return From Mission.")
index 1c893de..dfac3be 100644 (file)
@@ -25,6 +25,8 @@ class NyukyoDockStatus: NSObject {
     dynamic var state: NSNumber? {
         didSet { updateState() }
     }
+    dynamic var shipId: NSNumber?
+    dynamic var completeTime: NSNumber?
     
     init?(number: Int) {
         guard 1...4 ~= number else { return nil }
@@ -37,7 +39,14 @@ class NyukyoDockStatus: NSObject {
         controller.automaticallyRearrangesObjects = true
         controller.fetch(nil)
         
-        bind("state", to: controller, withKeyPath: "selection.state")
+        bind(#keyPath(state), to: controller, withKeyPath: "selection.state")
+        bind(#keyPath(shipId), to: controller, withKeyPath: "selection.ship_id")
+        bind(#keyPath(completeTime), to: controller, withKeyPath: "selection.complete_time")
+    }
+    deinit {
+        unbind(#keyPath(state))
+        unbind(#keyPath(shipId))
+        unbind(#keyPath(completeTime))
     }
     
     private func updateState() {
@@ -51,10 +60,10 @@ class NyukyoDockStatus: NSObject {
             return
         }
         
-        guard let si = controller.value(forKeyPath: "selection.ship_id") as? Int,
-            si != 0
+        guard let shipId = shipId as? Int,
+            shipId != 0
             else { return }
-        guard let ship = ServerDataStore.default.ship(byId: si)
+        guard let ship = ServerDataStore.default.ship(byId: shipId)
             else {
                 name = "Unknown"
                 DispatchQueue(label: "NyukyoDockStatus")
@@ -71,15 +80,14 @@ class NyukyoDockStatus: NSObject {
             time = nil
             return
         }
-        guard let t = controller.value(forKeyPath: "selection.complete_time") as? Int
+        guard let completeTime = completeTime as? Int
             else {
                 name = nil
                 time = nil
                 return
         }
-        let compTime = TimeInterval(Int(t / 1_000))
-        let now = Date()
-        let diff = compTime - now.timeIntervalSince1970
+        let compTime = TimeInterval(Int(completeTime / 1_000))
+        let diff = compTime - Date().timeIntervalSince1970
         
         realTime = diff < 0 ? 0 : diff