Skip to content

Commit 65e7970

Browse files
authored
Add power on button to disabled banner (#275)
* Add power on button to disabled banner * lintage
1 parent db744c9 commit 65e7970

6 files changed

Lines changed: 37 additions & 1 deletion

File tree

Topaz/TopazMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct TopazMain: App {
1515
@State var appModel: AppModel
1616

1717
init() {
18+
BluetoothSystemState.shared.requestPowerOn = liveBluetoothPowerRequest
1819
let deviceSelector = DeviceSelector()
1920
self.appModel = AppModel(
2021
appDomainProcessors: appDomainProcessors(deviceSelector: deviceSelector),

lib/Sources/App/BluetoothErrorView.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ import UIHelpers
55
struct BluetoothErrorView: View {
66

77
let state: SystemState
8+
let onPowerUp: () -> Void
89

910
@Environment(\.openURL) private var openURL
1011

12+
init(
13+
state: SystemState,
14+
onPowerUp: @escaping () -> Void = { }
15+
) {
16+
self.state = state
17+
self.onPowerUp = onPowerUp
18+
}
19+
1120
var body: some View {
1221
HStack(spacing: 16) {
1322
HStack(spacing: 8) {
@@ -29,6 +38,17 @@ struct BluetoothErrorView: View {
2938
.frame(maxWidth: 88, minHeight: 40)
3039
.background(Color.buttonDefault)
3140
.cornerRadius(24)
41+
} else if state == .poweredOff {
42+
Button {
43+
onPowerUp()
44+
} label: {
45+
Image(systemName: "power")
46+
.foregroundStyle(Color.textPrimaryInverse)
47+
.font(.headline)
48+
}
49+
.frame(maxWidth: 68, minHeight: 40)
50+
.background(Color.buttonDefault)
51+
.cornerRadius(24)
3252
}
3353
}
3454
.padding(.leading, 16)

lib/Sources/App/WebContainerModel.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ public final class WebContainerModel {
4444
self.virtualKeyboard = virtualKeyboard
4545
self.bluetoothSystem = bluetoothSystem
4646
}
47+
48+
func requestPowerOn() {
49+
bluetoothSystem.requestPowerOn()
50+
}
4751
}

lib/Sources/App/WebContainerView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ struct WebContainerView: View {
3030
VStack(spacing: 16) {
3131
if webContainerModel.shouldShowErrorState {
3232
BluetoothErrorView(
33-
state: webContainerModel.bluetoothSystem.systemState
33+
state: webContainerModel.bluetoothSystem.systemState,
34+
onPowerUp: webContainerModel.requestPowerOn
3435
)
3536
}
3637
if webContainerModel.shouldShowNavBar {

lib/Sources/BluetoothEngine/BluetoothSystemState.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public final class BluetoothSystemState {
1919
guard newValue != .unknown else { return }
2020
self.systemState = newValue
2121
}
22+
23+
public var requestPowerOn: () -> Void = { }
2224
}
2325

2426
public extension BluetoothSystemState {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import CoreBluetooth
2+
3+
public func liveBluetoothPowerRequest() {
4+
let options: [String: Any] = [
5+
CBCentralManagerOptionShowPowerAlertKey: true as NSNumber
6+
]
7+
_ = CBCentralManager(delegate: nil, queue: nil, options: options)
8+
}

0 commit comments

Comments
 (0)