aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/MASDictionaryTransformer.m
blob: f0d9b2eacb994420d11a6b7de91870f9534eeb29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import "MASDictionaryTransformer.h"
#import "MASShortcut.h"

static NSString *const MASKeyCodeKey = @"keyCode";
static NSString *const MASModifierFlagsKey = @"modifierFlags";

@implementation MASDictionaryTransformer

+ (BOOL) allowsReverseTransformation
{
    return YES;
}

- (NSDictionary*) reverseTransformedValue: (MASShortcut*) shortcut
{
    return @{
        MASKeyCodeKey: @([shortcut keyCode]),
        MASModifierFlagsKey: @([shortcut modifierFlags])
    };
}

- (MASShortcut*) transformedValue: (NSDictionary*) dictionary
{
    // We have to be defensive here as the value may come from user defaults.
    if (![dictionary isKindOfClass:[NSDictionary class]]) {
        return nil;
    }

    id keyCodeBox = [dictionary objectForKey:MASKeyCodeKey];
    id modifierFlagsBox = [dictionary objectForKey:MASModifierFlagsKey];

    SEL integerValue = @selector(integerValue);
    if (![keyCodeBox respondsToSelector:integerValue] || ![modifierFlagsBox respondsToSelector:integerValue]) {
        return nil;
    }

    return [MASShortcut
        shortcutWithKeyCode:[keyCodeBox integerValue]
        modifierFlags:[modifierFlagsBox integerValue]];
}

@end