OSDN Git Service

編成支援の3つのスクロールビューを同期してスクロールするようにした
[kcd/KCD.git] / KCD / HMShipViewController.m
1 //
2 //  HMShipViewController.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/03/04.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMShipViewController.h"
10
11 #import "HMAppDelegate.h"
12 #import "HMServerDataStore.h"
13
14
15 typedef NS_ENUM(NSInteger, ViewType) {
16         kExpView = 0,
17         kPowerView = 1,
18         kPower2View = 2,
19 };
20
21 @interface HMShipViewController ()
22 @property (weak) NSView *currentTableView;
23 @end
24
25 @implementation HMShipViewController
26
27 - (id)init
28 {
29         self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil];
30         return self;
31 }
32
33 - (void)awakeFromNib
34 {
35         self.currentTableView = self.expTableView;
36         
37         
38         [self.shipController fetchWithRequest:nil merge:YES error:NULL];
39         
40         NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
41         id data = [ud objectForKey:@"shipviewsortdescriptor"];
42         id sortDescriptors = [NSKeyedUnarchiver unarchiveObjectWithData:data];
43         if(sortDescriptors) {
44                 [self.shipController setSortDescriptors:sortDescriptors];
45         }
46         
47         [self.shipController addObserver:self
48                                                   forKeyPath:NSSortDescriptorsBinding
49                                                          options:0
50                                                          context:NULL];
51         
52         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
53         [nc addObserver:self
54                    selector:@selector(scrollViewDidEndLiveScrollNotification:)
55                            name:NSScrollViewDidEndLiveScrollNotification
56                          object:self.expTableView];
57         [nc addObserver:self
58                    selector:@selector(scrollViewDidEndLiveScrollNotification:)
59                            name:NSScrollViewDidEndLiveScrollNotification
60                          object:self.powerTableView];
61         [nc addObserver:self
62                    selector:@selector(scrollViewDidEndLiveScrollNotification:)
63                            name:NSScrollViewDidEndLiveScrollNotification
64                          object:self.power2TableView];
65 }
66 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
67 {
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"];
73                 
74                 return;
75         }
76         
77         [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
78 }
79 - (NSManagedObjectContext *)managedObjectContext
80 {
81         return [HMServerDataStore defaultManager].managedObjectContext;
82 }
83
84 - (void)showViewWithNumber:(ViewType)number
85 {
86         NSView *newSelection = nil;
87         switch (number) {
88                 case kExpView:
89                         newSelection = self.expTableView;
90                         break;
91                 case kPowerView:
92                         newSelection = self.powerTableView;
93                         break;
94                 case kPower2View:
95                         newSelection = self.power2TableView;
96                         break;
97         }
98         
99         if(!newSelection) return;
100         if([self.currentTableView isEqual:newSelection]) return;
101         
102         [newSelection setFrame:[self.currentTableView frame]];
103         [newSelection setAutoresizingMask:[self.currentTableView autoresizingMask]];
104         [self.view replaceSubview:self.currentTableView with:newSelection];
105         self.currentTableView = newSelection;
106 }
107
108
109 - (IBAction)changeCategory:(id)sender
110 {
111         NSArray *categories = [[NSApp delegate] shipTypeCategories];
112         
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]];
117         }
118         [self.shipController setFilterPredicate:predicate];
119         [self.shipController rearrangeObjects];
120 }
121
122 - (IBAction)changeView:(id)sender
123 {
124         NSInteger tag = -1;
125         if([sender respondsToSelector:@selector(selectedSegment)]) {
126                 NSSegmentedCell *cell = [sender cell];
127                 NSUInteger index = [sender selectedSegment];
128                 tag = [cell tagForSegment:index];
129         } else {
130                 tag = [sender tag];
131         }
132         [self showViewWithNumber:tag];
133 }
134
135 #pragma mark - NSScrollViewDidEndLiveScrollNotification
136 - (void)scrollViewDidEndLiveScrollNotification:(NSNotification *)notification
137 {
138         id object = [notification object];
139         
140         NSRect visibleRect = [object documentVisibleRect];
141         
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];
146                 }
147         }
148 }
149 @end