OSDN Git Service

UserDefaults関連をCotoEditorから取り込んだ
[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     
13     static let showHideShip = #selector(UpgradableShipsWindowController.showHideShip(_:))
14 }
15
16 final class UpgradableShipsWindowController: NSWindowController {
17     
18     let managedObjectContext = ServerDataStore.default.context
19     
20     private static var excludeShiIDsCache: [Int] = []
21     
22     class func isExcludeShipID(_ shipID: Int) -> Bool {
23         
24         return excludeShiIDsCache.contains(shipID)
25     }
26     
27     class func keyPathsForValuesAffectingFilterPredicate() -> Set<String> {
28         
29         return [#keyPath(showLevelOneShipInUpgradableList), #keyPath(showsExcludedShipInUpgradableList)]
30     }
31     
32     private var excludeShiIDsCache: [Int] {
33         
34         get { return UpgradableShipsWindowController.excludeShiIDsCache }
35         set { UpgradableShipsWindowController.excludeShiIDsCache = newValue }
36     }
37     
38     private func isExcludeShipID(_ shipID: Int) -> Bool {
39         
40         return excludeShiIDsCache.contains(shipID)
41     }
42     
43     
44     @IBOutlet var contextualMenu: NSMenu!
45     @IBOutlet var tableView: NSTableView!
46     @IBOutlet var shipsController: NSArrayController!
47     
48     override var windowNibName: String! {
49         
50         return "UpgradableShipsWindowController"
51     }
52     
53     dynamic var filterPredicate: NSPredicate? {
54         
55         var filterPredicate: NSPredicate?
56         var excludeShip: NSPredicate?
57         
58         if showLevelOneShipInUpgradableList == false {
59             
60             filterPredicate = NSPredicate(format: "lv != 1")
61         }
62         
63         if showsExcludedShipInUpgradableList == false,
64             excludeShiIDs.count != 0 {
65             
66             excludeShip = NSPredicate(format: "NOT id IN %@", excludeShiIDs)
67         }
68         
69         if let filterPredicate = filterPredicate,
70             let excludeShip = excludeShip {
71             
72             return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
73         }
74         
75         if let filterPredicate = filterPredicate { return filterPredicate }
76         if let excludeShip = excludeShip { return excludeShip }
77         
78         return nil
79     }
80     
81     var showLevelOneShipInUpgradableList: Bool {
82         
83         get { return UserDefaults.standard[.showLevelOneShipInUpgradableList] }
84         set { UserDefaults.standard[.showLevelOneShipInUpgradableList] = newValue }
85     }
86     
87     var showsExcludedShipInUpgradableList: Bool {
88         
89         get { return UserDefaults.standard[.showsExcludedShipInUpgradableList] }
90         set { UserDefaults.standard[.showsExcludedShipInUpgradableList] = newValue }
91     }
92     
93     var excludeShiIDs: [Int] {
94         
95         get { return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? [] }
96         set {
97             notifyChangeValue(forKey: #keyPath(filterPredicate)) {
98                 
99                 (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
100                 UpgradableShipsWindowController.excludeShiIDsCache = newValue
101             }
102         }
103     }
104     
105     private var excludeShipIDsSaveURL: URL {
106         
107         return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
108     }
109     
110     override func windowDidLoad() {
111         
112         super.windowDidLoad()
113         
114         excludeShiIDsCache = excludeShiIDs
115     }
116     
117     private func includeShip(shipID: Int) {
118         
119         var array = excludeShiIDs
120         
121         guard let index = array.index(of: shipID) else { return }
122         
123         array.remove(at: index)
124         excludeShiIDs = array
125     }
126     
127     private func excludeShip(shipID: Int) {
128         
129         var array = excludeShiIDs
130         array.append(shipID)
131         excludeShiIDs = array
132     }
133     
134     @IBAction func showHideShip(_ sender: AnyObject?) {
135         
136         let row = tableView.clickedRow
137         
138         guard let ships = shipsController.arrangedObjects as? [Ship],
139             case 0..<ships.count = row
140             else { return }
141         
142         let shipID = ships[row].id
143         if isExcludeShipID(shipID) {
144             
145             includeShip(shipID: shipID)
146             
147         } else {
148             
149             excludeShip(shipID: shipID)
150         }
151     }
152     
153     override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
154         
155         if menuItem.action == .showHideShip {
156             
157             let row = tableView.clickedRow
158             
159             guard let ships = shipsController.arrangedObjects as? [Ship],
160                 case 0..<ships.count = row
161                 else { return false }
162             
163             let shipID = ships[row].id
164             if isExcludeShipID(shipID) {
165                 
166                 menuItem.title = NSLocalizedString("Show Kanmusu", comment: "UpgradableShipsWindowController menu item")
167                 
168             } else {
169                 
170                 menuItem.title = NSLocalizedString("Hide Kanmusu", comment: "UpgradableShipsWindowController menu item")
171             }
172             
173             return true
174         }
175         
176         return false
177     }
178 }