2022-06-13 20:43:51 -07:00
//
// D e v i c e C o n f i g . s w i f t
// M e s h t a s t i c A p p l e
//
// C o p y r i g h t ( c ) G a r t h V a n d e r H o u w e n 6 / 1 3 / 2 2 .
//
import SwiftUI
// D e f a u l t o f 0 i s O n e M i n u t e
enum DeviceRoles : Int , CaseIterable , Identifiable {
case client = 0
case clientMute = 1
case router = 2
case routerClient = 3
var id : Int { self . rawValue }
var description : String {
get {
switch self {
case . client :
return " Client (default) "
case . clientMute :
return " Client Mute - Packets will not hop over this node, does not contribute to routing packets for mesh. "
case . router :
return " Router - Mesh packets will prefer to be routed over this node. This node will not be used by client apps. The wifi/ble radios and the oled screen will be put to sleep. "
case . routerClient :
return " Router Client - Mesh packets will prefer to be routed over this node. The Router Client can be used as both a Router and an app connected Client. "
}
}
}
}
struct DeviceConfig : View {
@ Environment ( \ . managedObjectContext ) var context
@ EnvironmentObject var bleManager : BLEManager
@ State var deviceRole = 0
@ State var serialEnabled = true
@ State var debugLogEnabled = false
2022-06-18 00:08:01 -07:00
@ State private var isPresentingFactoryResetConfirm : Bool = false
2022-06-13 20:43:51 -07:00
var body : some View {
2022-06-18 00:08:01 -07:00
ZStack {
VStack {
2022-06-13 20:43:51 -07:00
2022-06-18 00:08:01 -07:00
Form {
2022-06-13 20:43:51 -07:00
2022-06-18 00:08:01 -07:00
Section ( header : Text ( " Options " ) ) {
Picker ( " Device Role " , selection : $ deviceRole ) {
ForEach ( DeviceRoles . allCases ) { dr in
Text ( dr . description )
}
2022-06-13 20:43:51 -07:00
}
2022-06-18 00:08:01 -07:00
. pickerStyle ( InlinePickerStyle ( ) )
. padding ( . top , 10 )
. padding ( . bottom , 10 )
2022-06-13 20:43:51 -07:00
}
2022-06-18 00:08:01 -07:00
Section ( header : Text ( " Debug " ) ) {
Toggle ( isOn : $ serialEnabled ) {
Label ( " Serial Console " , systemImage : " terminal " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
Toggle ( isOn : $ debugLogEnabled ) {
2022-06-13 20:43:51 -07:00
2022-06-18 00:08:01 -07:00
Label ( " Debug Log " , systemImage : " ant.fill " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
2022-06-13 20:43:51 -07:00
}
2022-06-18 00:08:01 -07:00
}
Button ( " Factory Reset " , role : . destructive ) {
2022-06-13 20:43:51 -07:00
2022-06-18 00:08:01 -07:00
isPresentingFactoryResetConfirm = true
}
. disabled ( bleManager . connectedPeripheral = = nil )
. buttonStyle ( . bordered )
. buttonBorderShape ( . capsule )
. controlSize ( . large )
. padding ( )
. confirmationDialog (
" Are you sure? " ,
isPresented : $ isPresentingFactoryResetConfirm
) {
Button ( " Erase all device settings? " , role : . destructive ) {
if ! bleManager . sendFactoryReset ( destNum : bleManager . connectedPeripheral . num , wantResponse : false ) {
print ( " Factory Reset Failed " )
}
2022-06-13 20:43:51 -07:00
}
}
2022-06-18 00:08:01 -07:00
Spacer ( )
2022-06-13 20:43:51 -07:00
}
2022-06-18 00:08:01 -07:00
2022-06-13 20:43:51 -07:00
. navigationTitle ( " Device Config " )
. navigationBarItems ( trailing :
ZStack {
2022-06-17 18:20:30 -07:00
ConnectedDevice ( bluetoothOn : bleManager . isSwitchedOn , deviceConnected : bleManager . connectedPeripheral != nil , name : ( bleManager . connectedPeripheral != nil ) ? bleManager . connectedPeripheral . shortName : " ????? " )
2022-06-13 20:43:51 -07:00
} )
. onAppear {
self . bleManager . context = context
}
. navigationViewStyle ( StackNavigationViewStyle ( ) )
}
}
}