From 8c01abacce6664c6189207c319f898a4a16e7dbc Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Tue, 25 Apr 2023 21:44:34 -0700 Subject: [PATCH] Remove keyboard type setting, hook up map settings toggles --- Meshtastic/Extensions/UserDefaults.swift | 39 ++++++- Meshtastic/Model/UserSettings.swift | 7 -- .../Views/Map/Custom/MapViewSwiftUI.swift | 105 ++++++++++-------- .../Views/Messages/ChannelMessageList.swift | 3 +- .../Views/Messages/UserMessageList.swift | 3 +- Meshtastic/Views/Nodes/NodeMap.swift | 14 ++- Meshtastic/Views/Settings/AppSettings.swift | 10 -- 7 files changed, 110 insertions(+), 71 deletions(-) diff --git a/Meshtastic/Extensions/UserDefaults.swift b/Meshtastic/Extensions/UserDefaults.swift index c8d3f9e7..fdfdb575 100644 --- a/Meshtastic/Extensions/UserDefaults.swift +++ b/Meshtastic/Extensions/UserDefaults.swift @@ -10,11 +10,11 @@ import Foundation extension UserDefaults { enum Keys: String, CaseIterable { + case hasBeenLaunched case meshtasticUsername case preferredPeripheralId case provideLocation case provideLocationInterval - case keyboardType case meshMapType case meshMapCenteringMode case meshMapRecentering @@ -37,6 +37,43 @@ extension UserDefaults { } } + static var meshtasticUsername: String { + get { + UserDefaults.standard.string(forKey: "meshtasticUsername") ?? "" + } + set { + UserDefaults.standard.set(newValue, forKey: "meshtasticUsername") + } + } + + static var preferredPeripheralId: String { + get { + UserDefaults.standard.string(forKey: "preferredPeripheralId") ?? "" + } + set { + UserDefaults.standard.set(newValue, forKey: "preferredPeripheralId") + } + } + + static var provideLocation: Bool { + get { + let result = UserDefaults.standard.bool(forKey: "provideLocation") + UserDefaults.standard.set(true, forKey: "provideLocation") + return result + } set { + UserDefaults.standard.set(newValue, forKey: "provideLocation") + } + } + + static var provideLocationInterval: Int { + get { + UserDefaults.standard.integer(forKey: "provideLocationInterval") + } + set { + UserDefaults.standard.set(newValue, forKey: "provideLocationInterval") + } + } + static var mapType: Int { get { UserDefaults.standard.integer(forKey: "meshMapType") diff --git a/Meshtastic/Model/UserSettings.swift b/Meshtastic/Model/UserSettings.swift index cc4d89a5..c40125d9 100644 --- a/Meshtastic/Model/UserSettings.swift +++ b/Meshtastic/Model/UserSettings.swift @@ -31,12 +31,6 @@ class UserSettings: ObservableObject { UserDefaults.standard.synchronize() } } - @Published var keyboardType: Int { - didSet { - UserDefaults.standard.set(keyboardType, forKey: "keyboardType") - UserDefaults.standard.synchronize() - } - } @Published var meshMapType: String { didSet { UserDefaults.standard.set(meshMapType, forKey: "meshMapType") @@ -86,7 +80,6 @@ class UserSettings: ObservableObject { self.preferredPeripheralId = UserDefaults.standard.object(forKey: "preferredPeripheralId") as? String ?? "" self.provideLocation = UserDefaults.standard.object(forKey: "provideLocation") as? Bool ?? false self.provideLocationInterval = UserDefaults.standard.object(forKey: "provideLocationInterval") as? Int ?? 900 - self.keyboardType = UserDefaults.standard.object(forKey: "keyboardType") as? Int ?? 0 self.meshMapType = UserDefaults.standard.string(forKey: "meshMapType") ?? "standard" self.meshMapCenteringMode = UserDefaults.standard.object(forKey: "meshMapCenteringMode") as? Int ?? 0 self.meshMapRecentering = UserDefaults.standard.object(forKey: "meshMapRecentering") as? Bool ?? false diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index f4fbbdd7..6e05c939 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -23,7 +23,8 @@ struct MapViewSwiftUI: UIViewRepresentable { let userTrackingMode: MKUserTrackingMode let showNodeHistory: Bool let showRouteLines: Bool - @AppStorage("meshMapRecentering") private var recenter: Bool = false + // User Defaults + //@State var enableMapRecentering: Bool = UserDefaults.enableMapRecentering // Offline Map Tiles @AppStorage("lastUpdatedLocalMapFile") private var lastUpdatedLocalMapFile = 0 @State private var loadedLastUpdatedLocalMapFile = 0 @@ -119,6 +120,7 @@ struct MapViewSwiftUI: UIViewRepresentable { mapView.mapType = mapViewType + // Offline maps and tile server settings if UserDefaults.enableOfflineMaps { if tileServerUrl.count > 0 { @@ -166,60 +168,69 @@ struct MapViewSwiftUI: UIViewRepresentable { } } + let latest = positions + .filter { $0.latest == true } + .sorted { $0.nodePosition?.num ?? 0 > $1.nodePosition?.num ?? -1 } - DispatchQueue.main.async { - let latest = positions - .filter { $0.latest == true } - .sorted { $0.nodePosition?.num ?? 0 > $1.nodePosition?.num ?? -1 } + // Node Route Lines + if showRouteLines { + // Remove all existing PolyLine Overlays + for overlay in mapView.overlays { + if overlay is MKPolyline { + mapView.removeOverlay(overlay) + } + } + var lineIndex = 0 + for position in latest { + + let nodePositions = positions.filter { $0.nodePosition?.num ?? 0 == position.nodePosition?.num ?? -1 } + let lineCoords = nodePositions.map ({ + (position) -> CLLocationCoordinate2D in + return position.nodeCoordinate! + }) + let polyline = MKPolyline(coordinates: lineCoords, count: nodePositions.count) + polyline.title = "\(String(position.nodePosition?.num ?? 0))" + mapView.addOverlay(polyline, level: .aboveLabels) + lineIndex += 1 + // There are 18 colors for lines, start over if we are at index 17 + if lineIndex > 17 { + lineIndex = 0 + } + } + } + + //DispatchQueue.main.async { + + + let annotationCount = waypoints.count + (showNodeHistory ? positions.count : latest.count) - // if annotationCount != mapView.annotations.count { + if annotationCount != mapView.annotations.count { print("Annotation Count: \(annotationCount) Map Annotations: \(mapView.annotations.count)") mapView.removeAnnotations(mapView.annotations) mapView.addAnnotations(waypoints) - if showRouteLines { - // Remove all existing PolyLine Overlays - for overlay in mapView.overlays { - if overlay is MKPolyline { - mapView.removeOverlay(overlay) - } - } - var lineIndex = 0 - for position in latest { - - let nodePositions = positions.filter { $0.nodePosition?.num ?? 0 == position.nodePosition?.num ?? -1 } - let lineCoords = nodePositions.map ({ - (position) -> CLLocationCoordinate2D in - return position.nodeCoordinate! - }) - let polyline = MKPolyline(coordinates: lineCoords, count: nodePositions.count) - polyline.title = "\(String(position.nodePosition?.num ?? 0))" - mapView.addOverlay(polyline, level: .aboveLabels) - lineIndex += 1 - // There are 18 colors for lines, start over if we are at index 17 - if lineIndex > 17 { - lineIndex = 0 - } - } - } - if userTrackingMode == MKUserTrackingMode.none { - mapView.showsUserLocation = false + + } + if userTrackingMode == MKUserTrackingMode.none { + mapView.showsUserLocation = false + + if UserDefaults.enableMapRecentering { + mapView.addAnnotations(showNodeHistory ? positions : latest) - if recenter { - if latest.count > 1 { - mapView.fitAllAnnotations() - } else { - mapView.fit(annotations:showNodeHistory ? positions : latest, andShow: false) - } - } - } else { - // Centering Done by tracking mode - mapView.addAnnotations(showNodeHistory ? positions : latest) - mapView.showsUserLocation = true + mapView.fitAllAnnotations() +// if latest.count > 1 { +// mapView.fitAllAnnotations() +// } else { +// mapView.fit(annotations:showNodeHistory ? positions : latest, andShow: false) +// } } - mapView.setUserTrackingMode(userTrackingMode, animated: true) - //} - } + } else { + // Centering Done by tracking mode + mapView.addAnnotations(showNodeHistory ? positions : latest) + mapView.showsUserLocation = true + } + mapView.setUserTrackingMode(userTrackingMode, animated: true) + // } } func makeCoordinator() -> MapCoordinator { diff --git a/Meshtastic/Views/Messages/ChannelMessageList.swift b/Meshtastic/Views/Messages/ChannelMessageList.swift index 0934b690..5a4f8891 100644 --- a/Meshtastic/Views/Messages/ChannelMessageList.swift +++ b/Meshtastic/Views/Messages/ChannelMessageList.swift @@ -275,7 +275,6 @@ struct ChannelMessageList: View { HStack(alignment: .top) { ZStack { - let kbType = UIKeyboardType(rawValue: UserDefaults.standard.object(forKey: "keyboardType") as? Int ?? 0) TextField("message", text: $typingMessage, axis: .vertical) .onChange(of: typingMessage, perform: { value in totalBytes = value.utf8.count @@ -290,7 +289,7 @@ struct ChannelMessageList: View { } } }) - .keyboardType(kbType!) + .keyboardType(.default) .toolbar { ToolbarItemGroup(placement: .keyboard) { Button("dismiss.keyboard") { diff --git a/Meshtastic/Views/Messages/UserMessageList.swift b/Meshtastic/Views/Messages/UserMessageList.swift index fa2661be..87fa8bcc 100644 --- a/Meshtastic/Views/Messages/UserMessageList.swift +++ b/Meshtastic/Views/Messages/UserMessageList.swift @@ -266,7 +266,6 @@ struct UserMessageList: View { HStack(alignment: .top) { ZStack { - let kbType = UIKeyboardType(rawValue: UserDefaults.standard.object(forKey: "keyboardType") as? Int ?? 0) TextField("message", text: $typingMessage, axis: .vertical) .onChange(of: typingMessage, perform: { value in totalBytes = value.utf8.count @@ -281,7 +280,7 @@ struct UserMessageList: View { } } }) - .keyboardType(kbType!) + .keyboardType(.default) .toolbar { ToolbarItemGroup(placement: .keyboard) { Button("dismiss.keyboard") { diff --git a/Meshtastic/Views/Nodes/NodeMap.swift b/Meshtastic/Views/Nodes/NodeMap.swift index bfbd7d1d..03c5dc70 100644 --- a/Meshtastic/Views/Nodes/NodeMap.swift +++ b/Meshtastic/Views/Nodes/NodeMap.swift @@ -103,19 +103,29 @@ struct NodeMap: View { Label("map.recentering", systemImage: "camera.metering.center.weighted") } .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + .onTapGesture { + self.enableMapRecentering.toggle() + UserDefaults.enableMapRecentering = self.enableMapRecentering + } + Toggle(isOn: $enableMapNodeHistoryPins) { Label("Show Node History", systemImage: "building.columns.fill") } .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + .onTapGesture { + self.enableMapNodeHistoryPins.toggle() + UserDefaults.enableMapNodeHistoryPins = self.enableMapNodeHistoryPins + } Toggle(isOn: $enableMapRouteLines) { Label("Show Route Lines", systemImage: "road.lanes") } .toggleStyle(SwitchToggleStyle(tint: .accentColor)) - .onChange(of: (enableMapRouteLines)) { newEnableMapRouteLines in - UserDefaults.enableMapRouteLines = newEnableMapRouteLines + .onTapGesture { + self.enableMapRouteLines.toggle() + UserDefaults.enableMapRouteLines = self.enableMapRouteLines } } Section(header: Text("Offline Maps")) { diff --git a/Meshtastic/Views/Settings/AppSettings.swift b/Meshtastic/Views/Settings/AppSettings.swift index 41e8f10c..303bdfb6 100644 --- a/Meshtastic/Views/Settings/AppSettings.swift +++ b/Meshtastic/Views/Settings/AppSettings.swift @@ -27,16 +27,6 @@ struct AppSettings: View { .disableAutocorrection(true) .listRowSeparator(.visible) } - Section(header: Text("options")) { - - Picker("keyboard.type", selection: $userSettings.keyboardType) { - ForEach(KeyboardType.allCases) { kb in - Text(kb.description) - } - } - .pickerStyle(DefaultPickerStyle()) - - } Section(header: Text("phone.gps")) { let accuracy = Measurement(value: locationHelper.locationManager.location?.horizontalAccuracy ?? 300, unit: UnitLength.meters)