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

import UIKit
import SmartDeviceLink
import SmartDeviceLinkSwift

class PerformInteractionManager: NSObject {
    fileprivate var manager: SDLManager!

    init(sdlManager: SDLManager) {
        self.manager = sdlManager
    }
    
    /// Shows a PICS. The handler is called when the user selects a menu item or when the menu times out after a set amount of time. A custom text-to-speech phrase is spoken when the menu is closed.
    ///
    /// - Parameter manager: The SDL Manager
    func show(from triggerSource: SDLTriggerSource) {
        manager.screenManager.presentSearchableChoiceSet(choiceSet, mode: interactionMode(for: triggerSource), with: self)
    }
}

// MARK: - PICS Menu

private extension PerformInteractionManager {
    /// The PICS menu items
    var choiceCells: [SDLChoiceCell] {
        let firstChoice = SDLChoiceCell(text: PICSFirstChoice, artwork: SDLArtwork(staticIcon: .key), voiceCommands: [VCPICSFirstChoice])
        let secondChoice = SDLChoiceCell(text: PICSSecondChoice, artwork: SDLArtwork(staticIcon: .microphone),  voiceCommands: [VCPICSecondChoice])
        let thirdChoice = SDLChoiceCell(text: PICSThirdChoice, artwork: SDLArtwork(staticIcon: .key), voiceCommands: [VCPICSThirdChoice])
        return [firstChoice, secondChoice, thirdChoice]
    }

    var vrHelpList: [SDLVRHelpItem] {
        let vrHelpListFirst = SDLVRHelpItem(text: VCPICSFirstChoice, image: nil)
        let vrHelpListSecond = SDLVRHelpItem(text: VCPICSecondChoice, image: nil)
        let vrHelpListThird = SDLVRHelpItem(text: VCPICSThirdChoice, image: nil)

        return [vrHelpListFirst, vrHelpListSecond, vrHelpListThird]
    }

    /// Creates a PICS with three menu items and customized voice commands
    var choiceSet: SDLChoiceSet {
        return SDLChoiceSet(title: PICSInitialPrompt, delegate: self, layout: .list, timeout: 10, initialPromptString: PICSInitialPrompt, timeoutPromptString: PICSTimeoutPrompt, helpPromptString: PICSHelpPrompt, vrHelpList: vrHelpList, choices: choiceCells)
    }

    func interactionMode(for triggerSource: SDLTriggerSource) -> SDLInteractionMode {
        return (triggerSource == .menu ? .manualOnly : .voiceRecognitionOnly)
    }
}

extension PerformInteractionManager: SDLChoiceSetDelegate {
    func choiceSet(_ choiceSet: SDLChoiceSet, didSelectChoice choice: SDLChoiceCell, withSource source: SDLTriggerSource, atRowIndex rowIndex: UInt) {
        SDLLog.d("User selected row: \(rowIndex), choice: \(choice)")
        manager.send(SDLSpeak(tts: TTSGoodJob))
    }

    func choiceSet(_ choiceSet: SDLChoiceSet, didReceiveError error: Error) {
        SDLLog.e("Error presenting choice set: \(error)")
        manager.send(SDLSpeak(tts: TTSYouMissed))
    }
}

extension PerformInteractionManager: SDLKeyboardDelegate {
    func keyboardDidAbort(withReason event: SDLKeyboardEvent) {
        SDLLog.w("Keyboard aborted with reason: \(event)")
        switch event {
        case SDLKeyboardEvent.cancelled:
            manager.send(SDLSpeak(tts: TTSYouMissed))
        case SDLKeyboardEvent.aborted:
            manager.send(SDLSpeak(tts: TTSYouMissed))
        default: break
        }
    }

    func userDidSubmitInput(_ inputText: String, withEvent source: SDLKeyboardEvent) {
        SDLLog.d("User did submit keyboard input: \(inputText), with event: \(source)")
        switch source {
        case SDLKeyboardEvent.voice: break
            // Start Voice search
        case SDLKeyboardEvent.submitted:
            manager.send(SDLSpeak(tts: TTSGoodJob))
        default: break
        }
    }

    func updateAutocomplete(withInput currentInputText: String, autoCompleteResultsHandler resultsHandler: @escaping SDLKeyboardAutoCompleteResultsHandler) {
        if currentInputText.lowercased().hasPrefix("f") {
            resultsHandler([PICSFirstChoice])
        } else if currentInputText.lowercased().hasPrefix("s") {
            resultsHandler([PICSSecondChoice])
        } else if currentInputText.lowercased().hasPrefix("t") {
            resultsHandler([PICSThirdChoice])
        } else {
            resultsHandler(nil)
        }
    }
}