2 // UpgradableShipsWindowController.swift
5 // Created by Hori,Masaki on 2016/12/18.
6 // Copyright © 2016年 Hori,Masaki. All rights reserved.
11 private extension Selector {
12 static let showHideShip = #selector(UpgradableShipsWindowController.showHideShip(_:))
15 class UpgradableShipsWindowController: NSWindowController {
16 let managedObjectContext = ServerDataStore.default.managedObjectContext
17 private static var excludeShiIDsCache: [Int] = []
19 class func isExcludeShipID(_ shipID: Int) -> Bool {
20 return excludeShiIDsCache.contains(shipID)
23 class func keyPathsForValuesAffectingFilterPredicate() -> Set<String> {
24 return ["showLevelOneShipInUpgradableList", "showsExcludedShipInUpgradableList"]
27 private var excludeShiIDsCache: [Int] {
29 return UpgradableShipsWindowController.excludeShiIDsCache
32 UpgradableShipsWindowController.excludeShiIDsCache = newValue
35 private func isExcludeShipID(_ shipID: Int) -> Bool {
36 return excludeShiIDsCache.contains(shipID)
40 @IBOutlet var contextualMenu: NSMenu!
41 @IBOutlet var tableView: NSTableView!
42 @IBOutlet var shipsController: NSArrayController!
44 override var windowNibName: String! {
45 return "UpgradableShipsWindowController"
48 dynamic var filterPredicate: NSPredicate? {
49 var filterPredicate: NSPredicate? = nil
50 var excludeShip: NSPredicate? = nil
51 if showLevelOneShipInUpgradableList == false {
52 filterPredicate = NSPredicate(format: "lv != 1")
54 if showsExcludedShipInUpgradableList == false,
55 excludeShiIDs.count != 0 {
56 excludeShip = NSPredicate(format: "NOT id IN %@", excludeShiIDs)
58 if let filterPredicate = filterPredicate,
59 let excludeShip = excludeShip {
60 return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
62 if let filterPredicate = filterPredicate { return filterPredicate }
63 if let excludeShip = excludeShip { return excludeShip }
68 var showLevelOneShipInUpgradableList: Bool {
70 return UserDefaults.standard.showLevelOneShipInUpgradableList
73 UserDefaults.standard.showLevelOneShipInUpgradableList = newValue
76 var showsExcludedShipInUpgradableList: Bool {
78 return UserDefaults.standard.showsExcludedShipInUpgradableList
81 UserDefaults.standard.showsExcludedShipInUpgradableList = newValue
84 var excludeShiIDs: [Int] {
86 return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? []
89 willChangeValue(forKey: "filterPredicate")
90 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
91 UpgradableShipsWindowController.excludeShiIDsCache = newValue
92 didChangeValue(forKey: "filterPredicate")
95 private var excludeShipIDsSaveURL: URL {
96 return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
99 override func windowDidLoad() {
100 super.windowDidLoad()
102 excludeShiIDsCache = excludeShiIDs
105 private func includeShip(shipID: Int) {
106 var array = excludeShiIDs
107 guard let index = array.index(of: shipID) else { return }
108 array.remove(at: index)
109 excludeShiIDs = array
111 private func excludeShip(shipID: Int) {
112 var array = excludeShiIDs
114 excludeShiIDs = array
117 @IBAction func showHideShip(_ sender: AnyObject?) {
118 let row = tableView.clickedRow
119 guard let ships = shipsController.arrangedObjects as? [Ship],
120 0..<ships.count ~= row
122 let shipID = ships[row].id
123 if isExcludeShipID(shipID) {
124 includeShip(shipID: shipID)
126 excludeShip(shipID: shipID)
130 override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
131 if menuItem.action == .showHideShip {
132 let row = tableView.clickedRow
133 guard let ships = shipsController.arrangedObjects as? [Ship],
134 0..<ships.count ~= row
135 else { return false }
136 let shipID = ships[row].id
137 if isExcludeShipID(shipID) {
138 menuItem.title = NSLocalizedString("Show Kanmusu", comment: "UpgradableShipsWindowController menu item")
140 menuItem.title = NSLocalizedString("Hide Kanmusu", comment: "UpgradableShipsWindowController menu item")