OSDN Git Service

必要でない Selectorのstatic letを削除
[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     private var excludeShiIDsCache: [Int] {
33         
34         get { return UpgradableShipsWindowController.excludeShiIDsCache }
35         set { UpgradableShipsWindowController.excludeShiIDsCache = newValue }
36     }
37     
38     private func isExcludeShipID(_ shipID: Int) -> Bool {
39         
40         return excludeShiIDsCache.contains(shipID)
41     }
42     
43     
44     @IBOutlet var contextualMenu: NSMenu!
45     @IBOutlet var tableView: NSTableView!
46     @IBOutlet var shipsController: NSArrayController!
47     
48     override var windowNibName: NSNib.Name {
49         
50         return .nibName(instanceOf: self)
51     }
52     
53     @objc dynamic var filterPredicate: NSPredicate? {
54         
55         var filterPredicate: NSPredicate?
56         var excludeShip: NSPredicate?
57         
58         if showLevelOneShipInUpgradableList == false {
59             
60             filterPredicate = NSPredicate(#keyPath(Ship.lv), notEqual: 1)
61         }
62         
63         if showsExcludedShipInUpgradableList == false,
64             excludeShiIDs.count != 0 {
65             
66             excludeShip = .not(NSPredicate(#keyPath(Ship.id), valuesIn: excludeShiIDs))
67         }
68         
69         if let filterPredicate = filterPredicate,
70             let excludeShip = excludeShip {
71             
72             return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
73         }
74         
75         if let filterPredicate = filterPredicate { return filterPredicate }
76         if let excludeShip = excludeShip { return excludeShip }
77         
78         return nil
79     }
80     
81     @objc var showLevelOneShipInUpgradableList: Bool {
82         
83         get { return UserDefaults.standard[.showLevelOneShipInUpgradableList] }
84         set { UserDefaults.standard[.showLevelOneShipInUpgradableList] = newValue }
85     }
86     
87     @objc var showsExcludedShipInUpgradableList: Bool {
88         
89         get { return UserDefaults.standard[.showsExcludedShipInUpgradableList] }
90         set { UserDefaults.standard[.showsExcludedShipInUpgradableList] = newValue }
91     }
92     
93     var excludeShiIDs: [Int] {
94         
95         get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
96         set {
97             notifyChangeValue(forKey: #keyPath(filterPredicate)) {
98                 
99                 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
100                 UpgradableShipsWindowController.excludeShiIDsCache = newValue
101             }
102         }
103     }
104     
105     private var excludeShipIDsSaveURL: URL {
106         
107         return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
108     }
109     
110     override func windowDidLoad() {
111         
112         super.windowDidLoad()
113         
114         excludeShiIDsCache = excludeShiIDs
115     }
116     
117     private func includeShip(shipID: Int) {
118         
119         var array = excludeShiIDs
120         
121         guard let index = array.index(of: shipID) else { return }
122         
123         array.remove(at: index)
124         excludeShiIDs = array
125     }
126     
127     private func excludeShip(shipID: Int) {
128         
129         var array = excludeShiIDs
130         array.append(shipID)
131         excludeShiIDs = array
132     }
133     
134     @IBAction func showHideShip(_ sender: AnyObject?) {
135         
136         let row = tableView.clickedRow
137         
138         guard let ships = shipsController.arrangedObjects as? [Ship] else { return }
139         guard case 0..<ships.count = row else { return }
140         
141         let shipID = ships[row].id
142         if isExcludeShipID(shipID) {
143             
144             includeShip(shipID: shipID)
145             
146         } else {
147             
148             excludeShip(shipID: shipID)
149         }
150     }
151     
152     override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
153         
154         guard let action = menuItem.action else { return false }
155         
156         switch action {
157             
158         case #selector(UpgradableShipsWindowController.showHideShip(_:)):
159             
160             let row = tableView.clickedRow
161             
162             guard let ships = shipsController.arrangedObjects as? [Ship] else { return false }
163             guard case 0..<ships.count = row else { return false }
164             
165             if isExcludeShipID(ships[row].id) {
166                 
167                 menuItem.title = LocalizedStrings.showKanmusu.string
168                 
169             } else {
170                 
171                 menuItem.title = LocalizedStrings.hideKanmusu.string
172             }
173             
174             return true
175             
176         default: return false
177         }
178     }
179 }