OSDN Git Service

UIを調整
[kcd/KCD.git] / KCD / HMSlotItemShortNameTransformer.m
1 //
2 //  HMSlotItemShortNameTransformer.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2015/10/23.
6 //  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMSlotItemShortNameTransformer.h"
10
11 #import "HMServerDataStore.h"
12
13 #import "HMKCSlotItemObject+Extensions.h"
14 #import "HMKCMasterSlotItemObject.h"
15
16 static NSDictionary *slotItemShortName = nil;
17
18 @implementation HMSlotItemShortNameTransformer
19 + (void)load
20 {
21         static dispatch_once_t onceToken;
22         dispatch_once(&onceToken, ^{
23                 [NSValueTransformer setValueTransformer:[self new] forName:@"HMSlotItemShortNameTransformer"];
24                 
25                 NSBundle *mainBundle = [NSBundle mainBundle];
26                 NSString *path = [mainBundle pathForResource:@"SlotItemShortName" ofType:@"plist"];
27                 if(!path) {
28                         NSLog(@"Could not find SlotItemShortName.plist");
29                         NSBeep();
30                         return;
31                 }
32                 id dict = [NSDictionary dictionaryWithContentsOfFile:path];
33                 if(!dict) {
34                         NSLog(@"SlotItemShortName.plist is not dictionay.");
35                         NSBeep();
36                         return;
37                 }
38                 slotItemShortName = dict;
39         });
40 }
41
42 + (Class)transformedValueClass
43 {
44         return [NSString class];
45 }
46 + (BOOL)allowsReverseTransformation
47 {
48         return NO;
49 }
50
51 - (id)transformedValue:(id)value
52 {
53         if(![value isKindOfClass:[NSNumber class]]) return nil;
54         NSInteger slotItemID = [value integerValue];
55         if(slotItemID == -1) return nil;
56         if(slotItemID == 0) return nil;
57         
58         HMServerDataStore *store = [HMServerDataStore defaultManager];
59         
60         NSError *error = nil;
61         NSArray *array = [store objectsWithEntityName:@"SlotItem"
62                                                                                         error:&error
63                                                                   predicateFormat:@"id = %@", value];
64         if([array count] == 0) {
65                 NSLog(@"SlotItem is invalid.");
66                 return nil;
67         }
68         
69         HMKCSlotItemObject *slotItem = array[0];
70         NSString *mstIDString = [slotItem.master_slotItem.id stringValue];
71         NSString *shortName = slotItemShortName[mstIDString];
72         if(shortName) return shortName;
73         
74         return [slotItem.name copy];
75 }
76 @end