OSDN Git Service

Timerをクロージャ方式にした
[kcd/KCD.git] / KCD / UpgradableShipsWindowController.swift
1 //
2 //  UpgradableShipsWindowController.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2016/12/18.
6 //  Copyright © 2016年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class UpgradableShipsWindowController: NSWindowController {
12     
13     @objc let managedObjectContext = ServerDataStore.default.context
14     
15     private static var excludeShiIDsCache: [Int] = []
16     
17     class func isExcludeShipID(_ shipID: Int) -> Bool {
18         
19         return excludeShiIDsCache.contains(shipID)
20     }
21     
22     @objc override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
23         
24         switch key {
25             
26         case #keyPath(filterPredicate): return [#keyPath(showLevelOneShipInUpgradableList), #keyPath(showsExcludedShipInUpgradableList)]
27         
28         default: return []
29             
30         }
31     }
32     
33     private var excludeShiIDsCache: [Int] {
34         
35         get { return UpgradableShipsWindowController.excludeShiIDsCache }
36         set { UpgradableShipsWindowController.excludeShiIDsCache = newValue }
37     }
38     
39     private func isExcludeShipID(_ shipID: Int) -> Bool {
40         
41         return excludeShiIDsCache.contains(shipID)
42     }
43     
44     
45     @IBOutlet private var contextualMenu: NSMenu!
46     @IBOutlet private var tableView: NSTableView!
47     @IBOutlet private var shipsController: NSArrayController!
48     
49     override var windowNibName: NSNib.Name {
50         
51         return .nibName(instanceOf: self)
52     }
53     
54     @objc dynamic var filterPredicate: NSPredicate? {
55         
56         var filterPredicate: NSPredicate?
57         var excludeShip: NSPredicate?
58         
59         if showLevelOneShipInUpgradableList == false {
60             
61             filterPredicate = NSPredicate(#keyPath(Ship.lv), notEqual: 1)
62         }
63         
64         if showsExcludedShipInUpgradableList == false,
65             excludeShiIDs.count != 0 {
66             
67             excludeShip = .not(NSPredicate(#keyPath(Ship.id), valuesIn: excludeShiIDs))
68         }
69         
70         if let filterPredicate = filterPredicate,
71             let excludeShip = excludeShip {
72             
73             return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
74         }
75         
76         if let filterPredicate = filterPredicate {
77             
78             return filterPredicate
79         }
80         if let excludeShip = excludeShip {
81             
82             return excludeShip
83         }
84         
85         return nil
86     }
87     
88     @objc var showLevelOneShipInUpgradableList: Bool {
89         
90         get { return UserDefaults.standard[.showLevelOneShipInUpgradableList] }
91         set { UserDefaults.standard[.showLevelOneShipInUpgradableList] = newValue }
92     }
93     
94     @objc var showsExcludedShipInUpgradableList: Bool {
95         
96         get { return UserDefaults.standard[.showsExcludedShipInUpgradableList] }
97         set { UserDefaults.standard[.showsExcludedShipInUpgradableList] = newValue }
98     }
99     
100     var excludeShiIDs: [Int] {
101         
102         get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
103         set {
104             notifyChangeValue(forKey: #keyPath(filterPredicate)) {
105                 
106                 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
107                 UpgradableShipsWindowController.excludeShiIDsCache = newValue
108             }
109         }
110     }
111     
112     private var excludeShipIDsSaveURL: URL {
113         
114         return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
115     }
116     
117     override func windowDidLoad() {
118         
119         super.windowDidLoad()
120         
121         excludeShiIDsCache = excludeShiIDs
122     }
123     
124     private func includeShip(shipID: Int) {
125         
126         guard let index = excludeShiIDs.index(of: shipID) else {
127             
128             return
129         }
130         
131         excludeShiIDs.remove(at: index)
132     }
133     
134     private func excludeShip(shipID: Int) {
135         
136         excludeShiIDs.append(shipID)
137     }
138     
139     @IBAction func showHideShip(_ sender: AnyObject?) {
140         
141         let row = tableView.clickedRow
142         
143         guard let ships = shipsController.arrangedObjects as? [Ship] else {
144             
145             return
146         }
147         guard case 0..<ships.count = row else {
148             
149             return
150         }
151         
152         let shipID = ships[row].id
153         if isExcludeShipID(shipID) {
154             
155             includeShip(shipID: shipID)
156             
157         } else {
158             
159             excludeShip(shipID: shipID)
160         }
161     }
162     
163     override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
164         
165         guard let action = menuItem.action else {
166             
167             return false
168         }
169         
170         switch action {
171             
172         case #selector(UpgradableShipsWindowController.showHideShip(_:)):
173             
174             let row = tableView.clickedRow
175             
176             guard let ships = shipsController.arrangedObjects as? [Ship] else {
177                 
178                 return false
179             }
180             guard case 0..<ships.count = row else {
181                 
182                 return false
183             }
184             
185             if isExcludeShipID(ships[row].id) {
186                 
187                 menuItem.title = LocalizedStrings.showKanmusu.string
188                 
189             } else {
190                 
191                 menuItem.title = LocalizedStrings.hideKanmusu.string
192             }
193             
194             return true
195             
196         default: return false
197             
198         }
199     }
200 }