OSDN Git Service

Doutakuを導入
[kcd/KCD.git] / KCD / CombinedCommand.swift
index 98e5598..9330666 100644 (file)
@@ -9,44 +9,67 @@
 import Cocoa
 
 enum CombineType: Int {
+    
     case cancel
     case maneuver
     case water
     case transportation
+    
+    func displayName() -> String {
+        
+        switch self {
+        case .cancel: return LocalizedStrings.uncombined.string
+        case .maneuver: return LocalizedStrings.maneuver.string
+        case .water: return LocalizedStrings.water.string
+        case .transportation: return LocalizedStrings.transportation.string
+        }
+    }
 }
 
 extension Notification.Name {
+    
     static let CombinedDidCange = Notification.Name("com.masakih.KCD.Notification.CombinedDidCange")
 }
 
-class CombinedCommand: JSONCommand {
+final class CombinedCommand: JSONCommand {
+    
     static let userInfoKey = "com.masakih.KCD.Notification.CombinedDidCange.CombinedType"
-    override class func canExecuteAPI(_ api: String) -> Bool {
-        if api == "/kcsapi/api_req_hensei/combined" { return true }
-        return false
+    
+    override class func canExecuteAPI(_ api: API) -> Bool {
+        
+        return api.endpoint == .henseiCombined
     }
     
     override func execute() {
-        if api == "/kcsapi/api_port/port" {
-            if let t = data["api_combined_flag"].int {
-                CombineType(rawValue: t)
-                    .map { postNotification(withType: $0) }
-            } else {
-                postNotification(withType: .cancel)
-            }
+        
+        if api.endpoint == .port {
+            
+            handlePort()
             return
         }
         
-        arguments["api_combined_type"]
+        parameter["api_combined_type"]
             .int
-            .flatMap { CombineType(rawValue:$0) }
-            .map { postNotification(withType: $0) }
+            .flatMap { CombineType(rawValue: $0) }
+            .map(postNotification(withType:))
+    }
+    
+    private func handlePort() {
+        
+        if let t = data["api_combined_flag"].int {
+            
+            CombineType(rawValue: t).map(postNotification(withType:))
+            
+        } else {
+            
+            postNotification(withType: .cancel)
+        }
     }
     
     private func postNotification(withType type: CombineType) {
+        
         let userInfo = [CombinedCommand.userInfoKey: type]
-        NotificationCenter
-            .default
-            .post(name: .CombinedDidCange, object: self, userInfo: userInfo)
+        
+        NotificationCenter.default.post(name: .CombinedDidCange, object: self, userInfo: userInfo)
     }
 }