OSDN Git Service

UIを調整
[kcd/KCD.git] / KCD / HMNyukyoDockStatus.m
1 //
2 //  HMNyukyoDockStatus.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/03/09.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMNyukyoDockStatus.h"
10
11 #import "HMServerDataStore.h"
12 #import "HMUserDefaults.h"
13
14
15 enum {
16         kNoShip = 0,
17         kHasShip = 1,
18 };
19
20 @interface HMNyukyoDockStatus ()
21 @property (strong) NSArrayController *controller;
22
23 @property (strong, readwrite) NSString *name;
24 @property (strong, readwrite) NSNumber *time;
25 @property (readwrite) BOOL isTasking;
26 @property (readwrite) BOOL didNotify;
27
28 @end
29
30
31 @implementation HMNyukyoDockStatus
32
33 - (id)initWithDockNumber:(NSInteger)dockNumber
34 {
35         self = [super init];
36         
37         if(dockNumber < 1 || dockNumber > 4) {
38                 self = nil;
39                 return nil;
40         }
41         
42         if(self) {
43                 _controller = [NSArrayController new];
44                 [self.controller setManagedObjectContext:[HMServerDataStore defaultManager].managedObjectContext];
45                 [self.controller setEntityName:@"NyukyoDock"];
46                 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %ld", dockNumber];
47                 [self.controller setFetchPredicate:predicate];
48                 [self.controller setAutomaticallyRearrangesObjects:YES];
49                 [self.controller fetch:nil];
50                 
51                 [self.controller addObserver:self
52                                                  forKeyPath:@"selection.state"
53                                                         options:0
54                                                         context:NULL];
55         }
56         
57         return self;
58 }
59
60 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
61 {
62         if([keyPath isEqualToString:@"selection.state"]) {
63                 NSInteger status = [[self.controller valueForKeyPath:@"selection.state"] integerValue];
64                 switch(status) {
65                         case kNoShip:
66                                 self.name = nil;
67                                 if(self.isTasking) self.isTasking = NO;
68                                 if(self.didNotify) self.didNotify = NO;
69                                 break;
70                         case kHasShip:
71                                 [self updateName:nil];
72                                 if(!self.isTasking) self.isTasking = YES;
73                                 break;
74                         default:
75                                 NSLog(@"Nyukyo Dock status is %ld", status);
76                                 break;
77                 }
78                 return;
79         }
80         
81         [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
82 }
83
84 - (void)update
85 {
86         if(!self.isTasking) {
87                 self.time = nil;
88                 return;
89         }
90         
91         NSNumber *compTimeValue = [self.controller valueForKeyPath:@"selection.complete_time"];
92         if(![compTimeValue isKindOfClass:[NSNumber class]]) {
93                 self.name = nil;
94                 self.time = nil;
95                 return;
96         }
97         
98         NSTimeInterval compTime = (NSUInteger)([compTimeValue doubleValue] / 1000.0);
99         NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
100         NSTimeInterval diff = compTime - [now timeIntervalSince1970];
101         
102         if(diff < 0) {
103                 self.time = @(0);
104         } else {
105                 self.time =  @(diff);
106         }
107         
108         if(!self.didNotify) {
109                 if(diff < 1 * 60) {
110                         NSUserNotification * notification = [NSUserNotification new];
111                         NSString *format = NSLocalizedString(@"%@ Will Finish Docking.", @"%@ Will Finish Docking.");
112                         notification.title = [NSString stringWithFormat:format, self.name];
113                         notification.informativeText = notification.title;
114                         if(HMStandardDefaults.playFinishNyukyoSound) {
115                                 notification.soundName = NSUserNotificationDefaultSoundName;
116                         }
117                         [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
118                         
119                         self.didNotify = YES;
120                 }
121         }
122 }
123
124 - (void)updateName:(id)dummy
125 {
126         NSNumber *nDockShipId = [self.controller valueForKeyPath:@"selection.ship_id"];
127         if(![nDockShipId isKindOfClass:[NSNumber class]]) return;
128         if([nDockShipId integerValue] == 0) return;
129         
130         NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Ship"];
131         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", nDockShipId];
132         [request setPredicate:predicate];
133         
134         NSArray *array = [self.managedObjectContext executeFetchRequest:request error:NULL];
135         if([array count] == 0) {
136                 [self performSelector:_cmd withObject:nil afterDelay:0.33];
137                 self.name = @"Unknown";
138                 return;
139         }
140         
141         NSString *newName = [array[0] valueForKeyPath:@"master_ship.name"];
142         if(![newName isEqualToString:self.name]) {
143                 self.name = newName;
144         }
145 }
146
147 @end