OSDN Git Service

処理を分かりやすくした
[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 levelOnePredicate: NSPredicate?
57         var excludeShip: NSPredicate?
58         
59         if showLevelOneShipInUpgradableList == false {
60             
61             levelOnePredicate = 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         switch (levelOnePredicate, excludeShip) {
71             
72         case let (p0?, p1?): return p0.and(p1)
73             
74         case let (p0?, _): return p0
75             
76         case let (_, p1?): return p1
77             
78         default: return nil
79         }
80     }
81     
82     @objc var showLevelOneShipInUpgradableList: Bool {
83         
84         get { return UserDefaults.standard[.showLevelOneShipInUpgradableList] }
85         set { UserDefaults.standard[.showLevelOneShipInUpgradableList] = newValue }
86     }
87     
88     @objc var showsExcludedShipInUpgradableList: Bool {
89         
90         get { return UserDefaults.standard[.showsExcludedShipInUpgradableList] }
91         set { UserDefaults.standard[.showsExcludedShipInUpgradableList] = newValue }
92     }
93     
94     var excludeShiIDs: [Int] {
95         
96         get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
97         set {
98             notifyChangeValue(forKey: #keyPath(filterPredicate)) {
99                 
100                 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
101                 UpgradableShipsWindowController.excludeShiIDsCache = newValue
102             }
103         }
104     }
105     
106     private var excludeShipIDsSaveURL: URL {
107         
108         return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
109     }
110     
111     override func windowDidLoad() {
112         
113         super.windowDidLoad()
114         
115         excludeShiIDsCache = excludeShiIDs
116     }
117     
118     private func includeShip(shipID: Int) {
119         
120         guard let index = excludeShiIDs.index(of: shipID) else {
121             
122             return
123         }
124         
125         excludeShiIDs.remove(at: index)
126     }
127     
128     private func excludeShip(shipID: Int) {
129         
130         excludeShiIDs.append(shipID)
131     }
132     
133     @IBAction func showHideShip(_ sender: AnyObject?) {
134         
135         let row = tableView.clickedRow
136         
137         guard let ships = shipsController.arrangedObjects as? [Ship] else {
138             
139             return
140         }
141         guard case 0..<ships.count = row else {
142             
143             return
144         }
145         
146         let shipID = ships[row].id
147         if isExcludeShipID(shipID) {
148             
149             includeShip(shipID: shipID)
150             
151         } else {
152             
153             excludeShip(shipID: shipID)
154         }
155     }
156     
157     override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
158         
159         guard let action = menuItem.action else {
160             
161             return false
162         }
163         
164         switch action {
165             
166         case #selector(UpgradableShipsWindowController.showHideShip(_:)):
167             
168             let row = tableView.clickedRow
169             
170             guard let ships = shipsController.arrangedObjects as? [Ship] else {
171                 
172                 return false
173             }
174             guard case 0..<ships.count = row else {
175                 
176                 return false
177             }
178             
179             if isExcludeShipID(ships[row].id) {
180                 
181                 menuItem.title = LocalizedStrings.showKanmusu.string
182                 
183             } else {
184                 
185                 menuItem.title = LocalizedStrings.hideKanmusu.string
186             }
187             
188             return true
189             
190         default: return false
191             
192         }
193     }
194 }