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.context
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 [#keyPath(showLevelOneShipInUpgradableList), #keyPath(showsExcludedShipInUpgradableList)]
27 private var excludeShiIDsCache: [Int] {
28 get { return UpgradableShipsWindowController.excludeShiIDsCache }
29 set { UpgradableShipsWindowController.excludeShiIDsCache = newValue }
31 private func isExcludeShipID(_ shipID: Int) -> Bool {
32 return excludeShiIDsCache.contains(shipID)
36 @IBOutlet var contextualMenu: NSMenu!
37 @IBOutlet var tableView: NSTableView!
38 @IBOutlet var shipsController: NSArrayController!
40 override var windowNibName: String! {
41 return "UpgradableShipsWindowController"
44 dynamic var filterPredicate: NSPredicate? {
45 var filterPredicate: NSPredicate?
46 var excludeShip: NSPredicate?
47 if showLevelOneShipInUpgradableList == false {
48 filterPredicate = NSPredicate(format: "lv != 1")
50 if showsExcludedShipInUpgradableList == false,
51 excludeShiIDs.count != 0 {
52 excludeShip = NSPredicate(format: "NOT id IN %@", excludeShiIDs)
54 if let filterPredicate = filterPredicate,
55 let excludeShip = excludeShip {
56 return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
58 if let filterPredicate = filterPredicate { return filterPredicate }
59 if let excludeShip = excludeShip { return excludeShip }
64 var showLevelOneShipInUpgradableList: Bool {
65 get { return UserDefaults.standard.showLevelOneShipInUpgradableList }
66 set { UserDefaults.standard.showLevelOneShipInUpgradableList = newValue }
68 var showsExcludedShipInUpgradableList: Bool {
69 get { return UserDefaults.standard.showsExcludedShipInUpgradableList }
70 set { UserDefaults.standard.showsExcludedShipInUpgradableList = newValue }
72 var excludeShiIDs: [Int] {
73 get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
75 notifyChangeValue(forKey: #keyPath(filterPredicate)) {
76 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
77 UpgradableShipsWindowController.excludeShiIDsCache = newValue
81 private var excludeShipIDsSaveURL: URL {
82 return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
85 override func windowDidLoad() {
88 excludeShiIDsCache = excludeShiIDs
91 private func includeShip(shipID: Int) {
92 var array = excludeShiIDs
93 guard let index = array.index(of: shipID) else { return }
94 array.remove(at: index)
97 private func excludeShip(shipID: Int) {
98 var array = excludeShiIDs
100 excludeShiIDs = array
103 @IBAction func showHideShip(_ sender: AnyObject?) {
104 let row = tableView.clickedRow
105 guard let ships = shipsController.arrangedObjects as? [Ship],
106 0..<ships.count ~= row
108 let shipID = ships[row].id
109 if isExcludeShipID(shipID) {
110 includeShip(shipID: shipID)
112 excludeShip(shipID: shipID)
116 override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
117 if menuItem.action == .showHideShip {
118 let row = tableView.clickedRow
119 guard let ships = shipsController.arrangedObjects as? [Ship],
120 0..<ships.count ~= row
121 else { return false }
122 let shipID = ships[row].id
123 if isExcludeShipID(shipID) {
124 menuItem.title = NSLocalizedString("Show Kanmusu", comment: "UpgradableShipsWindowController menu item")
126 menuItem.title = NSLocalizedString("Hide Kanmusu", comment: "UpgradableShipsWindowController menu item")