OSDN Git Service

#keyPathが使用可能な部分で全て使用するようにした
[kcd/KCD.git] / KCD / UpgradableShipsWindowController.swift
1 //
2 //  UpgradableShipsWindowController.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2016/12/18.
6 //  Copyright © 2016年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 private extension Selector {
12     static let showHideShip = #selector(UpgradableShipsWindowController.showHideShip(_:))
13 }
14
15 class UpgradableShipsWindowController: NSWindowController {
16     let managedObjectContext = ServerDataStore.default.context
17     private static var excludeShiIDsCache: [Int] = []
18     
19     class func isExcludeShipID(_ shipID: Int) -> Bool {
20         return excludeShiIDsCache.contains(shipID)
21     }
22     
23     class func keyPathsForValuesAffectingFilterPredicate() -> Set<String> {
24         return [#keyPath(showLevelOneShipInUpgradableList), #keyPath(showsExcludedShipInUpgradableList)]
25     }
26     
27     private var excludeShiIDsCache: [Int] {
28         get { return UpgradableShipsWindowController.excludeShiIDsCache }
29         set { UpgradableShipsWindowController.excludeShiIDsCache = newValue }
30     }
31     private func isExcludeShipID(_ shipID: Int) -> Bool {
32         return excludeShiIDsCache.contains(shipID)
33     }
34     
35     
36     @IBOutlet var contextualMenu: NSMenu!
37     @IBOutlet var tableView: NSTableView!
38     @IBOutlet var shipsController: NSArrayController!
39     
40     override var windowNibName: String! {
41         return "UpgradableShipsWindowController"
42     }
43     
44     dynamic var filterPredicate: NSPredicate? {
45         var filterPredicate: NSPredicate?
46         var excludeShip: NSPredicate?
47         if showLevelOneShipInUpgradableList == false {
48             filterPredicate = NSPredicate(format: "lv != 1")
49         }
50         if showsExcludedShipInUpgradableList == false,
51             excludeShiIDs.count != 0 {
52             excludeShip = NSPredicate(format: "NOT id IN %@", excludeShiIDs)
53         }
54         if let filterPredicate = filterPredicate,
55             let excludeShip = excludeShip {
56             return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
57         }
58         if let filterPredicate = filterPredicate { return filterPredicate }
59         if let excludeShip = excludeShip { return excludeShip }
60         
61         return nil
62     }
63     
64     var showLevelOneShipInUpgradableList: Bool {
65         get { return UserDefaults.standard.showLevelOneShipInUpgradableList }
66         set { UserDefaults.standard.showLevelOneShipInUpgradableList = newValue }
67     }
68     var showsExcludedShipInUpgradableList: Bool {
69         get { return UserDefaults.standard.showsExcludedShipInUpgradableList }
70         set { UserDefaults.standard.showsExcludedShipInUpgradableList = newValue }
71     }
72     var excludeShiIDs: [Int] {
73         get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
74         set {
75             notifyChangeValue(forKey: #keyPath(filterPredicate)) {
76                 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
77                 UpgradableShipsWindowController.excludeShiIDsCache = newValue
78             }
79         }
80     }
81     private var excludeShipIDsSaveURL: URL {
82         return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
83     }
84     
85     override func windowDidLoad() {
86         super.windowDidLoad()
87         
88         excludeShiIDsCache = excludeShiIDs
89     }
90     
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)
95         excludeShiIDs = array
96     }
97     private func excludeShip(shipID: Int) {
98         var array = excludeShiIDs
99         array.append(shipID)
100         excludeShiIDs = array
101     }
102     
103     @IBAction func showHideShip(_ sender: AnyObject?) {
104         let row = tableView.clickedRow
105         guard let ships = shipsController.arrangedObjects as? [Ship],
106             0..<ships.count ~= row
107             else { return }
108         let shipID = ships[row].id
109         if isExcludeShipID(shipID) {
110             includeShip(shipID: shipID)
111         } else {
112             excludeShip(shipID: shipID)
113         }
114     }
115     
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")
125             } else {
126                 menuItem.title = NSLocalizedString("Hide Kanmusu", comment: "UpgradableShipsWindowController menu item")
127             }
128             
129             return true
130         }
131         
132         return false
133     }
134 }