From 4fec2be045c8f72f7405e958873cc2dcafa451e4 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 12 Nov 2023 00:28:59 -0800 Subject: [PATCH] Finish mocking up the waypoint form --- .../Nodes/Helpers/Map/WaypointForm.swift | 104 +++++++++++++++++- Meshtastic/Views/Nodes/MeshMap.swift | 8 +- 2 files changed, 104 insertions(+), 8 deletions(-) diff --git a/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift b/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift index 3ab7fb0a..15206d5a 100644 --- a/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift +++ b/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift @@ -14,9 +14,6 @@ struct WaypointForm: View { @EnvironmentObject var bleManager: BLEManager @Environment(\.dismiss) private var dismiss @State var waypoint: WaypointEntity - - - @FocusState private var iconIsFocused: Bool @State private var name: String = "" @State private var description: String = "" @@ -30,10 +27,107 @@ struct WaypointForm: View { var body: some View { - ZStack { + VStack { + + Text((waypoint.id > 0) ? "Editing Waypoint" : "Create Waypoint") + .font(.largeTitle) + Divider() Form { + + let distance = CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude).distance(from: CLLocation(latitude: waypoint.coordinate.latitude , longitude: waypoint.coordinate.longitude )) + Section(header: Text("Coordinate") ) { + HStack { + Text("Location: \(String(format: "%.5f", waypoint.coordinate.latitude) + "," + String(format: "%.5f", waypoint.coordinate.longitude))") + .textSelection(.enabled) + .foregroundColor(Color.gray) + } + HStack { + if waypoint.coordinate.latitude != 0 && waypoint.coordinate.longitude != 0 { + DistanceText(meters: distance) + .foregroundColor(Color.gray) + } + } + } Section(header: Text("Waypoint Options")) { - + HStack { + Text("Name") + Spacer() + TextField( + "Name", + text: $name, + axis: .vertical + ) + .foregroundColor(Color.gray) + .onChange(of: name, perform: { _ in + let totalBytes = name.utf8.count + // Only mess with the value if it is too big + if totalBytes > 30 { + let firstNBytes = Data(name.utf8.prefix(30)) + if let maxBytesString = String(data: firstNBytes, encoding: String.Encoding.utf8) { + // Set the name back to the last place where it was the right size + name = maxBytesString + } + } + }) + } + HStack { + Text("Description") + Spacer() + TextField( + "Description", + text: $description, + axis: .vertical + ) + .foregroundColor(Color.gray) + .onChange(of: description, perform: { _ in + let totalBytes = description.utf8.count + // Only mess with the value if it is too big + if totalBytes > 100 { + let firstNBytes = Data(description.utf8.prefix(100)) + if let maxBytesString = String(data: firstNBytes, encoding: String.Encoding.utf8) { + // Set the name back to the last place where it was the right size + description = maxBytesString + } + } + }) + } + HStack { + Text("Icon") + Spacer() + EmojiOnlyTextField(text: $icon, placeholder: "Select an emoji") + .font(.title) + .focused($iconIsFocused) + .onChange(of: icon) { value in + + // If you have anything other than emojis in your string make it empty + if !value.onlyEmojis() { + icon = "" + } + // If a second emoji is entered delete the first one + if value.count >= 1 { + + if value.count > 1 { + let index = value.index(value.startIndex, offsetBy: 1) + icon = String(value[index]) + } + iconIsFocused = false + } + } + + } + Toggle(isOn: $expires) { + Label("Expires", systemImage: "clock.badge.xmark") + } + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + if expires { + DatePicker("Expire", selection: $expire, in: Date.now...) + .datePickerStyle(.compact) + .font(.callout) + } + Toggle(isOn: $locked) { + Label("Locked", systemImage: "lock") + } + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) } } #if targetEnvironment(macCatalyst) diff --git a/Meshtastic/Views/Nodes/MeshMap.swift b/Meshtastic/Views/Nodes/MeshMap.swift index 99ffa4f5..cff653b5 100644 --- a/Meshtastic/Views/Nodes/MeshMap.swift +++ b/Meshtastic/Views/Nodes/MeshMap.swift @@ -162,9 +162,11 @@ struct MeshMap: View { /// Create a new WaypointEntity using the values from the newWaypoint which will trigger the WaypointForm sheet editingWaypoint = WaypointEntity(context: context) editingWaypoint!.name = "Waypoint Pin" - editingWaypoint!.latitudeI = Int32(newWaypoint?.latitude ?? 0 * 1e7) - editingWaypoint!.longitudeI = Int32(newWaypoint?.longitude ?? 0 * 1e7) - default: + + + editingWaypoint!.latitudeI = Int32((newWaypoint?.latitude ?? 0) * 1e7) + editingWaypoint!.longitudeI = Int32((newWaypoint!.longitude ?? 0) * 1e7) + editingWaypoint!.id = Int64(UInt32.random(in: UInt32(UInt8.max)..