OSDN Git Service

staticプロパティをインスタンスプロパティに変更
[kcd/KCD.git] / KCD / UpgradableShipsWindowController.swift
index ab810a2..e721e6c 100644 (file)
@@ -8,11 +8,6 @@
 
 import Cocoa
 
-private extension Selector {
-    
-    static let showHideShip = #selector(UpgradableShipsWindowController.showHideShip(_:))
-}
-
 final class UpgradableShipsWindowController: NSWindowController {
     
     @objc let managedObjectContext = ServerDataStore.default.context
@@ -31,6 +26,7 @@ final class UpgradableShipsWindowController: NSWindowController {
         case #keyPath(filterPredicate): return [#keyPath(showLevelOneShipInUpgradableList), #keyPath(showsExcludedShipInUpgradableList)]
         
         default: return []
+            
         }
     }
     
@@ -46,9 +42,9 @@ final class UpgradableShipsWindowController: NSWindowController {
     }
     
     
-    @IBOutlet var contextualMenu: NSMenu!
-    @IBOutlet var tableView: NSTableView!
-    @IBOutlet var shipsController: NSArrayController!
+    @IBOutlet private var contextualMenu: NSMenu!
+    @IBOutlet private var tableView: NSTableView!
+    @IBOutlet private var shipsController: NSArrayController!
     
     override var windowNibName: NSNib.Name {
         
@@ -57,30 +53,30 @@ final class UpgradableShipsWindowController: NSWindowController {
     
     @objc dynamic var filterPredicate: NSPredicate? {
         
-        var filterPredicate: NSPredicate?
+        var levelOnePredicate: NSPredicate?
         var excludeShip: NSPredicate?
         
         if showLevelOneShipInUpgradableList == false {
             
-            filterPredicate = NSPredicate(format: "lv != 1")
+            levelOnePredicate = NSPredicate(#keyPath(Ship.lv), notEqual: 1)
         }
         
         if showsExcludedShipInUpgradableList == false,
-            excludeShiIDs.count != 0 {
+            excludeShiIDsCache.count != 0 {
             
-            excludeShip = NSPredicate(format: "NOT id IN %@", excludeShiIDs)
+            excludeShip = .not(NSPredicate(#keyPath(Ship.id), valuesIn: excludeShiIDsCache))
         }
         
-        if let filterPredicate = filterPredicate,
-            let excludeShip = excludeShip {
+        switch (levelOnePredicate, excludeShip) {
+            
+        case let (p0?, p1?): return p0.and(p1)
+            
+        case let (p0?, _): return p0
+            
+        case let (_, p1?): return p1
             
-            return NSCompoundPredicate(andPredicateWithSubpredicates: [filterPredicate, excludeShip])
+        default: return nil
         }
-        
-        if let filterPredicate = filterPredicate { return filterPredicate }
-        if let excludeShip = excludeShip { return excludeShip }
-        
-        return nil
     }
     
     @objc var showLevelOneShipInUpgradableList: Bool {
@@ -109,39 +105,43 @@ final class UpgradableShipsWindowController: NSWindowController {
     
     private var excludeShipIDsSaveURL: URL {
         
-        return ApplicationDirecrories.support.appendingPathComponent("ExcludeShipIDs")
+        return ApplicationDirecrories.shared.support.appendingPathComponent("ExcludeShipIDs")
     }
     
-    override func windowDidLoad() {
+    override func windowWillLoad() {
         
-        super.windowDidLoad()
+        super.windowWillLoad()
         
         excludeShiIDsCache = excludeShiIDs
     }
     
     private func includeShip(shipID: Int) {
         
-        var array = excludeShiIDs
-        
-        guard let index = array.index(of: shipID) else { return }
+        guard let index = excludeShiIDs.index(of: shipID) else {
+            
+            return
+        }
         
-        array.remove(at: index)
-        excludeShiIDs = array
+        excludeShiIDs.remove(at: index)
     }
     
     private func excludeShip(shipID: Int) {
         
-        var array = excludeShiIDs
-        array.append(shipID)
-        excludeShiIDs = array
+        excludeShiIDs.append(shipID)
     }
     
     @IBAction func showHideShip(_ sender: AnyObject?) {
         
         let row = tableView.clickedRow
         
-        guard let ships = shipsController.arrangedObjects as? [Ship] else { return }
-        guard case 0..<ships.count = row else { return }
+        guard let ships = shipsController.arrangedObjects as? [Ship] else {
+            
+            return
+        }
+        guard case 0..<ships.count = row else {
+            
+            return
+        }
         
         let shipID = ships[row].id
         if isExcludeShipID(shipID) {
@@ -156,15 +156,27 @@ final class UpgradableShipsWindowController: NSWindowController {
     
     override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
         
-        if menuItem.action == .showHideShip {
+        guard let action = menuItem.action else {
+            
+            return false
+        }
+        
+        switch action {
+            
+        case #selector(UpgradableShipsWindowController.showHideShip(_:)):
             
             let row = tableView.clickedRow
             
-            guard let ships = shipsController.arrangedObjects as? [Ship] else { return false }
-            guard case 0..<ships.count = row else { return false }
+            guard let ships = shipsController.arrangedObjects as? [Ship] else {
+                
+                return false
+            }
+            guard case 0..<ships.count = row else {
+                
+                return false
+            }
             
-            let shipID = ships[row].id
-            if isExcludeShipID(shipID) {
+            if isExcludeShipID(ships[row].id) {
                 
                 menuItem.title = LocalizedStrings.showKanmusu.string
                 
@@ -174,8 +186,9 @@ final class UpgradableShipsWindowController: NSWindowController {
             }
             
             return true
+            
+        default: return false
+            
         }
-        
-        return false
     }
 }