summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/public/SDLChoiceSet.m
blob: 8baf085a6974ff514d58a460726d8c1679deac17 (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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
//  SDLChoiceSet.m
//  SmartDeviceLink
//
//  Created by Joel Fischer on 5/21/18.
//  Copyright © 2018 smartdevicelink. All rights reserved.
//

#import "SDLChoiceSet.h"

#import "SDLChoiceCell.h"
#import "SDLLogMacros.h"
#import "SDLTTSChunk.h"
#import "SDLVrHelpItem.h"
#import "SDLVersion.h"
#import "SDLGlobals.h"

NS_ASSUME_NONNULL_BEGIN

@interface SDLChoiceSet()

@property (nullable, copy, nonatomic) SDLChoiceSetCanceledHandler canceledHandler;

@end

@implementation SDLChoiceSet

static const float TimeoutDefault = 0.0;
static const float TimeoutMinCap = 5.0;
static const float TimeoutMaxCap = 100.0;
static NSTimeInterval _defaultTimeout = 10.0;
static SDLChoiceSetLayout _defaultLayout = SDLChoiceSetLayoutList;

- (instancetype)init {
    self = [super init];
    if (!self) { return nil; }

    _timeout = TimeoutDefault;
    _layout = self.class.defaultLayout;

    return self;
}

- (instancetype)initWithTitle:(NSString *)title delegate:(id<SDLChoiceSetDelegate>)delegate choices:(NSArray<SDLChoiceCell *> *)choices {
    return [self initWithTitle:title delegate:delegate layout:SDLChoiceSet.defaultLayout timeout:SDLChoiceSet.defaultTimeout initialPrompt:nil timeoutPrompt:nil helpPrompt:nil vrHelpList:nil choices:choices];
}

- (instancetype)initWithTitle:(NSString *)title delegate:(id<SDLChoiceSetDelegate>)delegate layout:(SDLChoiceSetLayout)layout timeout:(NSTimeInterval)timeout initialPromptString:(nullable NSString *)initialPrompt timeoutPromptString:(nullable NSString *)timeoutPrompt helpPromptString:(nullable NSString *)helpPrompt vrHelpList:(nullable NSArray<SDLVRHelpItem *> *)helpList choices:(NSArray<SDLChoiceCell *> *)choices {
    NSArray<SDLTTSChunk *> *initialTTS = [SDLTTSChunk textChunksFromString:initialPrompt];
    NSArray<SDLTTSChunk *> *timeoutTTS = [SDLTTSChunk textChunksFromString:timeoutPrompt];
    NSArray<SDLTTSChunk *> *helpTTS = [SDLTTSChunk textChunksFromString:helpPrompt];

    return [self initWithTitle:title delegate:delegate layout:layout timeout:timeout initialPrompt:initialTTS timeoutPrompt:timeoutTTS helpPrompt:helpTTS vrHelpList:helpList choices:choices];
}

- (instancetype)initWithTitle:(NSString *)title delegate:(id<SDLChoiceSetDelegate>)delegate layout:(SDLChoiceSetLayout)layout timeout:(NSTimeInterval)timeout initialPrompt:(nullable NSArray<SDLTTSChunk *> *)initialPrompt timeoutPrompt:(nullable NSArray<SDLTTSChunk *> *)timeoutPrompt helpPrompt:(nullable NSArray<SDLTTSChunk *> *)helpPrompt vrHelpList:(nullable NSArray<SDLVRHelpItem *> *)helpList choices:(NSArray<SDLChoiceCell *> *)choices {
    self = [self init];
    if (!self) { return nil; }

    if (choices.count == 0 || choices.count > 100) {
        SDLLogW(@"Attempted to create a choice set with %lu choices; Only 1 - 100 choices are valid", (unsigned long)choices.count);
        return nil;
    }

    if (title.length == 0 || title.length > 500) {
        SDLLogW(@"Attempted to create a choice set title with a %lu length. Only 500 characters are supported", (unsigned long)title.length);
        return nil;
    }

    if (![self sdl_choiceCellsAreUnique:choices]) { return nil; }

    for (NSUInteger i = 0; i < helpList.count; i++) {
        helpList[i].position = @(i + 1);
    }

    _title = title;
    _delegate = delegate;
    _layout = layout;
    self.timeout = timeout;
    _initialPrompt = initialPrompt;
    _timeoutPrompt = timeoutPrompt;
    _helpPrompt = helpPrompt;
    _helpList = helpList;
    _choices = choices;

    return self;
}

#pragma mark - Cancel

- (void)cancel {
    if (self.canceledHandler == nil) { return; }
    self.canceledHandler();
}

#pragma mark - Getters / Setters

+ (void)setDefaultTimeout:(NSTimeInterval)defaultTimeout {
    _defaultTimeout = defaultTimeout;
}

+ (NSTimeInterval)defaultTimeout {
   if (_defaultTimeout < TimeoutMinCap) {
        return TimeoutMinCap;
    } else if (_defaultTimeout > TimeoutMaxCap) {
        return TimeoutMaxCap;
    }

    return _defaultTimeout;
}

- (NSTimeInterval)timeout {
    if (_timeout == TimeoutDefault) {
        return SDLChoiceSet.defaultTimeout;
    } else if (_timeout < TimeoutMinCap) {
        return TimeoutMinCap;
    } else if (_timeout > TimeoutMaxCap) {
        return TimeoutMaxCap;
    }

    return _timeout;
}

+ (SDLChoiceSetLayout)defaultLayout {
    return _defaultLayout;
}

+ (void)setDefaultLayout:(SDLChoiceSetLayout)defaultLayout {
    _defaultLayout = defaultLayout;
}

- (void)setHelpList:(nullable NSArray<SDLVRHelpItem *> *)helpList {
    _helpList = helpList;

    for (NSUInteger i = 0; i < _helpList.count; i++) {
        _helpList[i].position = @(i + 1);
    }
}

#pragma mark - Helpers

/**
 Check for duplicate choices and voiceCommands

 @param choices The choices you will be adding
 @return Boolean that indicates whether choices and voice commands are unique or not
 */
-(BOOL)sdl_choiceCellsAreUnique:(NSArray<SDLChoiceCell *> *)choices {
    NSMutableSet<SDLChoiceCell *> *identicalCellsCheckSet = [NSMutableSet setWithCapacity:choices.count];
    NSMutableSet<NSString *> *identicalVoiceCommandsCheckSet = [NSMutableSet set];
    NSUInteger allVoiceCommandsCount = 0;
    for (SDLChoiceCell *cell in choices) {
        [identicalCellsCheckSet addObject:cell];

        if (cell.voiceCommands == nil) { continue; }
        [identicalVoiceCommandsCheckSet addObjectsFromArray:cell.voiceCommands];
        allVoiceCommandsCount += cell.voiceCommands.count;
    }

    if (identicalCellsCheckSet.count < choices.count) {
        SDLLogE(@"Attempted to create a choice set with duplicate cells. At least one property must be different between any two cells. The choice set will not be set.");
        return NO;
    }

    // All the VR commands must be unique
    if (identicalVoiceCommandsCheckSet.count < allVoiceCommandsCount) {
        SDLLogE(@"Attempted to create a choice set where the cells contained duplicate voice commands. All VR commands must be unique. There are %lu unique VR commands and %lu VR commands. The choice set will not be set.", (unsigned long)identicalVoiceCommandsCheckSet.count, (unsigned long)allVoiceCommandsCount);
        return NO;
    }

    return YES;
}

#pragma mark - Etc.

- (NSString *)description {
    return [NSString stringWithFormat:@"SDLChoiceSet: \"%@\", layout: %@", _title, (_layout == SDLChoiceSetLayoutList ? @"List" : @"Tiles")];
}

- (NSString *)debugDescription {
    return [NSString stringWithFormat:@"SDLChoiceSet: Title: \"%@\", layout: %@, timeout: %@, initial prompt: \"%@\", timeout prompt: \"%@\", help prompt: \"%@\", help list: %@, choices: %@", _title, (_layout == SDLChoiceSetLayoutList ? @"List" : @"Tiles"), @(_timeout), _initialPrompt, _timeoutPrompt, _helpPrompt, _helpList, _choices];
}

@end

NS_ASSUME_NONNULL_END