OSDN Git Service

UAをVersion/10.0.3 Safari/602.4.8に変更
[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     case cancel
13     case maneuver
14     case water
15     case transportation
16 }
17
18 extension Notification.Name {
19     static let CombinedDidCange = Notification.Name("com.masakih.KCD.Notification.CombinedDidCange")
20 }
21
22 class CombinedCommand: JSONCommand {
23     static let userInfoKey = "com.masakih.KCD.Notification.CombinedDidCange.CombinedType"
24     override class func canExecuteAPI(_ api: String) -> Bool {
25         if api == "/kcsapi/api_req_hensei/combined" { return true }
26         return false
27     }
28     
29     override func execute() {
30         if api == "/kcsapi/api_port/port" {
31             if let t = data["api_combined_flag"].int {
32                 CombineType(rawValue: t)
33                     .map { postNotification(withType: $0) }
34             } else {
35                 postNotification(withType: .cancel)
36             }
37             return
38         }
39         
40         parameter["api_combined_type"]
41             .int
42             .flatMap { CombineType(rawValue: $0) }
43             .map { postNotification(withType: $0) }
44     }
45     
46     private func postNotification(withType type: CombineType) {
47         let userInfo = [CombinedCommand.userInfoKey: type]
48         NotificationCenter
49             .default
50             .post(name: .CombinedDidCange, object: self, userInfo: userInfo)
51     }
52 }