summaryrefslogtreecommitdiff
path: root/Example Apps/Example ObjC/PerformInteractionManager.m
blob: 82c8a6535d97727aaf941f037633e7015a9d64fe (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
//
//  PerformInteractionManager.m
//  SmartDeviceLink-Example-ObjC
//
//  Created by Nicole on 5/15/18.
//  Copyright © 2018 smartdevicelink. All rights reserved.
//

#import "PerformInteractionManager.h"

#import "AppConstants.h"

#import "SmartDeviceLink.h"

NS_ASSUME_NONNULL_BEGIN

@interface PerformInteractionManager() <SDLChoiceSetDelegate, SDLKeyboardDelegate>

@property (weak, nonatomic) SDLManager *manager;

@property (strong, nonatomic, readonly) SDLChoiceSet *choiceSet;
@property (copy, nonatomic, readonly) NSArray<SDLChoiceCell *> *cells;

@end

@implementation PerformInteractionManager

- (instancetype)initWithManager:(SDLManager *)manager {
    self = [super init];
    if (!self) { return nil; }

    _manager = manager;

    return self;
}

- (void)showWithTriggerSource:(SDLTriggerSource)source {
    [self.manager.screenManager presentSearchableChoiceSet:self.choiceSet mode:[self modeForTriggerSource:source] withKeyboardDelegate:self];
}

- (SDLChoiceSet *)choiceSet {
    return [[SDLChoiceSet alloc] initWithTitle:PICSInitialPrompt delegate:self layout:SDLChoiceSetLayoutList timeout:10 initialPromptString:PICSInitialPrompt timeoutPromptString:PICSTimeoutPrompt helpPromptString:PICSHelpPrompt vrHelpList:nil choices:self.cells];
}

- (NSArray<SDLChoiceCell *> *)cells {
    SDLChoiceCell *firstChoice = [[SDLChoiceCell alloc] initWithText:PICSFirstChoice];
    SDLChoiceCell *secondChoice = [[SDLChoiceCell alloc] initWithText:PICSSecondChoice];
    SDLChoiceCell *thirdChoice = [[SDLChoiceCell alloc] initWithText:PICSThirdChoice];

    return @[firstChoice, secondChoice, thirdChoice];
}

- (SDLInteractionMode)modeForTriggerSource:(SDLTriggerSource)source {
    return ([source isEqualToEnum:SDLTriggerSourceMenu] ? SDLInteractionModeManualOnly : SDLInteractionModeVoiceRecognitionOnly);
}

#pragma mark - SDLChoiceSetDelegate

- (void)choiceSet:(SDLChoiceSet *)choiceSet didSelectChoice:(SDLChoiceCell *)choice withSource:(SDLTriggerSource)source atRowIndex:(NSUInteger)rowIndex {
    [self.manager sendRequest:[[SDLSpeak alloc] initWithTTS:TTSGoodJob]];
}

- (void)choiceSet:(SDLChoiceSet *)choiceSet didReceiveError:(NSError *)error {
    [self.manager sendRequest:[[SDLSpeak alloc] initWithTTS:TTSYouMissed]];
}

#pragma mark - SDLKeyboardDelegate

- (void)userDidSubmitInput:(NSString *)inputText withEvent:(SDLKeyboardEvent)source {
    if ([source isEqualToEnum:SDLKeyboardEventSubmitted]) {
        [self.manager sendRequest:[[SDLSpeak alloc] initWithTTS:TTSGoodJob]];
    } else if ([source isEqualToEnum:SDLKeyboardEventVoice]) {
        // Start an audio pass thru voice session
    }
}

- (void)keyboardDidAbortWithReason:(SDLKeyboardEvent)event {
    [self.manager sendRequest:[[SDLSpeak alloc] initWithTTS:TTSYouMissed]];
}

- (void)updateAutocompleteWithInput:(NSString *)currentInputText completionHandler:(SDLKeyboardAutocompleteCompletionHandler)completionHandler {
    if ([currentInputText.lowercaseString hasPrefix:@"f"]) {
        completionHandler(PICSFirstChoice);
    } else if ([currentInputText.lowercaseString hasPrefix:@"s"]) {
        completionHandler(PICSSecondChoice);
    } else if ([currentInputText.lowercaseString hasPrefix:@"t"]) {
        completionHandler(PICSThirdChoice);
    } else {
        completionHandler(nil);
    }
}

@end

NS_ASSUME_NONNULL_END