OSDN Git Service

改修工廠メニューを更新
[kcd/KCD.git] / KCD / CombinedCommand.swift
1 //
2 //  CombinedCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/09.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 enum CombineType: Int {
12     
13     case cancel
14     
15     case maneuver
16     
17     case water
18     
19     case transportation
20     
21     func displayName() -> String {
22         
23         switch self {
24             
25         case .cancel: return LocalizedStrings.uncombined.string
26             
27         case .maneuver: return LocalizedStrings.maneuver.string
28             
29         case .water: return LocalizedStrings.water.string
30             
31         case .transportation: return LocalizedStrings.transportation.string
32             
33         }
34     }
35 }
36
37 extension Notification.Name {
38     
39     static let CombinedDidCange = Notification.Name("com.masakih.KCD.Notification.CombinedDidCange")
40 }
41
42 final class CombinedCommand: JSONCommand {
43     
44     static let userInfoKey = "com.masakih.KCD.Notification.CombinedDidCange.CombinedType"
45     
46     override class func canExecuteAPI(_ api: API) -> Bool {
47         
48         return api.endpoint == .henseiCombined
49     }
50     
51     override func execute() {
52         
53         if api.endpoint == .port {
54             
55             handlePort()
56             
57             return
58         }
59         
60         parameter["api_combined_type"]
61             .int
62             .flatMap { CombineType(rawValue: $0) }
63             .map(postNotification(withType:))
64     }
65     
66     private func handlePort() {
67         
68         if let t = data["api_combined_flag"].int {
69             
70             CombineType(rawValue: t).map(postNotification(withType:))
71             
72         } else {
73             
74             postNotification(withType: .cancel)
75         }
76     }
77     
78     private func postNotification(withType type: CombineType) {
79         
80         let userInfo = [CombinedCommand.userInfoKey: type]
81         
82         NotificationCenter.default.post(name: .CombinedDidCange, object: self, userInfo: userInfo)
83     }
84 }