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
|
//
// ButtonManager.swift
// SmartDeviceLink
//
// Created by Nicole on 4/11/18.
// Copyright © 2018 smartdevicelink. All rights reserved.
//
import Foundation
import SmartDeviceLink
import SmartDeviceLinkSwift
typealias RefreshUIHandler = (() -> Void)
class ButtonManager: NSObject {
fileprivate let sdlManager: SDLManager!
fileprivate var refreshUIHandler: RefreshUIHandler?
/// Textfields are visible if true; hidden if false
public fileprivate(set) var textEnabled: Bool {
didSet {
guard let refreshUIHandler = refreshUIHandler else { return }
refreshUIHandler()
}
}
/// UI images are visible if true; hidden if false
public fileprivate(set) var imagesEnabled: Bool {
didSet {
guard let refreshUIHandler = refreshUIHandler else { return }
refreshUIHandler()
}
}
private var isSubtleAlertAllowed: Bool {
return sdlManager.permissionManager.isRPCNameAllowed(.subtleAlert)
}
private var isAlertAllowed: Bool {
return sdlManager.permissionManager.isRPCNameAllowed(.alert)
}
init(sdlManager: SDLManager, updateScreenHandler: RefreshUIHandler? = nil) {
self.sdlManager = sdlManager
self.refreshUIHandler = updateScreenHandler
textEnabled = true
imagesEnabled = true
}
/// An array of all the soft buttons
///
/// - Parameter manager: The SDL Manager
/// - Returns: An array of all soft buttons for the UI
func allScreenSoftButtons() -> [SDLSoftButtonObject] {
return [softButtonAlert, softButtonSubtleAlert, softButtonTextVisible, softButtonImagesVisible]
}
}
// MARK: - Custom Soft Buttons
extension ButtonManager {
/// Returns a soft button that shows an alert when tapped.
private var softButtonAlert: SDLSoftButtonObject {
let imageAndTextState = SDLSoftButtonState(stateName: AlertSoftButtonImageAndTextState, text: AlertSoftButtonText, image: UIImage(named: AlertBWIconName)?.withRenderingMode(.alwaysTemplate))
let textState = SDLSoftButtonState(stateName: AlertSoftButtonTextState, text: AlertSoftButtonText, image: nil)
let initialButtonStateName = self.imagesEnabled ? imageAndTextState.name : textState.name
return SDLSoftButtonObject(name: AlertSoftButton, states: [imageAndTextState, textState], initialStateName: initialButtonStateName) { [weak self] (buttonPress, buttonEvent) in
guard let self = self, buttonPress != nil else { return }
if (self.isAlertAllowed) {
AlertManager.sendAlert(imageName: CarBWIconImageName, textField1: AlertMessageText, sdlManager: self.sdlManager)
} else if (self.isSubtleAlertAllowed) {
AlertManager.sendSubtleAlert(imageName: CarBWIconImageName, textField1: AlertMessageText, sdlManager: self.sdlManager)
} else {
SDLLog.w("The module does not support the Alert request or the Subtle Alert request")
}
}
}
/// Returns a soft button that shows a subtle alert when tapped. If the subtle alert is not supported, then a regular alert is shown.
private var softButtonSubtleAlert: SDLSoftButtonObject {
let imageAndTextState = SDLSoftButtonState(stateName: SubtleAlertSoftButtonImageAndTextState, text: SubtleAlertSoftButtonText, image: UIImage(named: BatteryFullBWIconName)?.withRenderingMode(.alwaysTemplate))
let textState = SDLSoftButtonState(stateName: SubtleAlertSoftButtonTextState, text: SubtleAlertSoftButtonText, image: nil)
let initialButtonStateName = self.imagesEnabled ? imageAndTextState.name : textState.name
return SDLSoftButtonObject(name: SubtleAlertSoftButton, states: [imageAndTextState, textState], initialStateName: initialButtonStateName) { [weak self] (buttonPress, buttonEvent) in
guard let self = self, buttonPress != nil else { return }
if (self.isSubtleAlertAllowed) {
AlertManager.sendSubtleAlert(imageName: BatteryEmptyBWIconName, textField1: SubtleAlertHeaderText, textField2: SubtleAlertSubheaderText, sdlManager: self.sdlManager)
} else if (self.isAlertAllowed) {
AlertManager.sendAlert(imageName: BatteryEmptyBWIconName, textField1: SubtleAlertHeaderText, textField2: SubtleAlertSubheaderText, sdlManager: self.sdlManager)
} else {
SDLLog.w("The module does not support the Alert request or the Subtle Alert request")
}
}
}
/// Returns a soft button that toggles the textfield visibility state.
private var softButtonTextVisible: SDLSoftButtonObject {
let textVisibleState = SDLSoftButtonState(stateName: TextVisibleSoftButtonTextOnState, text: TextVisibleSoftButtonTextOnText, artwork: nil)
let textNotVisibleState = SDLSoftButtonState(stateName: TextVisibleSoftButtonTextOffState, text: TextVisibleSoftButtonTextOffText, image: nil)
let initialButtonStateName = self.textEnabled ? textVisibleState.name : textNotVisibleState.name
return SDLSoftButtonObject(name: TextVisibleSoftButton, states: [textVisibleState, textNotVisibleState], initialStateName: initialButtonStateName) { [unowned self] (buttonPress, buttonEvent) in
guard buttonPress != nil else { return }
self.textEnabled = !self.textEnabled
// Update the button state
let softButton = self.sdlManager.screenManager.softButtonObjectNamed(TextVisibleSoftButton)
softButton?.transitionToNextState()
}
}
/// Returns a soft button that toggles the image visibility state.
private var softButtonImagesVisible: SDLSoftButtonObject {
let imagesVisibleState = SDLSoftButtonState(stateName: ImagesVisibleSoftButtonImageOnState, text: ImagesVisibleSoftButtonImageOnText, image: nil)
let imagesNotVisibleState = SDLSoftButtonState(stateName: ImagesVisibleSoftButtonImageOffState, text: ImagesVisibleSoftButtonImageOffText, image: nil)
let initialButtonStateName = self.imagesEnabled ? imagesVisibleState.name : imagesNotVisibleState.name
return SDLSoftButtonObject(name: ImagesVisibleSoftButton, states: [imagesVisibleState, imagesNotVisibleState], initialStateName: initialButtonStateName) { [weak self] (buttonPress, buttonEvent) in
guard let self = self, let sdlManager = self.sdlManager, buttonPress != nil else { return }
self.imagesEnabled = !self.imagesEnabled
if let imagesVisibleSoftButton = sdlManager.screenManager.softButtonObjectNamed(ImagesVisibleSoftButton) {
imagesVisibleSoftButton.transitionToNextState()
}
if let alertSoftButton = sdlManager.screenManager.softButtonObjectNamed(AlertSoftButton) {
alertSoftButton.transitionToNextState()
}
if let subtleAlertSoftButton = sdlManager.screenManager.softButtonObjectNamed(SubtleAlertSoftButton) {
subtleAlertSoftButton.transitionToNextState()
}
}
}
}
|