OSDN Git Service

swiftline 'variable_name'のエラーを修正
[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             guard let data = json["api_data"] as? [String: Any]
32                 else { return }
33             if let t = data["api_combined_flag"] as? Int {
34                 CombineType(rawValue: t)
35                     .map { postNotification(withType: $0) }
36             }
37             else { postNotification(withType: .cancel) }
38             return
39         }
40         
41         arguments["api_combined_type"]
42             .flatMap { Int($0) }
43             .flatMap { CombineType(rawValue:$0) }
44             .map { postNotification(withType: $0) }
45     }
46     
47     private func postNotification(withType type: CombineType) {
48         let userInfo = [CombinedCommand.userInfoKey: type]
49         NotificationCenter
50             .default
51             .post(name: .CombinedDidCange, object: self, userInfo: userInfo)
52     }
53 }