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 let managedObjectContext = ServerDataStore.default.context
20 private static var excludeShiIDsCache: [Int] = []
22 class func isExcludeShipID(_ shipID: Int) -> Bool {
24 return excludeShiIDsCache.contains(shipID)
27 class func keyPathsForValuesAffectingFilterPredicate() -> Set<String> {
29 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: String! {
50 return "UpgradableShipsWindowController"
53 dynamic var filterPredicate: NSPredicate? {
55 var filterPredicate: NSPredicate?
56 var excludeShip: NSPredicate?
58 if showLevelOneShipInUpgradableList == false {
60 filterPredicate = NSPredicate(format: "lv != 1")
63 if showsExcludedShipInUpgradableList == false,
64 excludeShiIDs.count != 0 {
66 excludeShip = NSPredicate(format: "NOT id IN %@", 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 var showLevelOneShipInUpgradableList: Bool {
83 get { return UserDefaults.standard[.showLevelOneShipInUpgradableList] }
84 set { UserDefaults.standard[.showLevelOneShipInUpgradableList] = newValue }
87 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],
139 case 0..<ships.count = row
142 let shipID = ships[row].id
143 if isExcludeShipID(shipID) {
145 includeShip(shipID: shipID)
149 excludeShip(shipID: shipID)
153 override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
155 if menuItem.action == .showHideShip {
157 let row = tableView.clickedRow
159 guard let ships = shipsController.arrangedObjects as? [Ship],
160 case 0..<ships.count = row
161 else { return false }
163 let shipID = ships[row].id
164 if isExcludeShipID(shipID) {
166 menuItem.title = NSLocalizedString("Show Kanmusu", comment: "UpgradableShipsWindowController menu item")
170 menuItem.title = NSLocalizedString("Hide Kanmusu", comment: "UpgradableShipsWindowController menu item")