OSDN Git Service

Squashed commit of the following: Swiftに変換した
[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 && excludeShiIDs.count != 0 {
55             excludeShip = NSPredicate(format: "NOT id IN %@", argumentArray: [excludeShiIDs])
56         }
57         if filterPredicate != nil && excludeShip != nil {
58             return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate!, excludeShip!])
59         }
60         if filterPredicate != nil { return filterPredicate }
61         if excludeShip != nil { return excludeShip }
62         
63         return nil
64     }
65     
66     var showLevelOneShipInUpgradableList: Bool {
67         get {
68             return UserDefaults.standard.showLevelOneShipInUpgradableList
69         }
70         set {
71             UserDefaults.standard.showLevelOneShipInUpgradableList = newValue
72         }
73     }
74     var showsExcludedShipInUpgradableList: Bool {
75         get {
76             return UserDefaults.standard.showsExcludedShipInUpgradableList
77         }
78         set {
79             UserDefaults.standard.showsExcludedShipInUpgradableList = newValue
80         }
81     }
82     var excludeShiIDs: [Int] {
83         get {
84             return (NSArray(contentsOf: excludeShipIDsSaveURL) as? [Int]) ?? []
85         }
86         set {
87             willChangeValue(forKey: "filterPredicate")
88             (newValue as NSArray).write(to: excludeShipIDsSaveURL, atomically: true)
89             UpgradableShipsWindowController.excludeShiIDsCache = newValue
90             didChangeValue(forKey: "filterPredicate")
91         }
92     }
93     private var excludeShipIDsSaveURL: URL {
94         return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
95     }
96     
97     override func windowDidLoad() {
98         super.windowDidLoad()
99         
100         excludeShiIDsCache = excludeShiIDs
101     }
102     
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
108     }
109     private func excludeShip(shipID: Int) {
110         var array = excludeShiIDs
111         array.append(shipID)
112         excludeShiIDs = array
113     }
114     
115     @IBAction func showHideShip(_ sender: AnyObject?) {
116         let row = tableView.clickedRow
117         guard row != -1,
118             let a = shipsController.arrangedObjects as? NSArray,
119             let ship = a.object(at: row) as? KCShipObject
120             else { return }
121         let shipID = ship.id
122         if isExcludeShipID(shipID) {
123             includeShip(shipID: shipID)
124         } else {
125             excludeShip(shipID: shipID)
126         }
127     }
128     
129     override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
130         if menuItem.action == .showHideShip {
131             let row = tableView.clickedRow
132             guard row != -1,
133                 let a = shipsController.arrangedObjects as? NSArray,
134                 let ship = a.object(at: row) as? KCShipObject
135                 else { return false }
136             let shipID = ship.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 }