From 3273a59ce276510714aeb2f4ba49910aafb0f36d Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Thu, 27 Apr 2023 18:01:23 -0700 Subject: [PATCH 1/2] Fix map line crash --- Meshtastic.xcodeproj/project.pbxproj | 4 ++++ Meshtastic/Enums/AppSettingsEnums.swift | 23 ++++++++++++++++--- Meshtastic/Extensions/Array.swift | 18 +++++++++++++++ .../Views/Map/Custom/MapViewSwiftUI.swift | 6 ++--- Meshtastic/Views/Nodes/NodeMap.swift | 22 +++++++++++++++--- 5 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 Meshtastic/Extensions/Array.swift diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index d6345c82..8f5d8da8 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -128,6 +128,7 @@ DDDB445029F8AC9C00EE2349 /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDB444F29F8AC9C00EE2349 /* UIImage.swift */; }; DDDB445229F8ACF900EE2349 /* Date.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDB445129F8ACF900EE2349 /* Date.swift */; }; DDDB445429F8AD1600EE2349 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDB445329F8AD1600EE2349 /* Data.swift */; }; + DDDB445629FB4BFD00EE2349 /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDB445529FB4BFD00EE2349 /* Array.swift */; }; DDDE59F529AF163D00490C6C /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD41A61C29AE7E8E003C5A37 /* WidgetKit.framework */; }; DDDE59F629AF163D00490C6C /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD41A61E29AE7E8F003C5A37 /* SwiftUI.framework */; }; DDDE59F929AF163D00490C6C /* WidgetsBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDE59F829AF163D00490C6C /* WidgetsBundle.swift */; }; @@ -319,6 +320,7 @@ DDDB444F29F8AC9C00EE2349 /* UIImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIImage.swift; sourceTree = ""; }; DDDB445129F8ACF900EE2349 /* Date.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Date.swift; sourceTree = ""; }; DDDB445329F8AD1600EE2349 /* Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = ""; }; + DDDB445529FB4BFD00EE2349 /* Array.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Array.swift; sourceTree = ""; }; DDDD527729B5B83F0045BC3C /* MeshtasticDataModelV9.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MeshtasticDataModelV9.xcdatamodel; sourceTree = ""; }; DDDE59F429AF163D00490C6C /* WidgetsExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = WidgetsExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; DDDE59F829AF163D00490C6C /* WidgetsBundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetsBundle.swift; sourceTree = ""; }; @@ -715,6 +717,7 @@ DDDB444729F8A9C900EE2349 /* String.swift */, DDDB444F29F8AC9C00EE2349 /* UIImage.swift */, DDDB443F29F79AB000EE2349 /* UserDefaults.swift */, + DDDB445529FB4BFD00EE2349 /* Array.swift */, ); path = Extensions; sourceTree = ""; @@ -974,6 +977,7 @@ DD6193792863875F00E59241 /* SerialConfig.swift in Sources */, DDA0B6B2294CDC55001356EC /* Channels.swift in Sources */, DD4A911E2708C65400501B7E /* AppSettings.swift in Sources */, + DDDB445629FB4BFD00EE2349 /* Array.swift in Sources */, DD5E5209298EE33B00D21B61 /* module_config.pb.swift in Sources */, DD2160AF28C5552500C17253 /* MQTTConfig.swift in Sources */, DDDB444229F8A88700EE2349 /* Double.swift in Sources */, diff --git a/Meshtastic/Enums/AppSettingsEnums.swift b/Meshtastic/Enums/AppSettingsEnums.swift index 994a0e90..97435ff3 100644 --- a/Meshtastic/Enums/AppSettingsEnums.swift +++ b/Meshtastic/Enums/AppSettingsEnums.swift @@ -152,12 +152,27 @@ enum LocationUpdateInterval: Int, CaseIterable, Identifiable { } enum MapTileServerLinks: Int, CaseIterable, Identifiable { - case openStreetMaps - case wikimedia - case nationalMap + case none = 0 + case openStreetMaps = 1 + case wikimedia = 2 + case nationalMap = 3 var id: Int { self.rawValue } + var description: String { + switch self { + case .none: + return "Please Select" + case .wikimedia: + return "Wikimedia" + case .openStreetMaps: + return "Open Street Maps" + case .nationalMap: + return "US National Map" + } + } var tileUrl: String { switch self { + case .none: + return "" case .wikimedia: return "https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png" case .openStreetMaps: @@ -168,6 +183,8 @@ enum MapTileServerLinks: Int, CaseIterable, Identifiable { } var zoomRange: [Int] { switch self { + case .none: + return [Int](0...1) case .wikimedia: return [Int](0...24) case .openStreetMaps: diff --git a/Meshtastic/Extensions/Array.swift b/Meshtastic/Extensions/Array.swift new file mode 100644 index 00000000..883dd6e9 --- /dev/null +++ b/Meshtastic/Extensions/Array.swift @@ -0,0 +1,18 @@ +// +// Array.swift +// Meshtastic +// +// Created by Garth Vander Houwen on 4/27/23. +// + +import Foundation + + +extension Array { + func mapNonNils(_ transform: (E) -> T) -> [T] where Element == Optional { + return self.compactMap { element in + guard let element = element else { return nil } + return transform(element) + } + } +} diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index d3959646..4d62b487 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -160,10 +160,10 @@ struct MapViewSwiftUI: UIViewRepresentable { var lineIndex = 0 for position in latest { - let nodePositions = positions.filter { $0.nodePosition?.num ?? 0 == position.nodePosition?.num ?? -1 } - let lineCoords = nodePositions.map ({ + let nodePositions = positions.filter { $0.nodeCoordinate != nil && $0.nodePosition?.num ?? 0 == position.nodePosition?.num ?? -1 } + let lineCoords = nodePositions.compactMap ({ (position) -> CLLocationCoordinate2D in - return position.nodeCoordinate! + return position.nodeCoordinate ?? LocationHelper.DefaultLocation }) let polyline = MKPolyline(coordinates: lineCoords, count: nodePositions.count) polyline.title = "\(String(position.nodePosition?.num ?? 0))" diff --git a/Meshtastic/Views/Nodes/NodeMap.swift b/Meshtastic/Views/Nodes/NodeMap.swift index 03c5dc70..d0f4f418 100644 --- a/Meshtastic/Views/Nodes/NodeMap.swift +++ b/Meshtastic/Views/Nodes/NodeMap.swift @@ -15,7 +15,7 @@ struct NodeMap: View { @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager - @AppStorage("meshMapType") private var meshMapType = 0 + @State var meshMapType: Int = UserDefaults.mapType @State var enableMapRecentering: Bool = UserDefaults.enableMapRecentering @State var enableMapRouteLines: Bool = UserDefaults.enableMapRouteLines @State var enableMapNodeHistoryPins: Bool = UserDefaults.enableMapNodeHistoryPins @@ -32,8 +32,9 @@ struct NodeMap: View { ), animation: .none) private var waypoints: FetchedResults - @State private var mapType: MKMapType = .standard + @State var mapType: MKMapType = .standard @State var selectedTracking: UserTrackingModes = .none + @State var selectedTileServer: MapTileServerLinks = .wikimedia @State var isPresentingInfoSheet: Bool = false @State var waypointCoordinate: WaypointCoordinate? @@ -137,8 +138,23 @@ struct NodeMap: View { self.enableOfflineMaps.toggle() UserDefaults.enableOfflineMaps = self.enableOfflineMaps } + Text("If you have shared a MBTiles file with meshtastic it will be loaded.") + .font(.caption) + .foregroundColor(.gray) + if UserDefaults.enableOfflineMaps { HStack { +// Picker("Tile Servers", selection: $selectedTileServer) { +// ForEach(MapTileServerLinks.allCases) { ts in +// Text(ts.description) +// .tag(ts.id) +// } +// } +// .pickerStyle(.menu) +// .onChange(of: (selectedTileServer)) { newTileServer in +// +// mapTileServer = selectedTileServer.tileUrl +// } Label("Tile Server", systemImage: "square.grid.3x2") TextField( @@ -147,7 +163,7 @@ struct NodeMap: View { axis: .vertical ) .foregroundColor(.gray) - .font(.caption2) + .font(.caption) .onChange(of: (mapTileServer)) { newMapTileServer in UserDefaults.mapTileServer = newMapTileServer } From f4a24d95e00903ab7a56ac4c3b4d791a43327a7b Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Thu, 27 Apr 2023 18:02:17 -0700 Subject: [PATCH 2/2] Remove unused extension --- Meshtastic.xcodeproj/project.pbxproj | 4 ---- Meshtastic/Extensions/Array.swift | 18 ------------------ 2 files changed, 22 deletions(-) delete mode 100644 Meshtastic/Extensions/Array.swift diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index 8f5d8da8..d6345c82 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -128,7 +128,6 @@ DDDB445029F8AC9C00EE2349 /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDB444F29F8AC9C00EE2349 /* UIImage.swift */; }; DDDB445229F8ACF900EE2349 /* Date.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDB445129F8ACF900EE2349 /* Date.swift */; }; DDDB445429F8AD1600EE2349 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDB445329F8AD1600EE2349 /* Data.swift */; }; - DDDB445629FB4BFD00EE2349 /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDB445529FB4BFD00EE2349 /* Array.swift */; }; DDDE59F529AF163D00490C6C /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD41A61C29AE7E8E003C5A37 /* WidgetKit.framework */; }; DDDE59F629AF163D00490C6C /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD41A61E29AE7E8F003C5A37 /* SwiftUI.framework */; }; DDDE59F929AF163D00490C6C /* WidgetsBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDE59F829AF163D00490C6C /* WidgetsBundle.swift */; }; @@ -320,7 +319,6 @@ DDDB444F29F8AC9C00EE2349 /* UIImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIImage.swift; sourceTree = ""; }; DDDB445129F8ACF900EE2349 /* Date.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Date.swift; sourceTree = ""; }; DDDB445329F8AD1600EE2349 /* Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = ""; }; - DDDB445529FB4BFD00EE2349 /* Array.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Array.swift; sourceTree = ""; }; DDDD527729B5B83F0045BC3C /* MeshtasticDataModelV9.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MeshtasticDataModelV9.xcdatamodel; sourceTree = ""; }; DDDE59F429AF163D00490C6C /* WidgetsExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = WidgetsExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; DDDE59F829AF163D00490C6C /* WidgetsBundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetsBundle.swift; sourceTree = ""; }; @@ -717,7 +715,6 @@ DDDB444729F8A9C900EE2349 /* String.swift */, DDDB444F29F8AC9C00EE2349 /* UIImage.swift */, DDDB443F29F79AB000EE2349 /* UserDefaults.swift */, - DDDB445529FB4BFD00EE2349 /* Array.swift */, ); path = Extensions; sourceTree = ""; @@ -977,7 +974,6 @@ DD6193792863875F00E59241 /* SerialConfig.swift in Sources */, DDA0B6B2294CDC55001356EC /* Channels.swift in Sources */, DD4A911E2708C65400501B7E /* AppSettings.swift in Sources */, - DDDB445629FB4BFD00EE2349 /* Array.swift in Sources */, DD5E5209298EE33B00D21B61 /* module_config.pb.swift in Sources */, DD2160AF28C5552500C17253 /* MQTTConfig.swift in Sources */, DDDB444229F8A88700EE2349 /* Double.swift in Sources */, diff --git a/Meshtastic/Extensions/Array.swift b/Meshtastic/Extensions/Array.swift deleted file mode 100644 index 883dd6e9..00000000 --- a/Meshtastic/Extensions/Array.swift +++ /dev/null @@ -1,18 +0,0 @@ -// -// Array.swift -// Meshtastic -// -// Created by Garth Vander Houwen on 4/27/23. -// - -import Foundation - - -extension Array { - func mapNonNils(_ transform: (E) -> T) -> [T] where Element == Optional { - return self.compactMap { element in - guard let element = element else { return nil } - return transform(element) - } - } -}