2 // UpgradableShipsWindowController.swift
5 // Created by Hori,Masaki on 2016/12/18.
6 // Copyright © 2016年 Hori,Masaki. All rights reserved.
11 private extension Selector {
13 static let showHideShip = #selector(UpgradableShipsWindowController.showHideShip(_:))
16 final class UpgradableShipsWindowController: NSWindowController {
18 @objc let managedObjectContext = ServerDataStore.default.context
20 private static var excludeShiIDsCache: [Int] = []
22 class func isExcludeShipID(_ shipID: Int) -> Bool {
24 return excludeShiIDsCache.contains(shipID)
27 @objc override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
31 case #keyPath(filterPredicate): return [#keyPath(showLevelOneShipInUpgradableList), #keyPath(showsExcludedShipInUpgradableList)]
37 private var excludeShiIDsCache: [Int] {
39 get { return UpgradableShipsWindowController.excludeShiIDsCache }
40 set { UpgradableShipsWindowController.excludeShiIDsCache = newValue }
43 private func isExcludeShipID(_ shipID: Int) -> Bool {
45 return excludeShiIDsCache.contains(shipID)
49 @IBOutlet var contextualMenu: NSMenu!
50 @IBOutlet var tableView: NSTableView!
51 @IBOutlet var shipsController: NSArrayController!
53 override var windowNibName: NSNib.Name {
55 return .nibName(instanceOf: self)
58 @objc dynamic var filterPredicate: NSPredicate? {
60 var filterPredicate: NSPredicate?
61 var excludeShip: NSPredicate?
63 if showLevelOneShipInUpgradableList == false {
65 filterPredicate = NSPredicate(format: "lv != 1")
68 if showsExcludedShipInUpgradableList == false,
69 excludeShiIDs.count != 0 {
71 excludeShip = NSPredicate(format: "NOT id IN %@", excludeShiIDs)
74 if let filterPredicate = filterPredicate,
75 let excludeShip = excludeShip {
77 return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
80 if let filterPredicate = filterPredicate { return filterPredicate }
81 if let excludeShip = excludeShip { return excludeShip }
86 @objc var showLevelOneShipInUpgradableList: Bool {
88 get { return UserDefaults.standard[.showLevelOneShipInUpgradableList] }
89 set { UserDefaults.standard[.showLevelOneShipInUpgradableList] = newValue }
92 @objc var showsExcludedShipInUpgradableList: Bool {
94 get { return UserDefaults.standard[.showsExcludedShipInUpgradableList] }
95 set { UserDefaults.standard[.showsExcludedShipInUpgradableList] = newValue }
98 var excludeShiIDs: [Int] {
100 get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
102 notifyChangeValue(forKey: #keyPath(filterPredicate)) {
104 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
105 UpgradableShipsWindowController.excludeShiIDsCache = newValue
110 private var excludeShipIDsSaveURL: URL {
112 return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
115 override func windowDidLoad() {
117 super.windowDidLoad()
119 excludeShiIDsCache = excludeShiIDs
122 private func includeShip(shipID: Int) {
124 var array = excludeShiIDs
126 guard let index = array.index(of: shipID) else { return }
128 array.remove(at: index)
129 excludeShiIDs = array
132 private func excludeShip(shipID: Int) {
134 var array = excludeShiIDs
136 excludeShiIDs = array
139 @IBAction func showHideShip(_ sender: AnyObject?) {
141 let row = tableView.clickedRow
143 guard let ships = shipsController.arrangedObjects as? [Ship] else { return }
144 guard case 0..<ships.count = row else { return }
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 if menuItem.action == .showHideShip {
161 let row = tableView.clickedRow
163 guard let ships = shipsController.arrangedObjects as? [Ship] else { return false }
164 guard case 0..<ships.count = row else { return false }
166 let shipID = ships[row].id
167 if isExcludeShipID(shipID) {
169 menuItem.title = LocalizedStrings.showKanmusu.string
173 menuItem.title = LocalizedStrings.hideKanmusu.string