Meshtastic-Apple/Meshtastic/Views/Settings/Config/DeviceConfig.swift

238 lines
6.4 KiB
Swift
Raw Normal View History

2022-06-13 20:43:51 -07:00
//
// DeviceConfig.swift
// Meshtastic Apple
//
// Copyright (c) Garth Vander Houwen 6/13/22.
//
import SwiftUI
struct DeviceConfig: View {
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@Environment(\.dismiss) private var goBack
2022-06-21 02:43:37 -07:00
var node: NodeInfoEntity?
2022-06-21 02:43:37 -07:00
@State private var isPresentingNodeDBResetConfirm = false
2022-07-26 21:52:36 -07:00
@State private var isPresentingFactoryResetConfirm = false
@State private var isPresentingSaveConfirm = false
2022-06-21 02:43:37 -07:00
@State var hasChanges = false
2022-06-13 20:43:51 -07:00
@State var deviceRole = 0
2022-12-05 19:47:56 -08:00
@State var buzzerGPIO = 0
@State var buttonGPIO = 0
2022-06-13 20:43:51 -07:00
@State var serialEnabled = true
@State var debugLogEnabled = false
var body: some View {
2022-06-20 00:13:04 -07:00
VStack {
2022-06-13 20:43:51 -07:00
2022-06-20 00:13:04 -07:00
Form {
2022-12-13 08:47:14 -08:00
Section(header: Text("options")) {
2022-06-13 20:43:51 -07:00
2022-06-20 00:13:04 -07:00
Picker("Device Role", selection: $deviceRole ) {
ForEach(DeviceRoles.allCases) { dr in
Text(dr.description)
2022-06-13 20:43:51 -07:00
}
}
.pickerStyle(DefaultPickerStyle())
2022-06-20 00:13:04 -07:00
.padding(.top, 10)
.padding(.bottom, 10)
}
Section(header: Text("Debug")) {
2022-06-13 20:43:51 -07:00
2022-06-20 00:13:04 -07:00
Toggle(isOn: $serialEnabled) {
2022-06-20 00:13:04 -07:00
Label("Serial Console", systemImage: "terminal")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
Toggle(isOn: $debugLogEnabled) {
2022-06-13 20:43:51 -07:00
2022-06-20 00:13:04 -07:00
Label("Debug Log", systemImage: "ant.fill")
2022-06-13 20:43:51 -07:00
}
2022-06-20 00:13:04 -07:00
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
}
2022-10-02 09:19:03 -07:00
2022-12-05 19:47:56 -08:00
Section(header: Text("GPIO")) {
Picker("Button GPIO", selection: $buttonGPIO) {
ForEach(0..<40) {
if $0 == 0 {
Text("Unset")
} else {
Text("Pin \($0)")
}
}
}
.pickerStyle(DefaultPickerStyle())
Picker("Buzzer GPIO", selection: $buzzerGPIO) {
ForEach(0..<40) {
if $0 == 0 {
Text("Unset")
} else {
Text("Pin \($0)")
}
}
}
.pickerStyle(DefaultPickerStyle())
}
2022-06-20 00:13:04 -07:00
}
2022-06-21 13:34:57 -07:00
.disabled(bleManager.connectedPeripheral == nil)
2022-06-20 00:13:04 -07:00
2022-10-02 09:19:03 -07:00
HStack {
Button("Reset NodeDB", role: .destructive) {
isPresentingNodeDBResetConfirm = true
2022-10-02 09:19:03 -07:00
}
.disabled(bleManager.connectedPeripheral == nil)
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.confirmationDialog(
2022-12-13 08:47:14 -08:00
"are.you.sure",
isPresented: $isPresentingNodeDBResetConfirm,
2022-10-02 09:19:03 -07:00
titleVisibility: .visible
) {
2022-10-03 21:22:18 -07:00
Button("Erase all device and app data?", role: .destructive) {
2022-10-07 15:57:57 -07:00
if bleManager.sendNodeDBReset(destNum: bleManager.connectedPeripheral.num) {
bleManager.disconnectPeripheral()
clearCoreDataDatabase(context: context)
2022-10-07 15:57:57 -07:00
} else {
print("NodeDB Reset Failed")
2022-10-02 09:19:03 -07:00
}
}
}
Button("Factory Reset", role: .destructive) {
isPresentingFactoryResetConfirm = true
}
.disabled(bleManager.connectedPeripheral == nil)
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.confirmationDialog(
"All device and app data will be deleted. You will also need to forget your devices under Settings > Bluetooth.",
2022-10-02 09:19:03 -07:00
isPresented: $isPresentingFactoryResetConfirm,
titleVisibility: .visible
) {
Button("Factory reset your device and app? ", role: .destructive) {
2022-10-02 09:19:03 -07:00
2022-10-07 15:57:57 -07:00
if bleManager.sendFactoryReset(destNum: bleManager.connectedPeripheral.num) {
bleManager.disconnectPeripheral()
clearCoreDataDatabase(context: context)
2022-10-07 15:57:57 -07:00
} else {
print("Factory Reset Failed")
2022-10-02 09:19:03 -07:00
}
}
}
}
2022-06-21 02:43:37 -07:00
HStack {
2022-06-21 02:43:37 -07:00
Button {
isPresentingSaveConfirm = true
} label: {
2022-12-12 20:35:38 -08:00
Label("save", systemImage: "square.and.arrow.down")
2022-06-21 02:43:37 -07:00
}
.disabled(bleManager.connectedPeripheral == nil || !hasChanges)
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.confirmationDialog(
2022-06-13 20:43:51 -07:00
2022-09-23 21:41:07 -07:00
"Are you sure you want to save?",
isPresented: $isPresentingSaveConfirm,
titleVisibility: .visible
2022-06-21 02:43:37 -07:00
) {
Button("Save Device Config to \(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown")?") {
2022-06-21 02:43:37 -07:00
var dc = Config.DeviceConfig()
dc.role = DeviceRoles(rawValue: deviceRole)!.protoEnumValue()
2022-09-10 18:38:00 -07:00
dc.serialEnabled = serialEnabled
2022-06-21 02:43:37 -07:00
dc.debugLogEnabled = debugLogEnabled
2022-12-05 19:47:56 -08:00
dc.buttonGpio = UInt32(buttonGPIO)
dc.buzzerGpio = UInt32(buzzerGPIO)
2022-06-21 02:43:37 -07:00
let adminMessageId = bleManager.saveDeviceConfig(config: dc, fromUser: node!.user!, toUser: node!.user!)
if adminMessageId > 0 {
2022-06-21 02:43:37 -07:00
// Should show a saved successfully alert once I know that to be true
// for now just disable the button after a successful save
hasChanges = false
goBack()
2022-06-21 02:43:37 -07:00
}
2022-09-23 21:41:07 -07:00
}
}
message: {
Text("After device config saves the node will reboot.")
2022-06-21 02:43:37 -07:00
}
2022-06-13 20:43:51 -07:00
}
2022-06-20 00:13:04 -07:00
Spacer()
}
2022-12-13 07:49:46 -08:00
.navigationTitle("device.config")
2022-06-20 00:13:04 -07:00
.navigationBarItems(trailing:
2022-06-13 20:43:51 -07:00
2022-06-20 00:13:04 -07:00
ZStack {
2022-06-13 20:43:51 -07:00
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????")
2022-06-20 00:13:04 -07:00
})
.onAppear {
2022-10-18 19:50:42 -07:00
self.bleManager.context = context
self.deviceRole = Int(node?.deviceConfig?.role ?? 0)
self.serialEnabled = (node?.deviceConfig?.serialEnabled ?? true)
self.debugLogEnabled = node?.deviceConfig?.debugLogEnabled ?? false
2022-12-05 19:47:56 -08:00
self.buttonGPIO = Int(node?.deviceConfig?.buttonGpio ?? 0)
self.buzzerGPIO = Int(node?.deviceConfig?.buzzerGpio ?? 0)
2022-10-18 19:50:42 -07:00
self.hasChanges = false
2022-06-21 02:43:37 -07:00
}
.onChange(of: deviceRole) { newRole in
2022-07-11 16:18:16 -07:00
if node != nil && node!.deviceConfig != nil {
if newRole != node!.deviceConfig!.role { hasChanges = true }
}
2022-06-21 02:43:37 -07:00
}
.onChange(of: serialEnabled) { newSerial in
2022-07-26 21:52:36 -07:00
2022-07-11 16:18:16 -07:00
if node != nil && node!.deviceConfig != nil {
2022-06-21 02:43:37 -07:00
2022-07-11 16:18:16 -07:00
if newSerial != node!.deviceConfig!.serialEnabled { hasChanges = true }
}
2022-06-21 02:43:37 -07:00
}
.onChange(of: debugLogEnabled) { newDebugLog in
2022-07-11 16:18:16 -07:00
if node != nil && node!.deviceConfig != nil {
if newDebugLog != node!.deviceConfig!.debugLogEnabled { hasChanges = true }
}
2022-06-13 20:43:51 -07:00
}
2022-12-05 19:47:56 -08:00
.onChange(of: buttonGPIO) { newButtonGPIO in
if node != nil && node!.deviceConfig != nil {
if newButtonGPIO != node!.deviceConfig!.buttonGpio { hasChanges = true }
}
}
.onChange(of: buzzerGPIO) { newBuzzerGPIO in
if node != nil && node!.deviceConfig != nil {
if newBuzzerGPIO != node!.deviceConfig!.buttonGpio { hasChanges = true }
}
}
2022-06-13 20:43:51 -07:00
}
}