2022-06-12 01:25:42 -07:00
//
// D e v i c e S e t t i n g s . 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 / 7 / 2 2 .
//
import SwiftUI
struct DisplayConfig : View {
2023-03-06 10:33:18 -08:00
2022-06-12 01:25:42 -07:00
@ Environment ( \ . managedObjectContext ) var context
@ EnvironmentObject var bleManager : BLEManager
2022-12-09 18:19:00 -08:00
@ Environment ( \ . dismiss ) private var goBack
2023-03-06 10:33:18 -08:00
2022-07-07 00:29:52 -07:00
var node : NodeInfoEntity ?
2023-03-06 10:33:18 -08:00
2022-06-21 10:02:05 -07:00
@ State private var isPresentingSaveConfirm : Bool = false
@ State var hasChanges = false
2022-06-12 01:25:42 -07:00
2022-06-12 22:07:58 -07:00
@ State var screenOnSeconds = 0
@ State var screenCarouselInterval = 0
@ State var gpsFormat = 0
2022-08-07 08:31:39 -07:00
@ State var compassNorthTop = false
2023-03-25 00:41:57 -07:00
@ State var wakeOnTapOrMotion = false
2022-10-22 08:45:53 -07:00
@ State var flipScreen = false
2022-12-11 14:14:08 -08:00
@ State var oledType = 0
2023-01-25 23:01:45 -08:00
@ State var displayMode = 0
2023-03-06 10:33:18 -08:00
2022-06-12 01:25:42 -07:00
var body : some View {
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
Form {
2023-03-13 19:17:43 -07:00
if node != nil && node ? . metadata = = nil && node ? . num ? ? 0 != bleManager . connectedPeripheral ? . num ? ? 0 {
Text ( " There has been no response to a request for device metadata over the admin channel for this node. " )
. font ( . callout )
. foregroundColor ( . orange )
2023-03-14 12:44:10 -07:00
2023-03-13 19:17:43 -07:00
} else if node != nil && node ? . num ? ? 0 != bleManager . connectedPeripheral ? . num ? ? 0 {
// L e t u s e r s k n o w w h a t i s g o i n g o n i f t h e y a r e u s i n g r e m o t e a d m i n a n d d o n ' t h a v e t h e c o n f i g y e t
2023-03-14 12:44:10 -07:00
if node ? . displayConfig = = nil {
2023-03-13 19:17:43 -07:00
Text ( " Display config data was requested over the admin channel but no response has been returned from the remote node. You can check the status of admin message requests in the admin message log. " )
. font ( . callout )
. foregroundColor ( . orange )
} else {
Text ( " Remote administration for: \( node ? . user ? . longName ? ? " Unknown " ) " )
. font ( . title3 )
2023-03-19 18:37:23 -07:00
. onAppear {
setDisplayValues ( )
}
2023-03-13 19:17:43 -07:00
}
2023-03-14 12:44:10 -07:00
} else if node != nil && node ? . num ? ? 0 = = bleManager . connectedPeripheral ? . num ? ? 0 {
2023-03-13 19:17:43 -07:00
Text ( " Configuration for: \( node ? . user ? . longName ? ? " Unknown " ) " )
. font ( . title3 )
} else {
Text ( " Please connect to a radio to configure settings. " )
. font ( . callout )
. foregroundColor ( . orange )
}
2022-12-30 11:08:59 -08:00
Section ( header : Text ( " Device Screen " ) ) {
2023-01-25 23:01:45 -08:00
Picker ( " Display Mode " , selection : $ displayMode ) {
ForEach ( DisplayModes . allCases ) { dm in
Text ( dm . description )
2022-06-12 01:25:42 -07:00
}
2022-12-30 11:08:59 -08:00
}
. pickerStyle ( DefaultPickerStyle ( ) )
2023-01-25 23:01:45 -08:00
Text ( " Override automatic OLED screen detection. " )
2022-12-30 11:08:59 -08:00
. font ( . caption )
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
Toggle ( isOn : $ compassNorthTop ) {
2022-06-12 01:25:42 -07:00
2022-12-30 11:08:59 -08: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 )
2023-03-25 00:41:57 -07:00
Toggle ( isOn : $ wakeOnTapOrMotion ) {
Label ( " Wake Screen on tap or motion " , systemImage : " gyroscope " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
Text ( " Requires that there be an accelerometer on your device. " )
. font ( . caption )
2022-12-30 11:08:59 -08:00
Toggle ( isOn : $ flipScreen ) {
2022-10-22 08:45:53 -07:00
2022-12-30 11:08:59 -08:00
Label ( " Flip Screen " , systemImage : " pip.swap " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
Text ( " Flip screen vertically " )
. font ( . caption )
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
Picker ( " OLED Type " , selection : $ oledType ) {
ForEach ( OledTypes . allCases ) { ot in
Text ( ot . description )
2022-12-11 14:14:08 -08:00
}
2022-06-12 01:25:42 -07:00
}
2022-12-30 11:08:59 -08:00
. pickerStyle ( DefaultPickerStyle ( ) )
Text ( " Override automatic OLED screen detection. " )
. font ( . caption )
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
}
2023-01-25 23:01:45 -08:00
Section ( header : Text ( " Timing & Format " ) ) {
Picker ( " Screen on for " , selection : $ screenOnSeconds ) {
ForEach ( ScreenOnIntervals . allCases ) { soi in
Text ( soi . description )
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
Text ( " How long the screen remains on after the user button is pressed or messages are received. " )
. font ( . caption )
2023-03-06 10:33:18 -08:00
2023-01-25 23:01:45 -08:00
Picker ( " Carousel Interval " , selection : $ screenCarouselInterval ) {
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 )
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
Picker ( " GPS Format " , selection : $ gpsFormat ) {
ForEach ( GpsFormats . allCases ) { lu in
Text ( lu . description )
2022-06-12 01:25:42 -07:00
}
}
2022-12-30 11:08:59 -08:00
. pickerStyle ( DefaultPickerStyle ( ) )
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
Text ( " The format used to display GPS coordinates on the device screen. " )
. font ( . caption )
. listRowSeparator ( . visible )
2022-06-12 01:25:42 -07:00
}
2022-12-30 11:08:59 -08:00
}
2023-01-31 22:59:43 -08:00
. disabled ( self . bleManager . connectedPeripheral = = nil || node ? . displayConfig = = nil )
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
Button {
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
isPresentingSaveConfirm = true
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
} label : {
2023-03-06 10:33:18 -08:00
2022-12-30 11:08:59 -08:00
Label ( " save " , systemImage : " square.and.arrow.down " )
}
. disabled ( bleManager . connectedPeripheral = = nil || ! hasChanges )
. buttonStyle ( . bordered )
. buttonBorderShape ( . capsule )
. controlSize ( . large )
. padding ( )
. confirmationDialog (
" are.you.sure " ,
isPresented : $ isPresentingSaveConfirm
) {
2023-05-05 09:27:24 -07:00
let nodeName = node ? . user ? . longName ? ? " unknown " . localized
let buttonText = String . localizedStringWithFormat ( " save.config %@ " . localized , nodeName )
2023-01-09 18:34:43 -08:00
Button ( buttonText ) {
2023-01-31 22:08:03 -08:00
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral . num , context : context )
2023-02-06 18:45:03 -08:00
if connectedNode != nil {
var dc = Config . DisplayConfig ( )
dc . gpsFormat = GpsFormats ( rawValue : gpsFormat ) ! . protoEnumValue ( )
dc . screenOnSecs = UInt32 ( screenOnSeconds )
dc . autoScreenCarouselSecs = UInt32 ( screenCarouselInterval )
dc . compassNorthTop = compassNorthTop
2023-03-25 00:41:57 -07:00
dc . wakeOnTapOrMotion = wakeOnTapOrMotion
2023-02-06 18:45:03 -08:00
dc . flipScreen = flipScreen
dc . oled = OledTypes ( rawValue : oledType ) ! . protoEnumValue ( )
dc . displaymode = DisplayModes ( rawValue : displayMode ) ! . protoEnumValue ( )
2023-03-06 10:33:18 -08:00
2023-03-15 09:27:33 -07:00
let adminMessageId = bleManager . saveDisplayConfig ( config : dc , fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
2023-02-06 18:45:03 -08:00
if adminMessageId > 0 {
2023-03-06 10:33:18 -08:00
2023-02-06 18:45:03 -08:00
// S h o u l d s h o w a s a v e d s u c c e s s f u l l y a l e r t o n c e I k n o w t h a t t o b e t r u e
// f o r n o w j u s t d i s a b l e t h e b u t t o n a f t e r a s u c c e s s f u l s a v e
hasChanges = false
goBack ( )
}
2022-06-21 10:02:05 -07:00
}
}
2022-06-12 01:25:42 -07:00
}
2022-12-30 11:08:59 -08:00
message : {
Text ( " config.save.confirm " )
}
2022-12-13 07:49:46 -08:00
. navigationTitle ( " display.config " )
2022-06-12 01:25:42 -07:00
. navigationBarItems ( trailing :
ZStack {
2022-07-01 19:44:25 -07:00
ConnectedDevice ( bluetoothOn : bleManager . isSwitchedOn , deviceConnected : bleManager . connectedPeripheral != nil , name : ( bleManager . connectedPeripheral != nil ) ? bleManager . connectedPeripheral . shortName : " ???? " )
2022-06-12 01:25:42 -07:00
} )
. onAppear {
2022-10-18 13:53:50 -07:00
self . bleManager . context = context
2023-03-19 18:37:23 -07:00
setDisplayValues ( )
2023-03-06 10:33:18 -08:00
2023-01-31 22:08:03 -08:00
// N e e d t o r e q u e s t a L o R a C o n f i g f r o m t h e r e m o t e n o d e b e f o r e a l l o w i n g c h a n g e s
if bleManager . connectedPeripheral != nil && node ? . displayConfig = = nil {
print ( " empty display config " )
2023-03-14 19:16:04 -07:00
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral ? . num ? ? 0 , context : context )
2023-03-05 14:40:07 -08:00
if node != nil && connectedNode != nil {
2023-02-06 18:45:03 -08:00
_ = bleManager . requestDisplayConfig ( fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
2023-01-31 22:08:03 -08:00
}
}
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 }
}
}
2023-03-25 00:41:57 -07:00
. onChange ( of : wakeOnTapOrMotion ) { newWakeOnTapOrMotion in
if node != nil && node ! . displayConfig != nil {
if newWakeOnTapOrMotion != node ! . displayConfig ! . wakeOnTapOrMotion { 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-06-12 01:25:42 -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 }
}
}
2022-12-11 14:14:08 -08:00
. onChange ( of : oledType ) { newOledType in
if node != nil && node ! . displayConfig != nil {
if newOledType != node ! . displayConfig ! . oledType { hasChanges = true }
}
}
2023-01-25 23:01:45 -08:00
. onChange ( of : displayMode ) { newDisplayMode in
if node != nil && node ! . displayConfig != nil {
if newDisplayMode != node ! . displayConfig ! . displayMode { hasChanges = true }
}
}
2022-06-12 01:25:42 -07:00
}
2023-03-19 18:37:23 -07:00
func setDisplayValues ( ) {
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
2023-03-25 00:41:57 -07:00
self . wakeOnTapOrMotion = node ? . displayConfig ? . wakeOnTapOrMotion ? ? false
2023-03-19 18:37:23 -07:00
self . flipScreen = node ? . displayConfig ? . flipScreen ? ? false
self . oledType = Int ( node ? . displayConfig ? . oledType ? ? 0 )
self . displayMode = Int ( node ? . displayConfig ? . displayMode ? ? 0 )
self . hasChanges = false
}
2022-06-12 01:25:42 -07:00
}