OSDN Git Service

Doutakuを導入
[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 private var contextualMenu: NSMenu!
45     @IBOutlet private var tableView: NSTableView!
46     @IBOutlet private 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         guard let index = excludeShiIDs.index(of: shipID) else { return }
120         
121         excludeShiIDs.remove(at: index)
122     }
123     
124     private func excludeShip(shipID: Int) {
125         
126         excludeShiIDs.append(shipID)
127     }
128     
129     @IBAction func showHideShip(_ sender: AnyObject?) {
130         
131         let row = tableView.clickedRow
132         
133         guard let ships = shipsController.arrangedObjects as? [Ship] else { return }
134         guard case 0..<ships.count = row else { return }
135         
136         let shipID = ships[row].id
137         if isExcludeShipID(shipID) {
138             
139             includeShip(shipID: shipID)
140             
141         } else {
142             
143             excludeShip(shipID: shipID)
144         }
145     }
146     
147     override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
148         
149         guard let action = menuItem.action else { return false }
150         
151         switch action {
152             
153         case #selector(UpgradableShipsWindowController.showHideShip(_:)):
154             
155             let row = tableView.clickedRow
156             
157             guard let ships = shipsController.arrangedObjects as? [Ship] else { return false }
158             guard case 0..<ships.count = row else { return false }
159             
160             if isExcludeShipID(ships[row].id) {
161                 
162                 menuItem.title = LocalizedStrings.showKanmusu.string
163                 
164             } else {
165                 
166                 menuItem.title = LocalizedStrings.hideKanmusu.string
167             }
168             
169             return true
170             
171         default: return false
172         }
173     }
174 }