OSDN Git Service

UIを調整
[kcd/KCD.git] / KCD / HMUpgradableShipsWindowController.m
1 //
2 //  HMUpgradableShipsWindowController.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/10/26.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMUpgradableShipsWindowController.h"
10
11 #import "HMAppDelegate.h"
12 #import "HMServerDataStore.h"
13 #import "HMUserDefaults.h"
14 #import "HMKCShipObject+Extensions.h"
15
16
17 static NSArray *sExcludeShiIDs = nil;
18
19 @interface HMUpgradableShipsWindowController ()
20
21 @property (nonatomic, weak) IBOutlet NSMenu *contextualMenu;
22 @property (nonatomic, weak) IBOutlet NSTableView *tableView;
23 @property (nonatomic, weak) IBOutlet NSArrayController *shipsController;
24
25 @property (nonatomic, strong) NSArray *excludeShiIDs;
26 @end
27
28
29 BOOL isExcludeShipID(id shipID)
30 {
31         if([sExcludeShiIDs containsObject:shipID]) {
32                 return YES;
33         }
34         return NO;
35 }
36
37 @implementation HMUpgradableShipsWindowController
38
39 - (id)init
40 {
41         self = [super initWithWindowNibName:NSStringFromClass([self class])];
42         if(self) {
43                 [self loadExcludeShipIDs];
44         }
45         return self;
46 }
47
48 - (NSURL *)excludeAhipIDsSaveURL
49 {
50         HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
51         NSURL *dir = appDelegate.supportDirectory;
52         NSURL *pathURL = [dir URLByAppendingPathComponent:@"ExcludeShipIDs"];
53         return pathURL;
54 }
55 - (void)saveExcludeShipIDs
56 {
57         NSURL *pathURL = [self excludeAhipIDsSaveURL];
58         
59         [self.excludeShiIDs writeToURL:pathURL atomically:YES];
60 }
61 - (void)loadExcludeShipIDs
62 {
63         NSURL *pathURL = [self excludeAhipIDsSaveURL];
64         NSArray *array = [NSArray arrayWithContentsOfURL:pathURL];
65         if(array && ![array isKindOfClass:[NSArray class]]) {
66                 NSLog(@"%@ is broken.", pathURL.path);
67                 array = nil;
68         }
69         self.excludeShiIDs = array ?: [NSArray array];
70 }
71
72 - (NSManagedObjectContext *)managedObjectContext
73 {
74         return [HMServerDataStore defaultManager].managedObjectContext;
75 }
76
77 - (NSArray *)excludeShiIDs
78 {
79         return sExcludeShiIDs;
80 }
81 - (void)setExcludeShiIDs:(NSArray *)excludeShiIDs
82 {
83         sExcludeShiIDs = excludeShiIDs;
84         [self saveExcludeShipIDs];
85 }
86
87 + (NSSet *)keyPathsForValuesAffectingFilterPredicate
88 {
89         return [NSSet setWithObjects:@"showLevelOneShipInUpgradableList", @"excludeShiIDs", @"showsExcludedShipInUpgradableList", nil];
90 }
91 - (void)setShowLevelOneShipInUpgradableList:(BOOL)showLevelOneShipInUpgradableList
92 {
93         HMStandardDefaults.showLevelOneShipInUpgradableList = showLevelOneShipInUpgradableList;
94 }
95 - (BOOL)showLevelOneShipInUpgradableList
96 {
97         return HMStandardDefaults.showLevelOneShipInUpgradableList;
98 }
99 - (void)setShowsExcludedShipInUpgradableList:(BOOL)showsExcludedShipInUpgradableList
100 {
101         HMStandardDefaults.showsExcludedShipInUpgradableList = showsExcludedShipInUpgradableList;
102 }
103 - (BOOL)showsExcludedShipInUpgradableList
104 {
105         return HMStandardDefaults.showsExcludedShipInUpgradableList;
106 }
107 - (NSPredicate *)filterPredicate
108 {
109         NSPredicate *filterPredicate = nil;
110         NSPredicate *excludeShip = nil;
111         if(!HMStandardDefaults.showLevelOneShipInUpgradableList) {
112                 filterPredicate = [NSPredicate predicateWithFormat:@"lv != 1"];
113         }
114         if(!self.showsExcludedShipInUpgradableList && self.excludeShiIDs.count != 0) {
115                 excludeShip = [NSPredicate predicateWithFormat:@"NOT id IN %@", self.excludeShiIDs];
116         }
117         if(filterPredicate && excludeShip) {
118                 return [[NSCompoundPredicate alloc] initWithType:NSAndPredicateType
119                                                                                    subpredicates:@[filterPredicate, excludeShip]];
120         }
121         if(filterPredicate) {
122                 return filterPredicate;
123         }
124         if(excludeShip ) {
125                 return excludeShip;
126         }
127         return nil;
128 }
129
130 - (void)includeShip:(id)shipID
131 {
132         NSMutableArray *array = [self.excludeShiIDs mutableCopy];
133         [array removeObject:shipID];
134         self.excludeShiIDs = array;
135 }
136 - (void)excludeShip:(id)shipID
137 {
138         NSMutableArray *array = [self.excludeShiIDs mutableCopy];
139         [array addObject:shipID];
140         self.excludeShiIDs = array;
141 }
142 - (IBAction)showHideShip:(id)sender
143 {
144         NSInteger row = self.tableView.clickedRow;
145         if(row == -1) return;
146         
147         HMKCShipObject *ship = [[self.shipsController arrangedObjects] objectAtIndex:row];
148         id shipID = ship.id;
149         if([self.excludeShiIDs containsObject:shipID]) {
150                 [self includeShip:shipID];
151         } else {
152                 [self excludeShip:shipID];
153         }
154 }
155
156 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
157 {
158         SEL action = menuItem.action;
159         if(action == @selector(showHideShip:)) {
160                 NSInteger row = self.tableView.clickedRow;
161                 if(row == -1) return NO;
162                 
163                 HMKCShipObject *ship = [[self.shipsController arrangedObjects] objectAtIndex:row];
164                 id shipID = ship.id;
165                 if([self.excludeShiIDs containsObject:shipID]) {
166                         menuItem.title = NSLocalizedString(@"Show Kanmusu", @"HMUpgradableShipsWindowController menu item");
167                 } else {
168                         menuItem.title = NSLocalizedString(@"Hide Kanmusu", @"HMUpgradableShipsWindowController menu item");
169                 }
170                 return YES;
171         }
172         
173         return NO;
174 }
175
176 @end