2022-07-14 07:14:43 -07:00
//
// S a v e C h a n n e l Q R C o d e . 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 7 / 1 3 / 2 2 .
//
import SwiftUI
struct SaveChannelQRCode : View {
2023-03-06 10:33:18 -08:00
2022-11-08 23:18:50 -08:00
@ Environment ( \ . dismiss ) private var dismiss
2023-03-06 10:33:18 -08:00
2022-10-18 20:25:11 -07:00
var channelSetLink : String
2024-04-03 00:04:46 -07:00
var addChannels : Bool = false
2022-10-18 19:50:42 -07:00
var bleManager : BLEManager
2024-05-05 08:38:27 -07:00
@ State var showError : Bool = false
2022-10-18 19:50:42 -07:00
@ State var connectedToDevice = false
2022-07-14 07:14:43 -07:00
var body : some View {
VStack {
2024-04-03 00:04:46 -07:00
Text ( " \( addChannels ? " Add " : " Replace all " ) Channels? " )
2022-10-12 22:15:15 -07:00
. font ( . title )
2024-07-07 09:40:06 -07:00
Text ( " These settings will \( addChannels ? " add " : " replace all " ) channels. The current LoRa Config will be replaced, if there are substantial changes to the LoRa config the device will reboot " )
2024-05-05 08:38:27 -07:00
. fixedSize ( horizontal : false , vertical : true )
2022-10-12 22:15:15 -07:00
. foregroundColor ( . gray )
2024-04-03 00:04:46 -07:00
. font ( . title3 )
2022-10-12 22:15:15 -07:00
. padding ( )
2023-03-06 10:33:18 -08:00
2024-05-05 08:38:27 -07:00
if showError {
Text ( " Channels being added from the QR code did not save. When adding channels the names must be unique. " )
. fixedSize ( horizontal : false , vertical : true )
. foregroundColor ( . red )
. font ( . callout )
. padding ( )
}
2022-11-08 22:01:32 -08:00
HStack {
2024-05-05 08:38:27 -07:00
if ! showError {
Button {
let success = bleManager . saveChannelSet ( base64UrlString : channelSetLink , addChannels : addChannels )
if success {
dismiss ( )
} else {
showError = true
}
} label : {
Label ( " save " , systemImage : " square.and.arrow.down " )
2022-10-19 16:58:49 -07:00
}
2024-05-05 08:38:27 -07:00
. buttonStyle ( . bordered )
. buttonBorderShape ( . capsule )
. controlSize ( . large )
. padding ( )
. disabled ( ! connectedToDevice )
} else {
Button {
dismiss ( )
} label : {
Label ( " cancel " , systemImage : " xmark " )
2023-03-06 10:33:18 -08:00
2024-05-05 08:38:27 -07:00
}
. buttonStyle ( . bordered )
. buttonBorderShape ( . capsule )
. controlSize ( . large )
. padding ( )
2022-10-10 21:21:58 -07:00
}
2024-05-05 08:38:27 -07:00
2022-11-08 22:01:32 -08:00
#if targetEnvironment ( macCatalyst )
Button {
2022-11-08 23:18:50 -08:00
dismiss ( )
2022-11-08 22:01:32 -08:00
} label : {
2022-12-12 20:35:38 -08:00
Label ( " cancel " , systemImage : " xmark " )
2023-03-06 10:33:18 -08:00
2022-11-08 22:01:32 -08:00
}
. buttonStyle ( . bordered )
. buttonBorderShape ( . capsule )
. controlSize ( . large )
. padding ( )
#endif
}
2022-10-18 19:50:42 -07:00
}
. onAppear {
connectedToDevice = bleManager . connectToPreferredPeripheral ( )
2022-10-10 21:21:58 -07:00
}
2022-07-14 07:14:43 -07:00
}
}