OSDN Git Service

swiftlint 'line_length'の警告を修正
[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             } else {
37                 postNotification(withType: .cancel)
38             }
39             return
40         }
41         
42         arguments["api_combined_type"]
43             .flatMap { Int($0) }
44             .flatMap { CombineType(rawValue:$0) }
45             .map { postNotification(withType: $0) }
46     }
47     
48     private func postNotification(withType type: CombineType) {
49         let userInfo = [CombinedCommand.userInfoKey: type]
50         NotificationCenter
51             .default
52             .post(name: .CombinedDidCange, object: self, userInfo: userInfo)
53     }
54 }