2 // HMShipViewController.m
5 // Created by Hori,Masaki on 2014/03/04.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMShipViewController.h"
11 #import "HMAppDelegate.h"
12 #import "HMServerDataStore.h"
15 typedef NS_ENUM(NSInteger, ViewType) {
21 @interface HMShipViewController ()
22 @property (weak) NSView *currentTableView;
25 @implementation HMShipViewController
29 self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil];
35 self.currentTableView = self.expTableView;
38 [self.shipController fetchWithRequest:nil merge:YES error:NULL];
40 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
41 id data = [ud objectForKey:@"shipviewsortdescriptor"];
42 id sortDescriptors = [NSKeyedUnarchiver unarchiveObjectWithData:data];
44 [self.shipController setSortDescriptors:sortDescriptors];
47 [self.shipController addObserver:self
48 forKeyPath:NSSortDescriptorsBinding
52 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
54 selector:@selector(scrollViewDidEndLiveScrollNotification:)
55 name:NSScrollViewDidEndLiveScrollNotification
56 object:self.expTableView];
58 selector:@selector(scrollViewDidEndLiveScrollNotification:)
59 name:NSScrollViewDidEndLiveScrollNotification
60 object:self.powerTableView];
62 selector:@selector(scrollViewDidEndLiveScrollNotification:)
63 name:NSScrollViewDidEndLiveScrollNotification
64 object:self.power2TableView];
66 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
68 if([keyPath isEqualToString:NSSortDescriptorsBinding]) {
69 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
70 id sortDescriptors = [self.shipController sortDescriptors];
71 id data = [NSKeyedArchiver archivedDataWithRootObject:sortDescriptors];
72 [ud setObject:data forKey:@"shipviewsortdescriptor"];
77 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
79 - (NSManagedObjectContext *)managedObjectContext
81 return [HMServerDataStore defaultManager].managedObjectContext;
84 - (void)showViewWithNumber:(ViewType)number
86 NSView *newSelection = nil;
89 newSelection = self.expTableView;
92 newSelection = self.powerTableView;
95 newSelection = self.power2TableView;
99 if(!newSelection) return;
100 if([self.currentTableView isEqual:newSelection]) return;
102 [newSelection setFrame:[self.currentTableView frame]];
103 [newSelection setAutoresizingMask:[self.currentTableView autoresizingMask]];
104 [self.view replaceSubview:self.currentTableView with:newSelection];
105 self.currentTableView = newSelection;
109 - (IBAction)changeCategory:(id)sender
111 NSArray *categories = [[NSApp delegate] shipTypeCategories];
113 NSPredicate *predicate = nil;
114 NSUInteger tag = [sender selectedSegment];
115 if(tag != 0 && tag < 8) {
116 predicate = [NSPredicate predicateWithFormat:@"master_ship.stype.id in %@", categories[tag - 1]];
118 [self.shipController setFilterPredicate:predicate];
119 [self.shipController rearrangeObjects];
122 - (IBAction)changeView:(id)sender
125 if([sender respondsToSelector:@selector(selectedSegment)]) {
126 NSSegmentedCell *cell = [sender cell];
127 NSUInteger index = [sender selectedSegment];
128 tag = [cell tagForSegment:index];
132 [self showViewWithNumber:tag];
135 #pragma mark - NSScrollViewDidEndLiveScrollNotification
136 - (void)scrollViewDidEndLiveScrollNotification:(NSNotification *)notification
138 id object = [notification object];
140 NSRect visibleRect = [object documentVisibleRect];
142 for(id item in @[self.expTableView, self.powerTableView, self.power2TableView]) {
143 if(![object isEqual:item]) {
144 NSView *view = [item documentView];
145 [view scrollRectToVisible:visibleRect];