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 && excludeShiIDs.count != 0 {
55 excludeShip = NSPredicate(format: "NOT id IN %@", argumentArray: [excludeShiIDs])
57 if filterPredicate != nil && excludeShip != nil {
58 return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate!, excludeShip!])
60 if filterPredicate != nil { return filterPredicate }
61 if excludeShip != nil { return excludeShip }
66 var showLevelOneShipInUpgradableList: Bool {
68 return UserDefaults.standard.showLevelOneShipInUpgradableList
71 UserDefaults.standard.showLevelOneShipInUpgradableList = newValue
74 var showsExcludedShipInUpgradableList: Bool {
76 return UserDefaults.standard.showsExcludedShipInUpgradableList
79 UserDefaults.standard.showsExcludedShipInUpgradableList = newValue
82 var excludeShiIDs: [Int] {
84 return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? []
87 willChangeValue(forKey: "filterPredicate")
88 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
89 UpgradableShipsWindowController.excludeShiIDsCache = newValue
90 didChangeValue(forKey: "filterPredicate")
93 private var excludeShipIDsSaveURL: URL {
94 return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
97 override func windowDidLoad() {
100 excludeShiIDsCache = excludeShiIDs
103 private func includeShip(shipID: Int) {
104 var array = excludeShiIDs
105 guard let index = array.index(of: shipID) else { return }
106 array.remove(at: index)
107 excludeShiIDs = array
109 private func excludeShip(shipID: Int) {
110 var array = excludeShiIDs
112 excludeShiIDs = array
115 @IBAction func showHideShip(_ sender: AnyObject?) {
116 let row = tableView.clickedRow
118 let a = shipsController.arrangedObjects as? NSArray,
119 let ship = a.object(at: row) as? KCShipObject
122 if isExcludeShipID(shipID) {
123 includeShip(shipID: shipID)
125 excludeShip(shipID: shipID)
129 override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
130 if menuItem.action == .showHideShip {
131 let row = tableView.clickedRow
133 let a = shipsController.arrangedObjects as? NSArray,
134 let ship = a.object(at: row) as? KCShipObject
135 else { return false }
137 if isExcludeShipID(shipID) {
138 menuItem.title = NSLocalizedString("Show Kanmusu", comment: "UpgradableShipsWindowController menu item")
140 menuItem.title = NSLocalizedString("Hide Kanmusu", comment: "UpgradableShipsWindowController menu item")