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 levelOnePredicate: NSPredicate?
57 var excludeShip: NSPredicate?
59 if showLevelOneShipInUpgradableList == false {
61 levelOnePredicate = NSPredicate(#keyPath(Ship.lv), notEqual: 1)
64 if showsExcludedShipInUpgradableList == false,
65 excludeShiIDsCache.count != 0 {
67 excludeShip = .not(NSPredicate(#keyPath(Ship.id), valuesIn: excludeShiIDsCache))
70 switch (levelOnePredicate, excludeShip) {
72 case let (p0?, p1?): return p0.and(p1)
74 case let (p0?, _): return p0
76 case let (_, p1?): return p1
82 @objc var showLevelOneShipInUpgradableList: Bool {
84 get { return UserDefaults.standard[.showLevelOneShipInUpgradableList] }
85 set { UserDefaults.standard[.showLevelOneShipInUpgradableList] = newValue }
88 @objc var showsExcludedShipInUpgradableList: Bool {
90 get { return UserDefaults.standard[.showsExcludedShipInUpgradableList] }
91 set { UserDefaults.standard[.showsExcludedShipInUpgradableList] = newValue }
94 var excludeShiIDs: [Int] {
96 get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
98 notifyChangeValue(forKey: #keyPath(filterPredicate)) {
100 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
101 UpgradableShipsWindowController.excludeShiIDsCache = newValue
106 private var excludeShipIDsSaveURL: URL {
108 return ApplicationDirecrories.shared.support.appendingPathComponent("ExcludeShipIDs")
111 override func windowWillLoad() {
113 super.windowWillLoad()
115 excludeShiIDsCache = excludeShiIDs
118 private func includeShip(shipID: Int) {
120 guard let index = excludeShiIDs.index(of: shipID) else {
125 excludeShiIDs.remove(at: index)
128 private func excludeShip(shipID: Int) {
130 excludeShiIDs.append(shipID)
133 @IBAction func showHideShip(_ sender: AnyObject?) {
135 let row = tableView.clickedRow
137 guard let ships = shipsController.arrangedObjects as? [Ship] else {
141 guard case 0..<ships.count = row else {
146 let shipID = ships[row].id
147 if isExcludeShipID(shipID) {
149 includeShip(shipID: shipID)
153 excludeShip(shipID: shipID)
157 override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
159 guard let action = menuItem.action else {
166 case #selector(UpgradableShipsWindowController.showHideShip(_:)):
168 let row = tableView.clickedRow
170 guard let ships = shipsController.arrangedObjects as? [Ship] else {
174 guard case 0..<ships.count = row else {
179 if isExcludeShipID(ships[row].id) {
181 menuItem.title = LocalizedStrings.showKanmusu.string
185 menuItem.title = LocalizedStrings.hideKanmusu.string
190 default: return false