2023-08-26 20:45:15 -07:00
//
// S t o r e F o r w a r d . 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 8 / 2 6 / 2 3 .
//
import SwiftUI
struct StoreForwardConfig : View {
@ 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 : Bool = false
// / E n a b l e t h e S t o r e a n d F o r w a r d M o d u l e
@ State var enabled = false
// / S e n d a H e a r t b e a t
@ State var heartbeat : Bool = false
// / N u m b e r o f R e c o r d s
@ State var records = 0
// / M a x n u m b e r o f h i s t o r y i t e m s t o r e t u r n
@ State var historyReturnMax = 0
// / T i m e w i n d o w f o r h i s t o r y
@ State var historyReturnWindow = 0
var body : some View {
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
2023-09-30 12:42:49 -07:00
if node ? . storeForwardConfig = = nil {
Text ( " Store and forward 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. " )
2023-08-26 20:45:15 -07:00
. font ( . callout )
. foregroundColor ( . orange )
} else {
Text ( " Remote administration for: \( node ? . user ? . longName ? ? " Unknown " ) " )
. font ( . title3 )
. onAppear {
setDetectionSensorValues ( )
}
}
} 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 " ) ) {
Toggle ( isOn : $ enabled ) {
Label ( " enabled " , systemImage : " envelope.arrow.triangle.branch " )
}
Toggle ( isOn : $ heartbeat ) {
Label ( " storeforward.heartbeat " , systemImage : " waveform.path.ecg " )
}
Picker ( " Number of records " , selection : $ records ) {
Text ( " unset " ) . tag ( 0 )
Text ( " 25 " ) . tag ( 25 )
Text ( " 50 " ) . tag ( 50 )
Text ( " 75 " ) . tag ( 75 )
Text ( " 100 " ) . tag ( 100 )
}
. pickerStyle ( DefaultPickerStyle ( ) )
Picker ( " History Return Max " , selection : $ historyReturnMax ) {
Text ( " unset " ) . tag ( 0 )
Text ( " 25 " ) . tag ( 25 )
Text ( " 50 " ) . tag ( 50 )
Text ( " 75 " ) . tag ( 75 )
Text ( " 100 " ) . tag ( 100 )
}
. pickerStyle ( DefaultPickerStyle ( ) )
Picker ( " History Return Window " , selection : $ historyReturnWindow ) {
Text ( " unset " ) . tag ( 0 )
2023-08-27 18:29:27 -07:00
Text ( " One Minute " ) . tag ( 60 )
Text ( " Five Minutes " ) . tag ( 300 )
Text ( " Ten Minutes " ) . tag ( 600 )
Text ( " Fifteen Minutes " ) . tag ( 900 )
Text ( " Thirty Minutes " ) . tag ( 1800 )
Text ( " One Hour " ) . tag ( 3600 )
2023-08-26 20:45:15 -07:00
}
. pickerStyle ( DefaultPickerStyle ( ) )
}
}
. scrollDismissesKeyboard ( . interactively )
2023-10-03 08:41:44 -07:00
. disabled ( self . bleManager . connectedPeripheral = = nil || node ? . storeForwardConfig = = nil )
2023-08-26 20:45:15 -07:00
Button {
isPresentingSaveConfirm = true
} label : {
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 ,
titleVisibility : . visible
) {
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral ? . num ? ? - 1 , context : context )
if connectedNode != nil {
let nodeName = node ? . user ? . longName ? ? " unknown " . localized
let buttonText = String . localizedStringWithFormat ( " save.config %@ " . localized , nodeName )
Button ( buttonText ) {
var sfc = ModuleConfig . StoreForwardConfig ( )
sfc . enabled = self . enabled
sfc . heartbeat = self . heartbeat
sfc . records = UInt32 ( self . records )
sfc . historyReturnMax = UInt32 ( self . historyReturnMax )
sfc . historyReturnWindow = UInt32 ( self . historyReturnWindow )
let adminMessageId = bleManager . saveStoreForwardModuleConfig ( config : sfc , 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 ( " storeforward.config " )
. navigationBarItems ( trailing :
ZStack {
2023-09-02 17:37:35 -07:00
ConnectedDevice ( bluetoothOn : bleManager . isSwitchedOn , deviceConnected : bleManager . connectedPeripheral != nil , name : ( bleManager . connectedPeripheral != nil ) ? bleManager . connectedPeripheral . shortName : " ? " )
2023-08-26 20:45:15 -07:00
} )
. onAppear {
self . bleManager . context = context
setDetectionSensorValues ( )
// N e e d t o r e q u e s t a D e t e c t i o n S e n s o r M o d u l e 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 ? . detectionSensorConfig = = nil {
print ( " empty store and forward module config " )
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral . num , context : context )
if node != nil && connectedNode != nil {
_ = bleManager . requestDetectionSensorModuleConfig ( fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
}
}
}
. onChange ( of : enabled ) { newEnabled in
if node != nil && node ? . detectionSensorConfig != nil {
if newEnabled != node ! . detectionSensorConfig ! . enabled { hasChanges = true }
}
}
. onChange ( of : heartbeat ) { newHeartbeat in
if node != nil && node ? . storeForwardConfig != nil {
if newHeartbeat != node ! . storeForwardConfig ! . heartbeat { hasChanges = true }
}
}
. onChange ( of : records ) { newRecords in
if node != nil && node ? . storeForwardConfig != nil {
if newRecords != node ! . storeForwardConfig ! . records { hasChanges = true }
}
}
. onChange ( of : historyReturnMax ) { newHistoryReturnMax in
if node != nil && node ? . storeForwardConfig != nil {
if newHistoryReturnMax != node ! . storeForwardConfig ! . historyReturnMax { hasChanges = true }
}
}
. onChange ( of : historyReturnWindow ) { newHistoryReturnWindow in
if node != nil && node ? . storeForwardConfig != nil {
if newHistoryReturnWindow != node ! . storeForwardConfig ! . historyReturnWindow { hasChanges = true }
}
}
}
func setDetectionSensorValues ( ) {
self . enabled = ( node ? . storeForwardConfig ? . enabled ? ? false )
self . heartbeat = ( node ? . storeForwardConfig ? . heartbeat ? ? true )
self . records = Int ( node ? . storeForwardConfig ? . records ? ? 50 )
self . historyReturnMax = Int ( node ? . storeForwardConfig ? . historyReturnMax ? ? 100 )
2023-08-27 18:29:27 -07:00
self . historyReturnWindow = Int ( node ? . storeForwardConfig ? . historyReturnWindow ? ? 300 )
2023-08-26 20:45:15 -07:00
self . hasChanges = false
}
}