2 // HMUpgradableShipsWindowController.m
5 // Created by Hori,Masaki on 2014/10/26.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMUpgradableShipsWindowController.h"
11 #import "HMAppDelegate.h"
12 #import "HMServerDataStore.h"
13 #import "HMUserDefaults.h"
14 #import "HMKCShipObject+Extensions.h"
17 static NSArray *sExcludeShiIDs = nil;
19 @interface HMUpgradableShipsWindowController ()
21 @property (nonatomic, weak) IBOutlet NSMenu *contextualMenu;
22 @property (nonatomic, weak) IBOutlet NSTableView *tableView;
23 @property (nonatomic, weak) IBOutlet NSArrayController *shipsController;
25 @property (nonatomic, strong) NSArray *excludeShiIDs;
29 BOOL isExcludeShipID(id shipID)
31 if([sExcludeShiIDs containsObject:shipID]) {
37 @implementation HMUpgradableShipsWindowController
41 self = [super initWithWindowNibName:NSStringFromClass([self class])];
43 [self loadExcludeShipIDs];
48 - (NSURL *)excludeAhipIDsSaveURL
50 HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
51 NSURL *dir = appDelegate.supportDirectory;
52 NSURL *pathURL = [dir URLByAppendingPathComponent:@"ExcludeShipIDs"];
55 - (void)saveExcludeShipIDs
57 NSURL *pathURL = [self excludeAhipIDsSaveURL];
59 [self.excludeShiIDs writeToURL:pathURL atomically:YES];
61 - (void)loadExcludeShipIDs
63 NSURL *pathURL = [self excludeAhipIDsSaveURL];
64 NSArray *array = [NSArray arrayWithContentsOfURL:pathURL];
65 if(array && ![array isKindOfClass:[NSArray class]]) {
66 NSLog(@"%@ is broken.", pathURL.path);
69 self.excludeShiIDs = array ?: [NSArray array];
72 - (NSManagedObjectContext *)managedObjectContext
74 return [HMServerDataStore defaultManager].managedObjectContext;
77 - (NSArray *)excludeShiIDs
79 return sExcludeShiIDs;
81 - (void)setExcludeShiIDs:(NSArray *)excludeShiIDs
83 sExcludeShiIDs = excludeShiIDs;
84 [self saveExcludeShipIDs];
87 + (NSSet *)keyPathsForValuesAffectingFilterPredicate
89 return [NSSet setWithObjects:@"showLevelOneShipInUpgradableList", @"excludeShiIDs", @"showsExcludedShipInUpgradableList", nil];
91 - (void)setShowLevelOneShipInUpgradableList:(BOOL)showLevelOneShipInUpgradableList
93 HMStandardDefaults.showLevelOneShipInUpgradableList = showLevelOneShipInUpgradableList;
95 - (BOOL)showLevelOneShipInUpgradableList
97 return HMStandardDefaults.showLevelOneShipInUpgradableList;
99 - (void)setShowsExcludedShipInUpgradableList:(BOOL)showsExcludedShipInUpgradableList
101 HMStandardDefaults.showsExcludedShipInUpgradableList = showsExcludedShipInUpgradableList;
103 - (BOOL)showsExcludedShipInUpgradableList
105 return HMStandardDefaults.showsExcludedShipInUpgradableList;
107 - (NSPredicate *)filterPredicate
109 NSPredicate *filterPredicate = nil;
110 NSPredicate *excludeShip = nil;
111 if(!HMStandardDefaults.showLevelOneShipInUpgradableList) {
112 filterPredicate = [NSPredicate predicateWithFormat:@"lv != 1"];
114 if(!self.showsExcludedShipInUpgradableList && self.excludeShiIDs.count != 0) {
115 excludeShip = [NSPredicate predicateWithFormat:@"NOT id IN %@", self.excludeShiIDs];
117 if(filterPredicate && excludeShip) {
118 return [[NSCompoundPredicate alloc] initWithType:NSAndPredicateType
119 subpredicates:@[filterPredicate, excludeShip]];
121 if(filterPredicate) {
122 return filterPredicate;
130 - (void)includeShip:(id)shipID
132 NSMutableArray *array = [self.excludeShiIDs mutableCopy];
133 [array removeObject:shipID];
134 self.excludeShiIDs = array;
136 - (void)excludeShip:(id)shipID
138 NSMutableArray *array = [self.excludeShiIDs mutableCopy];
139 [array addObject:shipID];
140 self.excludeShiIDs = array;
142 - (IBAction)showHideShip:(id)sender
144 NSInteger row = self.tableView.clickedRow;
145 if(row == -1) return;
147 HMKCShipObject *ship = [[self.shipsController arrangedObjects] objectAtIndex:row];
149 if([self.excludeShiIDs containsObject:shipID]) {
150 [self includeShip:shipID];
152 [self excludeShip:shipID];
156 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
158 SEL action = menuItem.action;
159 if(action == @selector(showHideShip:)) {
160 NSInteger row = self.tableView.clickedRow;
161 if(row == -1) return NO;
163 HMKCShipObject *ship = [[self.shipsController arrangedObjects] objectAtIndex:row];
165 if([self.excludeShiIDs containsObject:shipID]) {
166 menuItem.title = NSLocalizedString(@"Show Kanmusu", @"HMUpgradableShipsWindowController menu item");
168 menuItem.title = NSLocalizedString(@"Hide Kanmusu", @"HMUpgradableShipsWindowController menu item");