USB-Meter / USB Meter / Extensions / CBManagerState.swift
Newer Older
191 lines | 6.416kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  CBManagerState.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 02/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
//import Foundation
10

            
11
import CoreBluetooth
12
import SwiftUI
13

            
14
//Manager States
15
//.poweredOff   A state that indicates Bluetooth is currently powered off.
16
//.poweredOn    A state that indicates Bluetooth is currently powered on and available to use.
17
//.resetting    A state that indicates the connection with the system service was momentarily lost.
18
//.unauthorized A state that indicates the application isn’t authorized to use the Bluetooth low energy role.
19
//.unknown      The manager’s state is unknown.
20
//.unsupported  A state that indicates this device doesn’t support the Bluetooth low energy central or client role.
21

            
22
extension CBManagerState {
23
    var description: String  {
24
        switch self {
25
        case .poweredOff:
26
            return "CBManagerState.poweredOff"
27
        case .poweredOn:
28
            return "CBManagerState.poweredOn"
29
        case .resetting:
30
            return "CBManagerState.resetting"
31
        case .unauthorized:
32
            return "CBManagerState.unauthorized"
33
        case .unknown:
34
            return "CBManagerState.unknown"
35
        case .unsupported:
36
            return "CBManagerState.unsupported"
37
        default:
38
            return "CBManagerState.other"
39
        }
40
    }
41

            
42
    var color: Color  {
43
        switch self {
44
        case .poweredOff:
45
            return Color.red
46
        case .poweredOn:
47
            return Color.blue
48
        case .resetting:
49
            return Color.green
50
        case .unauthorized:
51
            return Color.orange
52
        case .unknown:
53
            return Color.secondary
54
        case .unsupported:
55
            return Color.gray
56
        default:
57
            return Color.yellow
58
        }
59
    }
60

            
61
    var helpView: AnyView  {
62
        switch self {
63
        case .poweredOff:
64
            return AnyView(poweredOffHelperView())
65
        case .poweredOn:
66
            return AnyView(poweredOnHelperView())
67
        case .resetting:
68
            return AnyView(resettingHelperView())
69
        case .unauthorized:
70
            return AnyView(unauthorizedHelperView())
71
        case .unknown:
72
            return AnyView(unknownHelperView())
73
        case .unsupported:
74
            return AnyView(unsupportedHelperView())
75
        default:
76
            return AnyView(defaultHelperView())
77
        }
78
    }
79

            
80
    private struct poweredOffHelperView: View {
81
        var body: some View {
Bogdan Timofte authored 2 weeks ago
82
            BluetoothHelpCard(
83
                title: "Bluetooth Off",
84
                detail: "Bluetooth is turned off on this device. You can enable it in Settings > Bluetooth.",
85
                tint: .red
86
            )
Bogdan Timofte authored 2 weeks ago
87
        }
88
    }
89
    private struct poweredOnHelperView: View {
90
        var body: some View {
Bogdan Timofte authored 2 weeks ago
91
            BluetoothHelpCard(
92
                title: "Bluetooth Ready",
93
                detail: "Bluetooth is powered on and ready for scanning.",
94
                tint: .blue
95
            )
Bogdan Timofte authored 2 weeks ago
96
        }
97
    }
98

            
99
    private struct resettingHelperView: View {
100
        var body: some View {
Bogdan Timofte authored 2 weeks ago
101
            BluetoothHelpCard(
102
                title: "Bluetooth Resetting",
103
                detail: "The Bluetooth stack is temporarily resetting. Wait a moment and try again.",
104
                tint: .green
105
            )
Bogdan Timofte authored 2 weeks ago
106
        }
107
    }
108

            
109
    private struct unauthorizedHelperView: View {
110
        var body: some View {
Bogdan Timofte authored 2 weeks ago
111
            VStack(alignment: .leading, spacing: 12) {
112
                Text("Bluetooth Access Needed")
113
                    .font(.headline)
114
                Text("This application does not have permission to access Bluetooth. You can enable it in Settings.")
115
                    .font(.footnote)
116
                    .foregroundColor(.secondary)
Bogdan Timofte authored 2 weeks ago
117
                Button(action: { UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil) }) {
118
                    Text("Settings")
119
                }
Bogdan Timofte authored 2 weeks ago
120
                .padding(.horizontal, 14)
121
                .padding(.vertical, 10)
122
                .meterCard(tint: .orange, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
123
                .buttonStyle(.plain)
Bogdan Timofte authored 2 weeks ago
124
            }
Bogdan Timofte authored 2 weeks ago
125
            .frame(maxWidth: .infinity, alignment: .leading)
126
            .padding(20)
127
            .meterCard(tint: .orange, fillOpacity: 0.18, strokeOpacity: 0.24)
128
            .padding()
Bogdan Timofte authored 2 weeks ago
129
        }
130
    }
131

            
132
    private struct unknownHelperView: View {
133
        var body: some View {
Bogdan Timofte authored 2 weeks ago
134
            BluetoothHelpCard(
135
                title: "Unknown Bluetooth State",
136
                detail: "Bluetooth is reporting an unknown state. Wait a moment and check again.",
137
                tint: .secondary
138
            )
Bogdan Timofte authored 2 weeks ago
139
        }
140
    }
141

            
142
    private struct unsupportedHelperView: View {
143
        var body: some View {
Bogdan Timofte authored 2 weeks ago
144
            BluetoothHelpCard(
145
                title: "Bluetooth Unsupported",
146
                detail: "This device does not support the Bluetooth capabilities required by these USB meters.",
147
                tint: .gray
148
            )
Bogdan Timofte authored 2 weeks ago
149
        }
150
    }
151

            
152
    private struct defaultHelperView: View {
153
        var body: some View {
Bogdan Timofte authored 2 weeks ago
154
            BluetoothHelpCard(
155
                title: "Other Bluetooth State",
156
                detail: "Bluetooth is in an unexpected state. Try again, then contact the developer if it persists.",
157
                tint: .yellow
158
            )
159
        }
160
    }
161

            
162
    private struct BluetoothHelpCard: View {
163
        let title: String
164
        let detail: String
165
        let tint: Color
166

            
167
        var body: some View {
168
            ScrollView {
169
                VStack(alignment: .leading, spacing: 10) {
170
                    Text(title)
171
                        .font(.system(.title3, design: .rounded).weight(.bold))
172
                    Text(detail)
173
                        .font(.footnote)
174
                        .foregroundColor(.secondary)
175
                }
176
                .frame(maxWidth: .infinity, alignment: .leading)
177
                .padding(20)
178
                .meterCard(tint: tint, fillOpacity: 0.18, strokeOpacity: 0.24)
179
                .padding()
Bogdan Timofte authored 2 weeks ago
180
            }
Bogdan Timofte authored 2 weeks ago
181
            .background(
182
                LinearGradient(
183
                    colors: [tint.opacity(0.14), Color.clear],
184
                    startPoint: .topLeading,
185
                    endPoint: .bottomTrailing
186
                )
187
                .ignoresSafeArea()
188
            )
Bogdan Timofte authored 2 weeks ago
189
        }
190
    }
191
}