// // CBManagerState.swift // USB Meter // // Created by Bogdan Timofte on 02/03/2020. // Copyright © 2020 Bogdan Timofte. All rights reserved. // //import Foundation import CoreBluetooth import SwiftUI //Manager States //.poweredOff A state that indicates Bluetooth is currently powered off. //.poweredOn A state that indicates Bluetooth is currently powered on and available to use. //.resetting A state that indicates the connection with the system service was momentarily lost. //.unauthorized A state that indicates the application isn’t authorized to use the Bluetooth low energy role. //.unknown The manager’s state is unknown. //.unsupported A state that indicates this device doesn’t support the Bluetooth low energy central or client role. extension CBManagerState { var description: String { switch self { case .poweredOff: return "CBManagerState.poweredOff" case .poweredOn: return "CBManagerState.poweredOn" case .resetting: return "CBManagerState.resetting" case .unauthorized: return "CBManagerState.unauthorized" case .unknown: return "CBManagerState.unknown" case .unsupported: return "CBManagerState.unsupported" default: return "CBManagerState.other" } } var color: Color { switch self { case .poweredOff: return Color.red case .poweredOn: return Color.blue case .resetting: return Color.green case .unauthorized: return Color.orange case .unknown: return Color.secondary case .unsupported: return Color.gray default: return Color.yellow } } var helpView: AnyView { switch self { case .poweredOff: return AnyView(poweredOffHelperView()) case .poweredOn: return AnyView(poweredOnHelperView()) case .resetting: return AnyView(resettingHelperView()) case .unauthorized: return AnyView(unauthorizedHelperView()) case .unknown: return AnyView(unknownHelperView()) case .unsupported: return AnyView(unsupportedHelperView()) default: return AnyView(defaultHelperView()) } } private struct poweredOffHelperView: View { var body: some View { Text("Bluetooth is turned off on this device. Tou can turn it on in Settings->Bluetooth") } } private struct poweredOnHelperView: View { var body: some View { Text("Bluetooth is up an running") } } private struct resettingHelperView: View { var body: some View { VStack { Text("Bluetooth is resetting") Text("Maybe wait for a while...") } } } private struct unauthorizedHelperView: View { var body: some View { HStack { Text("This application does not have permission to access Bluetooth. You can give it in ") Button(action: { UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil) }) { Text("Settings") } } } } private struct unknownHelperView: View { var body: some View { VStack { Text("Bluetooth state is unknown!") Text("There is no help available for this situation.") } } } private struct unsupportedHelperView: View { var body: some View { VStack { Text("Your device does not have required capabilities to establish Bluetooth connections with USB Meters") Text("There is no help available for this situation.") } } } private struct defaultHelperView: View { var body: some View { VStack { Text("Unknown Bluetooth state.") Text("You may contact develloper.") } } } }