2023-11-28 20:03:08 -08:00
//
// A m b i e n t L i g h t i n g C o n f i g . s w i f t
// M e s h t a s t i c
//
// 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 1 1 / 2 6 / 2 3
//
import SwiftUI
@ available ( iOS 17.0 , macOS 14.0 , * )
struct AmbientLightingConfig : View {
@ Environment ( \ . self ) var environment
@ Environment ( \ . managedObjectContext ) var context
@ EnvironmentObject var bleManager : BLEManager
@ Environment ( \ . dismiss ) private var goBack
var node : NodeInfoEntity ?
@ State private var isPresentingSaveConfirm : Bool = false
@ State var hasChanges = false
@ State var ledState : Bool = false
@ State var current = 10
@ State var red = 0
@ State var green = 0
@ State var blue = 0
@ State private var color = Color ( red : 51 , green : 199 , blue : 88 ) // C o l o r ( . s R G B , r e d : 0 . 9 8 , g r e e n : 0 . 9 , b l u e : 0 . 2 )
@ State private var components : Color . Resolved ?
var body : some View {
VStack {
Form {
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 )
} 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
if node ? . rtttlConfig = = nil {
Text ( " Ambient Lighting 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 )
. onAppear {
setAmbientLightingConfigValue ( )
}
}
} else if node != nil && node ? . num ? ? 0 = = bleManager . connectedPeripheral ? . num ? ? 0 {
Text ( " Configuration for: \( node ? . user ? . longName ? ? " Unknown " ) " )
. font ( . title3 )
} else {
Text ( " Please connect to a radio to configure settings. " )
. font ( . callout )
. foregroundColor ( . orange )
}
Section ( header : Text ( " options " ) ) {
2023-12-01 16:33:17 -08:00
Toggle ( isOn : $ ledState ) {
Label ( " LED State " , systemImage : ledState ? " lightbulb.led.fill " : " lightbulb.led " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
. listRowSeparator ( . hidden )
Text ( " The state of the LED (on/off) " )
. font ( . caption )
. foregroundStyle ( . gray )
HStack {
Image ( systemName : " eyedropper " )
. foregroundColor ( . accentColor )
ColorPicker ( " Color " , selection : $ color , supportsOpacity : false )
. padding ( 5 )
}
HStack {
Image ( systemName : " directcurrent " )
. foregroundColor ( . accentColor )
Stepper ( " Current: \( current ) " , value : $ current , in : 0. . . 31 , step : 1 )
. padding ( 5 )
2023-11-28 20:03:08 -08:00
}
. onChange ( of : color , initial : true ) {
components = color . resolve ( in : environment )
hasChanges = true
}
}
}
2023-12-01 16:33:17 -08:00
. disabled ( self . bleManager . connectedPeripheral = = nil || node ? . ambientLightingConfig = = nil )
2023-11-28 20:03:08 -08:00
Button {
isPresentingSaveConfirm = true
} label : {
Label ( " save " , systemImage : " square.and.arrow.down " )
}
. disabled ( self . bleManager . connectedPeripheral = = nil || ! hasChanges )
. buttonStyle ( . bordered )
. buttonBorderShape ( . capsule )
. controlSize ( . large )
. padding ( )
. confirmationDialog (
" are.you.sure " ,
isPresented : $ isPresentingSaveConfirm ,
titleVisibility : . visible
) {
let nodeName = node ? . user ? . longName ? ? " unknown " . localized
let buttonText = String . localizedStringWithFormat ( " save.config %@ " . localized , nodeName )
Button ( buttonText ) {
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral . num , context : context )
if connectedNode != nil {
var al = ModuleConfig . AmbientLightingConfig ( )
al . ledState = ledState
al . current = UInt32 ( current )
if let components {
al . red = UInt32 ( components . red * 255 )
al . green = UInt32 ( components . green * 255 )
al . blue = UInt32 ( components . blue * 255 )
}
let adminMessageId = bleManager . saveAmbientLightingModuleConfig ( config : al , fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
if adminMessageId > 0 {
// 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 ( )
}
}
}
}
message : {
Text ( " config.save.confirm " )
}
. navigationTitle ( " ambient.lighting.config " )
. navigationBarItems ( trailing :
ZStack {
ConnectedDevice ( bluetoothOn : bleManager . isSwitchedOn , deviceConnected : bleManager . connectedPeripheral != nil , name : ( bleManager . connectedPeripheral != nil ) ? bleManager . connectedPeripheral . shortName : " ? " )
} )
. onAppear {
self . bleManager . context = context
setAmbientLightingConfigValue ( )
// N e e d t o r e q u e s t a A m b i e n t L i g h t i n g 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 ? . ambientLightingConfig = = nil {
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral . num , context : context )
if node != nil && connectedNode != nil {
_ = bleManager . requestAmbientLightingConfig ( fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
}
}
}
. onChange ( of : ledState ) { newLedState in
if node != nil && node ! . ambientLightingConfig != nil {
if newLedState != node ! . ambientLightingConfig ! . ledState { hasChanges = true }
}
}
}
}
func setAmbientLightingConfigValue ( ) {
self . ledState = node ? . ambientLightingConfig ? . ledState ? ? false
self . current = Int ( node ? . ambientLightingConfig ? . current ? ? 10 )
2023-11-28 21:18:59 -08:00
let red = Double ( node ? . ambientLightingConfig ? . red ? ? 255 )
let green = Double ( node ? . ambientLightingConfig ? . green ? ? 255 )
let blue = Double ( node ? . ambientLightingConfig ? . blue ? ? 255 )
color = Color ( red : red / 255.0 , green : green / 255.0 , blue : blue / 255.0 )
2023-11-28 20:03:08 -08:00
self . hasChanges = false
}
}