summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLSoftButtonState.m
blob: a150a223a75cd86c916f15475a5619896e71edb2 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
//  SDLSoftButtonState.m
//  SmartDeviceLink
//
//  Created by Joel Fischer on 2/22/18.
//  Copyright © 2018 smartdevicelink. All rights reserved.
//

#import "SDLSoftButtonState.h"

#import "SDLArtwork.h"
#import "SDLImage.h"
#import "SDLLogMacros.h"
#import "SDLSoftButton.h"
#import "SDLSoftButtonType.h"

NS_ASSUME_NONNULL_BEGIN

@interface SDLFile()

@property (assign, nonatomic, readwrite) BOOL persistent;

@end

@interface SDLSoftButtonState()

@property (strong, nonatomic, readonly) SDLSoftButtonType type;
@property (strong, nonatomic, readonly, nullable) SDLImage *image;

@end

@implementation SDLSoftButtonState

- (instancetype)initWithStateName:(NSString *)stateName text:(nullable NSString *)text image:(nullable UIImage *)image {
    SDLArtwork *artwork = [[SDLArtwork alloc] initWithImage:image persistent:YES asImageFormat:SDLArtworkImageFormatPNG];
    return [self initWithStateName:stateName text:text artwork:artwork];
}

- (instancetype)initWithStateName:(NSString *)stateName text:(nullable NSString *)text artwork:(nullable SDLArtwork *)artwork {
    self = [super init];
    if (!self) { return nil; }

    if (artwork == nil && text == nil) {
        SDLLogE(@"Attempted to create an invalid soft button state: text and artwork are both nil");
        return nil;
    }

    _name = stateName;
    _text = text;
    _artwork = artwork;
    _systemAction = SDLSystemActionDefaultAction;

    return self;
}

- (SDLSoftButton *)softButton {
    SDLSystemAction action = self.systemAction ?: SDLSystemActionDefaultAction;
    return [[SDLSoftButton alloc] initWithType:self.type text:self.text image:self.image highlighted:self.highlighted buttonId:0 systemAction:action handler:nil];
}

- (SDLSoftButtonType)type {
    if (self.artwork != nil && self.text != nil) {
        return SDLSoftButtonTypeBoth;
    } else if (self.artwork != nil) {
        return SDLSoftButtonTypeImage;
    } else {
        return SDLSoftButtonTypeText;
    }
}

- (nullable SDLImage *)image {
    if (self.artwork == nil) { return nil; }

    return [[SDLImage alloc] initWithName:self.artwork.name ofType:SDLImageTypeDynamic isTemplate:self.artwork.isTemplate];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"Name: %@, Text: %@, Image: %@", self.name, self.text, self.image.value];
}

- (NSString *)debugDescription {
    return [NSString stringWithFormat:@"Name: %@, Text: %@, Image: %@, Soft Button: %@", self.name, self.text, self.image, self.softButton];
}

@end

NS_ASSUME_NONNULL_END