From 12ee105106adfc0eba38bdaf8291a4efc2bb6b37 Mon Sep 17 00:00:00 2001 From: Ed Haber Date: Fri, 19 Jan 2024 14:13:51 -0500 Subject: [PATCH] Allow users to enter an existing key when adding a channel Allow the key field to be edited by a user and validate that the user supplied key is valid base64 encoded and the correct key length. --- Meshtastic/Views/Settings/Channels.swift | 33 +++++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/Meshtastic/Views/Settings/Channels.swift b/Meshtastic/Views/Settings/Channels.swift index 39d69d8e..c736d35a 100644 --- a/Meshtastic/Views/Settings/Channels.swift +++ b/Meshtastic/Views/Settings/Channels.swift @@ -25,6 +25,7 @@ struct Channels: View { var node: NodeInfoEntity? @State var hasChanges = false + @State var hasValidKey = false @State private var isPresentingEditView = false @State private var isPresentingSaveConfirm: Bool = false @State private var channelIndex: Int32 = 0 @@ -167,9 +168,33 @@ struct Channels: View { HStack(alignment: .top) { Text("Key") Spacer() - Text(channelKey) - .foregroundColor(Color.gray) - .textSelection(.enabled) + TextField( + "Key", + text: $channelKey + ) + .disableAutocorrection(true) + .keyboardType(.alphabet) + .foregroundColor(Color.gray) + .textSelection(.enabled) + .background( + RoundedRectangle(cornerRadius: 25.0) + .stroke( + hasValidKey ? + Color.green : + Color.red + , lineWidth: 2.0) + ) + .onChange(of: channelKey, perform: { _ in + let tempKey = Data(base64Encoded: channelKey) ?? Data() + if tempKey.count == channelKeySize || channelKeySize == -1{ + hasValidKey = true + } + else { + hasValidKey = false + } + hasChanges = true + }) + .disabled(channelKeySize <= 0) // TextField( // "", // text: $channelKey, @@ -256,7 +281,7 @@ struct Channels: View { } label: { Label("save", systemImage: "square.and.arrow.down") } - .disabled(bleManager.connectedPeripheral == nil || !hasChanges) + .disabled(bleManager.connectedPeripheral == nil || !hasChanges || !hasValidKey) .buttonStyle(.bordered) .buttonBorderShape(.capsule) .controlSize(.large)