OSDN Git Service

UIを調整
[kcd/KCD.git] / KCD / HMMaserShipCommand.m
1 //
2 //  HMMaserShipCommand.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/02/16.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMMaserShipCommand.h"
10
11 @interface HMMaserShipCommand ()
12 @property (nonatomic, strong) NSArray *masterSTypes;
13 @end
14
15 @implementation HMMaserShipCommand
16 - (NSString *)dataKey
17 {
18         return @"api_data.api_mst_ship";
19 }
20 - (void)execute
21 {
22         [self commitJSONToEntityNamed:@"MasterShip"];
23 }
24
25 - (void)setStype:(id)value toObject:(NSManagedObject *)object
26 {
27         id currentValue = [object valueForKeyPath:@"stype.name"];
28         if(currentValue && ![currentValue isEqual:[NSNull null]]
29            && [currentValue isEqual:value]) return;
30         
31         if(!self.masterSTypes) {
32                 NSManagedObjectContext *managedObjectContext = [object managedObjectContext];
33                 NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MasterSType"];
34                 NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES];
35                 [req setSortDescriptors:@[sortDescriptor]];
36                 NSError *error = nil;
37                 self.masterSTypes = [managedObjectContext executeFetchRequest:req
38                                                                                                                            error:&error];
39                 if(error) {
40                         [self log:@"Fetch error: %@", error];
41                         return;
42                 }
43                 if(!self.masterSTypes || [self.masterSTypes count] == 0) {
44                         [self log:@"Master SType is invalidate"];
45                         return;
46                 }
47         }
48         
49         NSRange range = NSMakeRange(0, self.masterSTypes.count);
50         NSUInteger index = [self.masterSTypes indexOfObject:value
51                                                                                   inSortedRange:range
52                                                                                                 options:NSBinarySearchingFirstEqual
53                                                                                 usingComparator:^(id obj1, id obj2) {
54                                                                                         id value1, value2;
55                                                                                         if([obj1 isKindOfClass:[NSNumber class]]) {
56                                                                                                 value1 = obj1;
57                                                                                         } else {
58                                                                                                 value1 = [obj1 valueForKey:@"id"];
59                                                                                         }
60                                                                                         if([obj2 isKindOfClass:[NSNumber class]]) {
61                                                                                                 value2 = obj2;
62                                                                                         } else {
63                                                                                                 value2 = [obj2 valueForKey:@"id"];
64                                                                                         }
65                                                                                         return [value1 compare:value2];
66                                                                                 }];
67         if(index == NSNotFound) {
68                 [self log:@"Could not find stype of id (%@)", value];
69                 return;
70         }
71         id item = [self.masterSTypes objectAtIndex:index];
72         
73         [self setValueIfNeeded:item toObject:object forKey:@"stype"];
74 }
75 - (BOOL)handleExtraValue:(id)value forKey:(NSString *)key toObject:(NSManagedObject *)object
76 {
77         if([key isEqualToString:@"api_stype"]) {
78                 [self setStype:value toObject:object];
79                 return YES;
80         }
81         return NO;
82 }
83
84 @end