OSDN Git Service

CoreDataエンティティのクラスをエンティティ名と同じにした
[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.managedObjectContext
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 ["showLevelOneShipInUpgradableList", "showsExcludedShipInUpgradableList"]
25     }
26     
27     private var excludeShiIDsCache: [Int] {
28         get {
29             return UpgradableShipsWindowController.excludeShiIDsCache
30         }
31         set {
32             UpgradableShipsWindowController.excludeShiIDsCache = newValue
33         }
34     }
35     private func isExcludeShipID(_ shipID: Int) -> Bool {
36         return excludeShiIDsCache.contains(shipID)
37     }
38     
39     
40     @IBOutlet var contextualMenu: NSMenu!
41     @IBOutlet var tableView: NSTableView!
42     @IBOutlet var shipsController: NSArrayController!
43     
44     override var windowNibName: String! {
45         return "UpgradableShipsWindowController"
46     }
47     
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")
53         }
54         if showsExcludedShipInUpgradableList == false,
55             excludeShiIDs.count != 0 {
56             excludeShip = NSPredicate(format: "NOT id IN %@", excludeShiIDs)
57         }
58         if let filterPredicate = filterPredicate,
59             let excludeShip = excludeShip {
60             return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
61         }
62         if let filterPredicate = filterPredicate { return filterPredicate }
63         if let excludeShip = excludeShip { return excludeShip }
64         
65         return nil
66     }
67     
68     var showLevelOneShipInUpgradableList: Bool {
69         get {
70             return UserDefaults.standard.showLevelOneShipInUpgradableList
71         }
72         set {
73             UserDefaults.standard.showLevelOneShipInUpgradableList = newValue
74         }
75     }
76     var showsExcludedShipInUpgradableList: Bool {
77         get {
78             return UserDefaults.standard.showsExcludedShipInUpgradableList
79         }
80         set {
81             UserDefaults.standard.showsExcludedShipInUpgradableList = newValue
82         }
83     }
84     var excludeShiIDs: [Int] {
85         get {
86             return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? []
87         }
88         set {
89             willChangeValue(forKey: "filterPredicate")
90             (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
91             UpgradableShipsWindowController.excludeShiIDsCache = newValue
92             didChangeValue(forKey: "filterPredicate")
93         }
94     }
95     private var excludeShipIDsSaveURL: URL {
96         return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
97     }
98     
99     override func windowDidLoad() {
100         super.windowDidLoad()
101         
102         excludeShiIDsCache = excludeShiIDs
103     }
104     
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
110     }
111     private func excludeShip(shipID: Int) {
112         var array = excludeShiIDs
113         array.append(shipID)
114         excludeShiIDs = array
115     }
116     
117     @IBAction func showHideShip(_ sender: AnyObject?) {
118         let row = tableView.clickedRow
119         guard let ships = shipsController.arrangedObjects as? [Ship],
120             0..<ships.count ~= row
121             else { return }
122         let shipID = ships[row].id
123         if isExcludeShipID(shipID) {
124             includeShip(shipID: shipID)
125         } else {
126             excludeShip(shipID: shipID)
127         }
128     }
129     
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")
139             } else {
140                 menuItem.title = NSLocalizedString("Hide Kanmusu", comment: "UpgradableShipsWindowController menu item")
141             }
142             
143             return true
144         }
145         
146         return false
147     }
148 }