Meshtastic-Apple/Meshtastic/Views/MapKitMap/WaypointFormMapKit.swift

260 lines
7.7 KiB
Swift
Raw Normal View History

//
// WaypointFormView.swift
// Meshtastic
//
// Copyright Garth Vander Houwen 1/10/23.
//
import SwiftUI
import CoreLocation
2023-11-09 17:43:38 -08:00
struct WaypointFormMapKit: View {
2023-03-06 10:33:18 -08:00
2023-01-14 09:42:09 -08:00
@EnvironmentObject var bleManager: BLEManager
@Environment(\.dismiss) private var dismiss
@State var coordinate: WaypointCoordinate
2023-01-14 09:42:09 -08:00
@FocusState private var iconIsFocused: Bool
@State private var name: String = ""
@State private var description: String = ""
2023-01-15 08:49:17 -08:00
@State private var icon: String = "📍"
2023-01-18 08:45:47 -08:00
@State private var latitude: Double = 0
@State private var longitude: Double = 0
@State private var expires: Bool = false
@State private var expire: Date = Date.now.addingTimeInterval(60 * 480) // 1 minute * 480 = 8 Hours
@State private var locked: Bool = false
2023-01-26 16:19:51 -08:00
@State private var lockedTo: Int64 = 0
2023-03-06 10:33:18 -08:00
var body: some View {
2023-03-06 10:33:18 -08:00
Form {
let distance = CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude).distance(from: CLLocation(latitude: coordinate.coordinate?.latitude ?? 0, longitude: coordinate.coordinate?.longitude ?? 0))
Section(header: Text((coordinate.waypointId > 0) ? "Editing Waypoint" : "Create Waypoint")) {
HStack {
2023-01-18 08:45:47 -08:00
Text("Location: \(String(format: "%.5f", latitude) + "," + String(format: "%.5f", longitude))")
2023-01-13 22:30:10 -08:00
.textSelection(.enabled)
.foregroundColor(Color.gray)
.font(.caption2)
if coordinate.coordinate?.latitude ?? 0 != 0 && coordinate.coordinate?.longitude ?? 0 != 0 {
DistanceText(meters: distance)
.foregroundColor(Color.gray)
.font(.caption2)
}
}
HStack {
Text("Name")
Spacer()
TextField(
"Name",
text: $name,
axis: .vertical
)
.foregroundColor(Color.gray)
2023-03-06 10:33:18 -08:00
.onChange(of: name, perform: { _ in
let totalBytes = name.utf8.count
// Only mess with the value if it is too big
if totalBytes > 30 {
name = String(name.dropLast())
}
})
}
HStack {
Text("Description")
Spacer()
TextField(
"Description",
text: $description,
axis: .vertical
)
.foregroundColor(Color.gray)
2023-03-06 10:33:18 -08:00
.onChange(of: description, perform: { _ in
let totalBytes = description.utf8.count
// Only mess with the value if it is too big
if totalBytes > 100 {
description = String(description.dropLast())
}
})
}
HStack {
Text("Icon")
Spacer()
2023-01-14 09:42:09 -08:00
EmojiOnlyTextField(text: $icon, placeholder: "Select an emoji")
.font(.title)
2023-01-14 09:42:09 -08:00
.focused($iconIsFocused)
.onChange(of: icon) { value in
2023-03-06 10:33:18 -08:00
// If you have anything other than emojis in your string make it empty
if !value.onlyEmojis() {
2023-01-14 09:42:09 -08:00
icon = ""
}
// If a second emoji is entered delete the first one
if value.count >= 1 {
2023-03-06 10:33:18 -08:00
if value.count > 1 {
let index = value.index(value.startIndex, offsetBy: 1)
2023-01-14 09:42:09 -08:00
icon = String(value[index])
}
2023-01-14 09:42:09 -08:00
iconIsFocused = false
}
}
2023-03-06 10:33:18 -08:00
}
2023-02-03 19:20:23 -08:00
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))
}
}
HStack {
Button {
2023-03-06 10:33:18 -08:00
2023-01-14 09:42:09 -08:00
var newWaypoint = Waypoint()
// Loading a waypoint from edit
if coordinate.waypointId > 0 {
newWaypoint.id = UInt32(coordinate.waypointId)
let waypoint = getWaypoint(id: Int64(coordinate.waypointId), context: bleManager.context!)
newWaypoint.latitudeI = waypoint.latitudeI
newWaypoint.longitudeI = waypoint.longitudeI
2023-01-16 17:40:28 -08:00
} else {
// New waypoint
2023-01-18 08:45:47 -08:00
newWaypoint.id = UInt32.random(in: UInt32(UInt8.max)..<UInt32.max)
newWaypoint.latitudeI = Int32(Double(coordinate.coordinate?.latitude ?? 0) * 1e7)
newWaypoint.longitudeI = Int32(Double(coordinate.coordinate?.longitude ?? 0) * 1e7)
2023-01-16 17:40:28 -08:00
}
2023-01-20 21:49:54 -08:00
newWaypoint.name = name.count > 0 ? name : "Dropped Pin"
2023-01-14 09:42:09 -08:00
newWaypoint.description_p = description
2023-01-15 08:49:17 -08:00
// Unicode scalar value for the icon emoji string
let unicodeScalers = icon.unicodeScalars
// First element as an UInt32
let unicode = unicodeScalers[unicodeScalers.startIndex].value
2023-01-14 11:26:32 -08:00
newWaypoint.icon = unicode
2023-01-15 19:15:00 -08:00
if locked {
2023-01-26 16:19:51 -08:00
if lockedTo == 0 {
newWaypoint.lockedTo = UInt32(bleManager.connectedPeripheral!.num)
} else {
newWaypoint.lockedTo = UInt32(lockedTo)
}
2023-01-15 19:15:00 -08:00
}
if expires {
2023-01-15 08:49:17 -08:00
newWaypoint.expire = UInt32(expire.timeIntervalSince1970)
} else {
newWaypoint.expire = 0
2023-01-15 08:49:17 -08:00
}
if bleManager.sendWaypoint(waypoint: newWaypoint) {
dismiss()
} else {
2023-01-21 07:28:50 -08:00
dismiss()
logger.error("Send waypoint failed")
2023-01-15 08:49:17 -08:00
}
} label: {
2023-01-14 11:26:32 -08:00
Label("Send", systemImage: "arrow.up")
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
2023-05-13 14:23:12 -07:00
.controlSize(.regular)
2023-01-15 08:49:17 -08:00
.disabled(bleManager.connectedPeripheral == nil)
2023-01-20 21:49:54 -08:00
.padding(.bottom)
2023-03-06 10:33:18 -08:00
Button(role: .cancel) {
dismiss()
} label: {
2023-01-20 21:49:54 -08:00
Label("cancel", systemImage: "x.circle")
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
2023-05-13 14:23:12 -07:00
.controlSize(.regular)
2023-01-20 21:49:54 -08:00
.padding(.bottom)
if coordinate.waypointId > 0 {
2023-03-06 10:33:18 -08:00
2023-02-03 19:20:23 -08:00
Menu {
Button("For me", action: {
let waypoint = getWaypoint(id: Int64(coordinate.waypointId), context: bleManager.context!)
2023-02-03 19:20:23 -08:00
bleManager.context!.delete(waypoint)
do {
try bleManager.context!.save()
} catch {
bleManager.context!.rollback()
}
dismiss() })
Button("For everyone", action: {
var newWaypoint = Waypoint()
2023-03-06 10:33:18 -08:00
if coordinate.waypointId > 0 {
newWaypoint.id = UInt32(coordinate.waypointId)
2023-03-06 10:33:18 -08:00
}
2023-02-03 19:20:23 -08:00
newWaypoint.name = name.count > 0 ? name : "Dropped Pin"
newWaypoint.description_p = description
newWaypoint.latitudeI = Int32(coordinate.coordinate?.latitude ?? 0 * 1e7)
newWaypoint.longitudeI = Int32(coordinate.coordinate?.longitude ?? 0 * 1e7)
2023-02-03 19:20:23 -08:00
// Unicode scalar value for the icon emoji string
let unicodeScalers = icon.unicodeScalars
// First element as an UInt32
let unicode = unicodeScalers[unicodeScalers.startIndex].value
newWaypoint.icon = unicode
if locked {
if lockedTo == 0 {
newWaypoint.lockedTo = UInt32(bleManager.connectedPeripheral!.num)
} else {
newWaypoint.lockedTo = UInt32(lockedTo)
}
}
newWaypoint.expire = 1
if bleManager.sendWaypoint(waypoint: newWaypoint) {
dismiss()
} else {
dismiss()
logger.error("Send waypoint failed")
2023-02-03 19:20:23 -08:00
}
})
}
label: {
2023-01-20 21:49:54 -08:00
Label("delete", systemImage: "trash")
2023-02-03 19:20:23 -08:00
.foregroundColor(.red)
2023-01-20 21:49:54 -08:00
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
2023-05-13 14:23:12 -07:00
.controlSize(.regular)
2023-01-20 21:49:54 -08:00
.padding(.bottom)
}
}
2023-01-16 17:40:28 -08:00
.onAppear {
if coordinate.waypointId > 0 {
let waypoint = getWaypoint(id: Int64(coordinate.waypointId), context: bleManager.context!)
2023-01-16 17:40:28 -08:00
name = waypoint.name ?? "Dropped Pin"
description = waypoint.longDescription ?? ""
icon = String(UnicodeScalar(Int(waypoint.icon)) ?? "📍")
2023-01-18 08:45:47 -08:00
latitude = Double(waypoint.latitudeI) / 1e7
longitude = Double(waypoint.longitudeI) / 1e7
2023-01-16 17:40:28 -08:00
if waypoint.expire != nil {
expires = true
expire = waypoint.expire ?? Date()
} else {
expires = false
}
2023-01-26 16:19:51 -08:00
if waypoint.locked > 0 {
locked = true
lockedTo = waypoint.locked
}
2023-01-18 08:45:47 -08:00
} else {
name = ""
description = ""
locked = false
expires = false
expire = Date.now.addingTimeInterval(60 * 480)
icon = "📍"
latitude = coordinate.coordinate?.latitude ?? 0
longitude = coordinate.coordinate?.longitude ?? 0
2023-01-16 17:40:28 -08:00
}
}
}
}