2 // HMHistoryWindowController.m
5 // Created by Hori,Masaki on 2014/03/24.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMHistoryWindowController.h"
11 #import "HMLocalDataStore.h"
13 #import "HMKaihatuHistory.h"
14 #import "HMKenzoHistory.h"
15 #import "HMDropShipHistory.h"
18 typedef NS_ENUM(NSUInteger, HMHistoryWindowTabIndex) {
19 kKaihatuHistoryIndex = 0,
20 kKenzoHistoryIndex = 1,
21 kDropHistoryIndex = 2,
24 @interface HMHistoryWindowController () <NSTabViewDelegate, NSTableViewDelegate>
26 @property (readonly) NSManagedObjectContext *manageObjectContext;
28 @property NSInteger selectedTabIndex;
30 @property (nonatomic, strong) IBOutlet NSArrayController *kaihatuHistoryController;
31 @property (nonatomic, strong) IBOutlet NSArrayController *kenzoHistoryController;
32 @property (nonatomic, strong) IBOutlet NSArrayController *dropHistoryController;
34 @property (nonatomic, weak) IBOutlet NSTableView *kaihatuHistoryTableView;
35 @property (nonatomic, weak) IBOutlet NSTableView *kenzoHistoryTableView;
36 @property (nonatomic, weak) IBOutlet NSTableView *dropHistoryTableView;
38 @property (nonatomic, weak) IBOutlet NSSearchField *searchField;
40 @property (nonatomic, strong) IBOutlet NSTouchBar *myTouchBar;
41 @property (nonatomic, weak) IBOutlet NSButton *searchButton;
45 @implementation HMHistoryWindowController
49 self = [super initWithWindowNibName:NSStringFromClass([self class])];
56 [super windowDidLoad];
58 NSString *predicateFormat = @"";
59 NSArrayController *target = nil;
60 switch (self.selectedTabIndex) {
61 case kKaihatuHistoryIndex:
62 target = self.kaihatuHistoryController;
63 predicateFormat = @"name contains $value";
65 case kKenzoHistoryIndex:
66 target = self.kenzoHistoryController;
67 predicateFormat = @"name contains $value";
69 case kDropHistoryIndex:
70 target = self.dropHistoryController;
71 predicateFormat = @"shipName contains $value";
78 [self.searchField bind:NSPredicateBinding
80 withKeyPath:NSFilterPredicateBinding
82 NSPredicateFormatBindingOption : predicateFormat,
86 - (NSManagedObjectContext *)manageObjectContext
88 return [[HMLocalDataStore defaultManager] managedObjectContext];
91 - (IBAction)delete:(id)sender
93 NSArrayController *target = nil;
94 switch (self.selectedTabIndex) {
95 case kKaihatuHistoryIndex:
96 target = self.kaihatuHistoryController;
98 case kKenzoHistoryIndex:
99 target = self.kenzoHistoryController;
101 case kDropHistoryIndex:
102 target = self.dropHistoryController;
109 NSArray *original = [target selectedObjects];
110 NSMutableArray *objectIds = [NSMutableArray new];
111 for(NSManagedObject *object in original) {
112 [objectIds addObject:object.objectID];
115 HMLocalDataStore *store = [HMLocalDataStore oneTimeEditor];
116 NSManagedObjectContext *moc = store.managedObjectContext;
118 for(NSManagedObjectID *objectID in objectIds) {
119 NSManagedObject *object = [moc objectWithID:objectID];
120 [moc deleteObject:object];
124 - (IBAction)addMark:(id)sender
126 NSArrayController *target = nil;
127 NSTableView *targetView = nil;
128 switch (self.selectedTabIndex) {
129 case kKaihatuHistoryIndex:
130 target = self.kaihatuHistoryController;
131 targetView = self.kaihatuHistoryTableView;
133 case kKenzoHistoryIndex:
134 target = self.kenzoHistoryController;
135 targetView = self.kenzoHistoryTableView;
137 case kDropHistoryIndex:
138 target = self.dropHistoryController;
139 targetView = self.dropHistoryTableView;
146 NSArray *a = target.arrangedObjects;
147 id o = a[targetView.clickedRow];
149 NSString *entityName = nil;
150 NSPredicate *predicate = nil;
151 switch (self.selectedTabIndex) {
152 case kKaihatuHistoryIndex:
154 HMKaihatuHistory *obj = o;
155 entityName = @"KaihatuHistory";
156 predicate = [NSPredicate predicateWithFormat:@"date = %@ AND name = %@", obj.date, obj.name];
159 case kKenzoHistoryIndex:
161 HMKenzoHistory *obj = o;
162 entityName = @"KenzoHistory";
163 predicate = [NSPredicate predicateWithFormat:@"date = %@ AND name = %@", obj.date, obj.name];
166 case kDropHistoryIndex:
168 HMDropShipHistory *obj = o;
169 entityName = @"DropShipHistory";
170 predicate = [NSPredicate predicateWithFormat:@"date = %@ AND mapCell = %@", obj.date, obj.mapCell];
176 HMLocalDataStore *store = [HMLocalDataStore oneTimeEditor];
177 NSError *error = nil;
178 NSArray<HMDropShipHistory *> *array = [store objectsWithEntityName:entityName predicate:predicate error:&error];
179 if(array.count == 0) {
180 NSLog(@"%s: ERROR", __PRETTY_FUNCTION__);
184 BOOL mark = array[0].mark.boolValue;
185 array[0].mark = mark ? @NO : @YES;
186 [store saveAction:nil];
189 - (IBAction)selectSearchField:(id)sender
191 [self.window makeFirstResponder:self.searchField];
194 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
196 SEL action = menuItem.action;
197 if(action == @selector(addMark:)) {
198 NSArrayController *target = nil;
199 NSTableView *targetView = nil;
200 switch (self.selectedTabIndex) {
201 case kKaihatuHistoryIndex:
202 target = self.kaihatuHistoryController;
203 targetView = self.kaihatuHistoryTableView;
205 case kKenzoHistoryIndex:
206 target = self.kenzoHistoryController;
207 targetView = self.kenzoHistoryTableView;
209 case kDropHistoryIndex:
210 target = self.dropHistoryController;
211 targetView = self.dropHistoryTableView;
217 NSArray<HMDropShipHistory *> *a = target.arrangedObjects;
218 HMDropShipHistory *o = a[targetView.clickedRow];
220 if(o.mark.boolValue) {
221 menuItem.title = NSLocalizedString(@"Remove mark", @"Remove history mark.");
223 menuItem.title = NSLocalizedString(@"Add mark", @"Add history mark.");
231 - (NSTouchBar *)touchBar
233 if(NSClassFromString(@"NSTouchBar") == Nil) return nil;
235 if(self.myTouchBar) return self.myTouchBar;
237 NSArray *toplevel = nil;
238 NSBundle *mainBundle = [NSBundle mainBundle];
239 [mainBundle loadNibNamed:@"HistoryWindowTouchBar"
241 topLevelObjects:&toplevel];
243 return self.myTouchBar;
246 #pragma mark - NSTableViewDelegate & NSTableViewDataSource
248 - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
250 NSView *itemView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:nil];
254 #pragma mark - NSTabViewDelegate
255 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
257 [self.searchField unbind:NSPredicateBinding];
259 NSString *predicateFormat = @"";
260 NSArrayController *target = nil;
261 switch (self.selectedTabIndex) {
262 case kKaihatuHistoryIndex:
263 target = self.kaihatuHistoryController;
264 predicateFormat = @"name contains $value";
266 case kKenzoHistoryIndex:
267 target = self.kenzoHistoryController;
268 predicateFormat = @"name contains $value";
270 case kDropHistoryIndex:
271 target = self.dropHistoryController;
272 predicateFormat = @"shipName contains $value";
279 [self.searchField bind:NSPredicateBinding
281 withKeyPath:NSFilterPredicateBinding
283 NSPredicateFormatBindingOption : predicateFormat,