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

181 lines
5.7 KiB
Swift
Raw Normal View History

//
// DeviceSettings.swift
// Meshtastic Apple
//
// Copyright (c) Garth Vander Houwen 6/7/22.
//
import SwiftUI
struct DisplayConfig: View {
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@Environment(\.dismiss) private var goBack
2022-06-21 10:02:05 -07:00
var node: NodeInfoEntity?
2022-06-21 10:02:05 -07:00
@State private var isPresentingSaveConfirm: Bool = false
@State var hasChanges = false
@State var screenOnSeconds = 0
@State var screenCarouselInterval = 0
@State var gpsFormat = 0
2022-08-07 08:31:39 -07:00
@State var compassNorthTop = false
2022-10-22 08:45:53 -07:00
@State var flipScreen = false
@State var oledType = 0
var body: some View {
VStack {
Form {
2022-08-07 08:31:39 -07:00
Section(header: Text("Device Screen")) {
Picker("Screen on for", selection: $screenOnSeconds ) {
2022-06-29 20:04:20 -07:00
ForEach(ScreenOnIntervals.allCases) { soi in
Text(soi.description)
}
}
.pickerStyle(DefaultPickerStyle())
2022-06-22 09:05:56 -07:00
Text("How long the screen remains on after the user button is pressed or messages are received.")
.font(.caption)
Picker("Carousel Interval", selection: $screenCarouselInterval ) {
2022-06-29 20:04:20 -07:00
ForEach(ScreenCarouselIntervals.allCases) { sci in
Text(sci.description)
}
}
.pickerStyle(DefaultPickerStyle())
Text("Automatically toggles to the next page on the screen like a carousel, based the specified interval.")
.font(.caption)
2022-08-07 08:31:39 -07:00
Toggle(isOn: $compassNorthTop) {
2022-08-07 08:31:39 -07:00
Label("Always point north", systemImage: "location.north.circle")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
Text("The compass heading on the screen outside of the circle will always point north.")
.font(.caption)
2022-10-22 08:45:53 -07:00
Toggle(isOn: $flipScreen) {
Label("Flip Screen", systemImage: "pip.swap")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
Text("Flip screen vertically")
.font(.caption)
Picker("OLED Type", selection: $oledType ) {
ForEach(OledTypes.allCases) { ot in
Text(ot.description)
}
}
.pickerStyle(DefaultPickerStyle())
Text("Override automatic OLED screen detection.")
.font(.caption)
2022-10-22 08:45:53 -07:00
}
Section(header: Text("Format")) {
Picker("GPS Format", selection: $gpsFormat ) {
2022-06-21 10:02:05 -07:00
ForEach(GpsFormats.allCases) { lu in
Text(lu.description)
}
}
.pickerStyle(DefaultPickerStyle())
Text("The format used to display GPS coordinates on the device screen.")
.font(.caption)
.listRowSeparator(.visible)
}
}
2022-06-21 13:34:57 -07:00
.disabled(bleManager.connectedPeripheral == nil)
2022-06-21 10:02:05 -07:00
Button {
isPresentingSaveConfirm = true
} label: {
2022-12-12 20:35:38 -08:00
Label("save", systemImage: "square.and.arrow.down")
2022-06-21 10:02:05 -07:00
}
.disabled(bleManager.connectedPeripheral == nil || !hasChanges)
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.confirmationDialog(
2022-12-13 08:47:14 -08:00
"are.you.sure",
2022-06-21 10:02:05 -07:00
isPresented: $isPresentingSaveConfirm
) {
Button("Save Display Config to \(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown")?") {
var dc = Config.DisplayConfig()
dc.gpsFormat = GpsFormats(rawValue: gpsFormat)!.protoEnumValue()
dc.screenOnSecs = UInt32(screenOnSeconds)
dc.autoScreenCarouselSecs = UInt32(screenCarouselInterval)
2022-08-07 08:31:39 -07:00
dc.compassNorthTop = compassNorthTop
2022-10-22 08:45:53 -07:00
dc.flipScreen = flipScreen
dc.oled = OledTypes(rawValue: oledType)!.protoEnumValue()
2022-06-21 10:02:05 -07:00
let adminMessageId = bleManager.saveDisplayConfig(config: dc, fromUser: node!.user!, toUser: node!.user!)
if adminMessageId > 0 {
2022-06-21 10:02:05 -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 10:02:05 -07:00
}
}
}
}
2022-12-13 07:49:46 -08:00
.navigationTitle("display.config")
.navigationBarItems(trailing:
ZStack {
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????")
})
.onAppear {
self.bleManager.context = context
self.gpsFormat = Int(node?.displayConfig?.gpsFormat ?? 0)
self.screenOnSeconds = Int(node?.displayConfig?.screenOnSeconds ?? 0)
self.screenCarouselInterval = Int(node?.displayConfig?.screenCarouselInterval ?? 0)
self.compassNorthTop = node?.displayConfig?.compassNorthTop ?? false
2022-10-22 08:45:53 -07:00
self.flipScreen = node?.displayConfig?.flipScreen ?? false
self.oledType = Int(node?.displayConfig?.oledType ?? 0)
self.hasChanges = false
2022-06-21 10:02:05 -07:00
}
.onChange(of: screenOnSeconds) { newScreenSecs in
2022-07-11 16:18:16 -07:00
if node != nil && node!.displayConfig != nil {
if newScreenSecs != node!.displayConfig!.screenOnSeconds { hasChanges = true }
2022-06-21 10:02:05 -07:00
}
}
.onChange(of: screenCarouselInterval) { newCarouselSecs in
2022-07-11 16:18:16 -07:00
if node != nil && node!.displayConfig != nil {
if newCarouselSecs != node!.displayConfig!.screenCarouselInterval { hasChanges = true }
2022-06-21 10:02:05 -07:00
}
}
2022-08-07 08:31:39 -07:00
.onChange(of: compassNorthTop) { newCompassNorthTop in
if node != nil && node!.displayConfig != nil {
if newCompassNorthTop != node!.displayConfig!.compassNorthTop { hasChanges = true }
}
}
2022-06-21 10:02:05 -07:00
.onChange(of: gpsFormat) { newGpsFormat in
2022-07-11 16:18:16 -07:00
if node != nil && node!.displayConfig != nil {
if newGpsFormat != node!.displayConfig!.gpsFormat { hasChanges = true }
2022-06-21 10:02:05 -07:00
}
}
2022-10-22 08:45:53 -07:00
.onChange(of: flipScreen) { newFlipScreen in
if node != nil && node!.displayConfig != nil {
if newFlipScreen != node!.displayConfig!.flipScreen { hasChanges = true }
}
}
.onChange(of: oledType) { newOledType in
if node != nil && node!.displayConfig != nil {
if newOledType != node!.displayConfig!.oledType { hasChanges = true }
}
}
}
}