OSDN Git Service

Squashed commit of the following: Swiftに変換した
[kcd/KCD.git] / KCD / JSONViewCommand.swift
1 //
2 //  JSONViewCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/19.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 #if ENABLE_JSON_LOG
12
13 class JSONViewCommand: JSONCommand {
14     let jsonTree: [JSONNode]?
15     let parameterList: [Any]
16     let recieveDate: Date?
17     let command: JSONCommand
18     
19     init(apiResponse: APIResponse, command: JSONCommand) {
20         self.recieveDate = Date()
21         self.parameterList = apiResponse.argumentArray
22         self.jsonTree = JSONNode
23             .nodeWithJSON(apiResponse.json as AnyObject?)
24             .map { [$0] }
25         self.command = command
26         super.init(apiResponse: apiResponse)
27     }
28     
29     required init(apiResponse: APIResponse) {
30         fatalError("use init(apiResponse:command:)")
31     }
32     
33     override func execute() {
34         command.execute()
35         
36         guard let _ = jsonTree else { return print("jsonTree is nil.") }
37         guard let _ = recieveDate else { return print("recieveDate is nil.") }
38         
39         DispatchQueue.main.async {
40             guard let appDelegate = NSApplication.shared().delegate as? AppDelegate
41                 else { return print("Can not get AppDelegate") }
42             let commands: [String:Any] = [
43                 "api": self.api,
44                 "argument": self.parameterList,
45                 "json": self.jsonTree ?? [],
46                 "recieveDate": self.recieveDate ?? Date(),
47                 "date": Date()
48              ]
49             appDelegate.jsonViewWindowController?.setCommand(commands as NSDictionary)
50         }
51     }
52 }
53
54 #endif