From 136eb334f3c509647e5538ecc5c4122501b5fd9a Mon Sep 17 00:00:00 2001 From: masakih Date: Wed, 29 Mar 2017 01:21:52 +0900 Subject: [PATCH] =?utf8?q?value(forKeyPath:)=E3=82=92=E4=BD=BF=E7=94=A8?= =?utf8?q?=E3=81=9B=E3=81=9Abind(=5F:=E2=80=A6)=E3=82=92=E4=BD=BF=E7=94=A8?= =?utf8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- KCD/KenzoDockStatus.swift | 37 ++++++++++++++++++++----------------- KCD/MissionStatus.swift | 23 ++++++++++++++++------- KCD/NyukyoDockStatus.swift | 24 ++++++++++++++++-------- 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/KCD/KenzoDockStatus.swift b/KCD/KenzoDockStatus.swift index a8d97967..d4d067f1 100644 --- a/KCD/KenzoDockStatus.swift +++ b/KCD/KenzoDockStatus.swift @@ -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 diff --git a/KCD/MissionStatus.swift b/KCD/MissionStatus.swift index 81bf4159..7e09049f 100644 --- a/KCD/MissionStatus.swift +++ b/KCD/MissionStatus.swift @@ -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.") diff --git a/KCD/NyukyoDockStatus.swift b/KCD/NyukyoDockStatus.swift index 1c893de4..dfac3be9 100644 --- a/KCD/NyukyoDockStatus.swift +++ b/KCD/NyukyoDockStatus.swift @@ -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 -- 2.11.0