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     case maneuver
15     case water
16     case transportation
17 }
18
19 extension Notification.Name {
20     
21     static let CombinedDidCange = Notification.Name("com.masakih.KCD.Notification.CombinedDidCange")
22 }
23
24 final class CombinedCommand: JSONCommand {
25     
26     static let userInfoKey = "com.masakih.KCD.Notification.CombinedDidCange.CombinedType"
27     
28     override class func canExecuteAPI(_ api: API) -> Bool {
29         
30         return api.endpoint == .henseiCombined
31     }
32     
33     override func execute() {
34         
35         if api.endpoint == .port {
36             
37             if let t = data["api_combined_flag"].int {
38                 
39                 CombineType(rawValue: t).map(postNotification(withType:))
40                 
41             } else {
42                 
43                 postNotification(withType: .cancel)
44             }
45             
46             return
47         }
48         
49         parameter["api_combined_type"]
50             .int
51             .flatMap { CombineType(rawValue: $0) }
52             .map(postNotification(withType:))
53     }
54     
55     private func postNotification(withType type: CombineType) {
56         
57         let userInfo = [CombinedCommand.userInfoKey: type]
58         
59         NotificationCenter.default.post(name: .CombinedDidCange, object: self, userInfo: userInfo)
60     }
61 }