2022-06-27 08:30:49 -07:00
//
// U s e r . 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 / 2 7 / 2 2 .
//
import SwiftUI
struct UserConfig : View {
@ Environment ( \ . managedObjectContext ) var context
@ EnvironmentObject var bleManager : BLEManager
2022-07-07 00:29:52 -07:00
var node : NodeInfoEntity ?
2022-06-27 08:30:49 -07:00
@ State private var isPresentingFactoryResetConfirm : Bool = false
@ State private var isPresentingSaveConfirm : Bool = false
@ State var initialLoad : Bool = true
@ State var hasChanges = false
@ State var shortName = " "
@ State var longName = " "
var body : some View {
VStack {
Form {
Section ( header : Text ( " USER DETAILS " ) ) {
HStack {
Label ( " Long Name " , systemImage : " person.crop.rectangle.fill " )
TextField ( " Long Name " , text : $ longName )
. onChange ( of : longName , perform : { value in
let totalBytes = longName . utf8 . count
// O n l y m e s s w i t h t h e v a l u e i f i t i s t o o b i g
2022-07-01 10:57:54 -07:00
if totalBytes > 36 {
2022-06-27 08:30:49 -07:00
2022-07-01 10:57:54 -07:00
let firstNBytes = Data ( longName . utf8 . prefix ( 36 ) )
2022-06-27 08:30:49 -07:00
if let maxBytesString = String ( data : firstNBytes , encoding : String . Encoding . utf8 ) {
// S e t t h e l o n g N a m e b a c k t o t h e l a s t p l a c e w h e r e i t w a s t h e r i g h t s i z e
longName = maxBytesString
}
}
} )
2022-08-02 09:44:59 -07:00
}
. keyboardType ( . default )
. disableAutocorrection ( true )
Text ( " Long name can be up to 36 bytes long. " )
. font ( . caption )
HStack {
Label ( " Short Name " , systemImage : " circlebadge.fill " )
TextField ( " Long Name " , text : $ shortName )
. foregroundColor ( . gray )
2022-06-27 08:30:49 -07:00
. onChange ( of : shortName , perform : { value in
let totalBytes = shortName . utf8 . count
// O n l y m e s s w i t h t h e v a l u e i f i t i s t o o b i g
2022-07-01 10:57:54 -07:00
if totalBytes > 4 {
2022-06-27 08:30:49 -07:00
2022-07-01 10:57:54 -07:00
let firstNBytes = Data ( shortName . utf8 . prefix ( 4 ) )
2022-06-27 08:30:49 -07:00
if let maxBytesString = String ( data : firstNBytes , encoding : String . Encoding . utf8 ) {
// S e t t h e s h o r t N a m e b a c k t o t h e l a s t p l a c e w h e r e i t w a s t h e r i g h t s i z e
shortName = maxBytesString
}
}
} )
. foregroundColor ( . gray )
}
2022-06-28 06:56:50 -07:00
. keyboardType ( . asciiCapable )
2022-06-27 08:30:49 -07:00
. disableAutocorrection ( true )
2022-07-01 18:30:40 -07:00
Text ( " The short name is used in maps and messaging and will be appended to the last 4 of the device MAC address to set the device's BLE Name. It can be up to 4 bytes long. " )
2022-06-28 06:56:50 -07:00
. font ( . caption )
2022-06-27 08:30:49 -07:00
}
}
. disabled ( bleManager . connectedPeripheral = = nil )
HStack {
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
) {
Button ( " Save User Config to \( bleManager . connectedPeripheral != nil ? bleManager . connectedPeripheral . longName : " Unknown " ) ? " ) {
var u = User ( )
u . shortName = shortName
u . longName = longName
2022-08-12 08:58:10 -07:00
let adminMessageId = bleManager . saveUser ( config : u , fromUser : node ! . user ! , toUser : node ! . user ! )
2022-07-02 10:06:50 -07:00
if adminMessageId > 0 {
2022-06-27 08:30:49 -07:00
hasChanges = false
}
}
}
}
Spacer ( )
}
. navigationTitle ( " User Config " )
. 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-27 08:30:49 -07:00
} )
. onAppear {
if self . initialLoad {
self . bleManager . context = context
2022-07-07 00:29:52 -07:00
self . shortName = node ? . user ! . shortName ? ? " "
self . longName = node ? . user ! . longName ? ? " "
2022-06-27 08:30:49 -07:00
self . hasChanges = false
self . initialLoad = false
}
}
. onChange ( of : shortName ) { newShort in
2022-07-11 15:43:25 -07:00
if node != nil && node ! . user != nil {
if newShort != node ? . user ! . shortName { hasChanges = true }
2022-06-27 08:30:49 -07:00
}
}
. onChange ( of : longName ) { newLong in
2022-07-11 15:43:25 -07:00
if node != nil && node ! . user != nil {
if newLong != node ? . user ! . longName { hasChanges = true }
2022-06-27 08:30:49 -07:00
}
}
. navigationViewStyle ( StackNavigationViewStyle ( ) )
}
}