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)]
33 private var excludeShiIDsCache: [Int] {
35 get { return UpgradableShipsWindowController.excludeShiIDsCache }
36 set { UpgradableShipsWindowController.excludeShiIDsCache = newValue }
39 private func isExcludeShipID(_ shipID: Int) -> Bool {
41 return excludeShiIDsCache.contains(shipID)
45 @IBOutlet private var contextualMenu: NSMenu!
46 @IBOutlet private var tableView: NSTableView!
47 @IBOutlet private var shipsController: NSArrayController!
49 override var windowNibName: NSNib.Name {
51 return .nibName(instanceOf: self)
54 @objc dynamic var filterPredicate: NSPredicate? {
56 var filterPredicate: NSPredicate?
57 var excludeShip: NSPredicate?
59 if showLevelOneShipInUpgradableList == false {
61 filterPredicate = NSPredicate(#keyPath(Ship.lv), notEqual: 1)
64 if showsExcludedShipInUpgradableList == false,
65 excludeShiIDs.count != 0 {
67 excludeShip = .not(NSPredicate(#keyPath(Ship.id), valuesIn: excludeShiIDs))
70 if let filterPredicate = filterPredicate,
71 let excludeShip = excludeShip {
73 return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
76 if let filterPredicate = filterPredicate {
78 return filterPredicate
80 if let excludeShip = excludeShip {
88 @objc var showLevelOneShipInUpgradableList: Bool {
90 get { return UserDefaults.standard[.showLevelOneShipInUpgradableList] }
91 set { UserDefaults.standard[.showLevelOneShipInUpgradableList] = newValue }
94 @objc var showsExcludedShipInUpgradableList: Bool {
96 get { return UserDefaults.standard[.showsExcludedShipInUpgradableList] }
97 set { UserDefaults.standard[.showsExcludedShipInUpgradableList] = newValue }
100 var excludeShiIDs: [Int] {
102 get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
104 notifyChangeValue(forKey: #keyPath(filterPredicate)) {
106 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
107 UpgradableShipsWindowController.excludeShiIDsCache = newValue
112 private var excludeShipIDsSaveURL: URL {
114 return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
117 override func windowDidLoad() {
119 super.windowDidLoad()
121 excludeShiIDsCache = excludeShiIDs
124 private func includeShip(shipID: Int) {
126 guard let index = excludeShiIDs.index(of: shipID) else {
131 excludeShiIDs.remove(at: index)
134 private func excludeShip(shipID: Int) {
136 excludeShiIDs.append(shipID)
139 @IBAction func showHideShip(_ sender: AnyObject?) {
141 let row = tableView.clickedRow
143 guard let ships = shipsController.arrangedObjects as? [Ship] else {
147 guard case 0..<ships.count = row else {
152 let shipID = ships[row].id
153 if isExcludeShipID(shipID) {
155 includeShip(shipID: shipID)
159 excludeShip(shipID: shipID)
163 override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
165 guard let action = menuItem.action else {
172 case #selector(UpgradableShipsWindowController.showHideShip(_:)):
174 let row = tableView.clickedRow
176 guard let ships = shipsController.arrangedObjects as? [Ship] else {
180 guard case 0..<ships.count = row else {
185 if isExcludeShipID(ships[row].id) {
187 menuItem.title = LocalizedStrings.showKanmusu.string
191 menuItem.title = LocalizedStrings.hideKanmusu.string
196 default: return false