2 // HMNyukyoDockStatus.m
5 // Created by Hori,Masaki on 2014/03/09.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMNyukyoDockStatus.h"
11 #import "HMServerDataStore.h"
12 #import "HMUserDefaults.h"
20 @interface HMNyukyoDockStatus ()
21 @property (strong) NSArrayController *controller;
23 @property (strong, readwrite) NSString *name;
24 @property (strong, readwrite) NSNumber *time;
25 @property (readwrite) BOOL isTasking;
26 @property (readwrite) BOOL didNotify;
31 @implementation HMNyukyoDockStatus
33 - (id)initWithDockNumber:(NSInteger)dockNumber
37 if(dockNumber < 1 || dockNumber > 4) {
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];
51 [self.controller addObserver:self
52 forKeyPath:@"selection.state"
60 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
62 if([keyPath isEqualToString:@"selection.state"]) {
63 NSInteger status = [[self.controller valueForKeyPath:@"selection.state"] integerValue];
67 if(self.isTasking) self.isTasking = NO;
68 if(self.didNotify) self.didNotify = NO;
71 [self updateName:nil];
72 if(!self.isTasking) self.isTasking = YES;
75 NSLog(@"Nyukyo Dock status is %ld", status);
81 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
91 NSNumber *compTimeValue = [self.controller valueForKeyPath:@"selection.complete_time"];
92 if(![compTimeValue isKindOfClass:[NSNumber class]]) {
98 NSTimeInterval compTime = (NSUInteger)([compTimeValue doubleValue] / 1000.0);
99 NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
100 NSTimeInterval diff = compTime - [now timeIntervalSince1970];
108 if(!self.didNotify) {
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;
117 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
119 self.didNotify = YES;
124 - (void)updateName:(id)dummy
126 NSNumber *nDockShipId = [self.controller valueForKeyPath:@"selection.ship_id"];
127 if(![nDockShipId isKindOfClass:[NSNumber class]]) return;
128 if([nDockShipId integerValue] == 0) return;
130 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Ship"];
131 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %@", nDockShipId];
132 [request setPredicate:predicate];
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";
141 NSString *newName = [array[0] valueForKeyPath:@"master_ship.name"];
142 if(![newName isEqualToString:self.name]) {