2 // UpgradableShipsWindowController.swift
5 // Created by Hori,Masaki on 2016/12/18.
6 // Copyright © 2016年 Hori,Masaki. All rights reserved.
11 final class UpgradableShipsWindowController: NSWindowController {
13 @objc let managedObjectContext = ServerDataStore.default.context
15 private static var excludeShiIDsCache: [Int] = []
17 class func isExcludeShipID(_ shipID: Int) -> Bool {
19 return excludeShiIDsCache.contains(shipID)
22 @objc override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
26 case #keyPath(filterPredicate): return [#keyPath(showLevelOneShipInUpgradableList), #keyPath(showsExcludedShipInUpgradableList)]
32 private var excludeShiIDsCache: [Int] {
34 get { return UpgradableShipsWindowController.excludeShiIDsCache }
35 set { UpgradableShipsWindowController.excludeShiIDsCache = newValue }
38 private func isExcludeShipID(_ shipID: Int) -> Bool {
40 return excludeShiIDsCache.contains(shipID)
44 @IBOutlet var contextualMenu: NSMenu!
45 @IBOutlet var tableView: NSTableView!
46 @IBOutlet var shipsController: NSArrayController!
48 override var windowNibName: NSNib.Name {
50 return .nibName(instanceOf: self)
53 @objc dynamic var filterPredicate: NSPredicate? {
55 var filterPredicate: NSPredicate?
56 var excludeShip: NSPredicate?
58 if showLevelOneShipInUpgradableList == false {
60 filterPredicate = NSPredicate(#keyPath(Ship.lv), notEqual: 1)
63 if showsExcludedShipInUpgradableList == false,
64 excludeShiIDs.count != 0 {
66 excludeShip = .not(NSPredicate(#keyPath(Ship.id), valuesIn: excludeShiIDs))
69 if let filterPredicate = filterPredicate,
70 let excludeShip = excludeShip {
72 return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
75 if let filterPredicate = filterPredicate { return filterPredicate }
76 if let excludeShip = excludeShip { return excludeShip }
81 @objc var showLevelOneShipInUpgradableList: Bool {
83 get { return UserDefaults.standard[.showLevelOneShipInUpgradableList] }
84 set { UserDefaults.standard[.showLevelOneShipInUpgradableList] = newValue }
87 @objc var showsExcludedShipInUpgradableList: Bool {
89 get { return UserDefaults.standard[.showsExcludedShipInUpgradableList] }
90 set { UserDefaults.standard[.showsExcludedShipInUpgradableList] = newValue }
93 var excludeShiIDs: [Int] {
95 get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
97 notifyChangeValue(forKey: #keyPath(filterPredicate)) {
99 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
100 UpgradableShipsWindowController.excludeShiIDsCache = newValue
105 private var excludeShipIDsSaveURL: URL {
107 return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
110 override func windowDidLoad() {
112 super.windowDidLoad()
114 excludeShiIDsCache = excludeShiIDs
117 private func includeShip(shipID: Int) {
119 var array = excludeShiIDs
121 guard let index = array.index(of: shipID) else { return }
123 array.remove(at: index)
124 excludeShiIDs = array
127 private func excludeShip(shipID: Int) {
129 var array = excludeShiIDs
131 excludeShiIDs = array
134 @IBAction func showHideShip(_ sender: AnyObject?) {
136 let row = tableView.clickedRow
138 guard let ships = shipsController.arrangedObjects as? [Ship] else { return }
139 guard case 0..<ships.count = row else { return }
141 let shipID = ships[row].id
142 if isExcludeShipID(shipID) {
144 includeShip(shipID: shipID)
148 excludeShip(shipID: shipID)
152 override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
154 guard let action = menuItem.action else { return false }
158 case #selector(UpgradableShipsWindowController.showHideShip(_:)):
160 let row = tableView.clickedRow
162 guard let ships = shipsController.arrangedObjects as? [Ship] else { return false }
163 guard case 0..<ships.count = row else { return false }
165 if isExcludeShipID(ships[row].id) {
167 menuItem.title = LocalizedStrings.showKanmusu.string
171 menuItem.title = LocalizedStrings.hideKanmusu.string
176 default: return false