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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
//
// SDLSoftButtonObject.m
// SmartDeviceLink
//
// Created by Joel Fischer on 2/22/18.
// Copyright © 2018 smartdevicelink. All rights reserved.
//
#import "SDLSoftButtonObject.h"
#import "SDLError.h"
#import "SDLLogMacros.h"
#import "SDLSoftButton.h"
#import "SDLSoftButtonManager.h"
#import "SDLSoftButtonState.h"
NS_ASSUME_NONNULL_BEGIN
@interface SDLSoftButtonManager()
- (void)sdl_transitionSoftButton:(SDLSoftButtonObject *)softButton;
@end
@interface SDLSoftButtonObject()
@property (assign, nonatomic) NSUInteger buttonId;
@property (strong, nonatomic) NSString *currentStateName;
@property (weak, nonatomic) SDLSoftButtonManager *manager;
@end
@implementation SDLSoftButtonObject
- (instancetype)initWithName:(NSString *)name states:(NSArray<SDLSoftButtonState *> *)states initialStateName:(NSString *)initialStateName handler:(nullable SDLRPCButtonNotificationHandler)eventHandler {
self = [super init];
if (!self) { return nil; }
// Make sure there aren't two states with the same name
if ([self sdl_hasTwoStatesOfSameName:states]) {
return nil;
}
_name = name;
_states = states;
_currentStateName = initialStateName;
_eventHandler = eventHandler;
return self;
}
- (instancetype)initWithName:(NSString *)name state:(SDLSoftButtonState *)state handler:(nullable SDLRPCButtonNotificationHandler)eventHandler {
return [self initWithName:name states:@[state] initialStateName:state.name handler:eventHandler];
}
- (BOOL)transitionToStateNamed:(NSString *)stateName {
if ([self stateWithName:stateName] == nil) {
SDLLogE(@"Attempted to transition to state: %@ on soft button: %@ but no state with that name was found", stateName, self.name);
return NO;
}
SDLLogD(@"Transitioning button %@ to state %@", self.name, stateName);
self.currentStateName = stateName;
[self.manager sdl_transitionSoftButton:self];
return YES;
}
- (void)transitionToNextState {
NSString *nextStateName = nil;
for (NSUInteger i = 0; i < self.states.count; i++) {
if ([self.states[i].name isEqualToString:self.currentStateName]) {
NSUInteger nextStateNumber = (i == self.states.count - 1) ? 0 : (i + 1);
nextStateName = self.states[nextStateNumber].name;
break;
}
}
if (nextStateName == nil) {
return;
}
[self transitionToStateNamed:nextStateName];
}
- (SDLSoftButtonState *)currentState {
SDLSoftButtonState *state = [self stateWithName:self.currentStateName];
if (state == nil) {
@throw [NSException sdl_invalidSoftButtonStateException];
} else {
return state;
}
}
- (SDLSoftButton *)currentStateSoftButton {
SDLSoftButton *button = self.currentState.softButton;
button.softButtonID = @(self.buttonId);
button.handler = self.eventHandler;
return button;
}
- (nullable SDLSoftButtonState *)stateWithName:(NSString *)stateName {
for (SDLSoftButtonState *state in self.states) {
if ([state.name isEqualToString:stateName]) {
return state;
}
}
return nil;
}
- (BOOL)sdl_hasTwoStatesOfSameName:(NSArray<SDLSoftButtonState *> *)states {
for (NSUInteger i = 0; i < states.count; i++) {
NSString *stateName = states[i].name;
for (NSUInteger j = (i + 1); j < states.count; j++) {
if ([states[j].name isEqualToString:stateName]) {
return YES;
}
}
}
return NO;
}
- (NSString *)description {
NSMutableArray<NSString *> *allStateNames = [NSMutableArray array];
for (SDLSoftButtonState *state in self.states) {
[allStateNames addObject:state.name];
}
return [NSString stringWithFormat:@"Name: %@, Current State: %@, All States: %@", self.name, self.currentStateName, allStateNames];
}
- (NSString *)debugDescription {
return [NSString stringWithFormat:@"Name: %@, Current State: %@, All States: %@", self.name, self.currentState, self.states];
}
@end
NS_ASSUME_NONNULL_END
|