From 70052affc4e632e9d119e2a644c5e793a9aef440 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Fri, 17 Nov 2023 23:07:11 -0800 Subject: [PATCH] Better camera on the node map Smart position for the database (store just the latest postion if it is the same as the last) --- Meshtastic/Persistence/UpdateCoreData.swift | 7 +++++++ .../Views/Nodes/Helpers/Map/NodeMapSwiftUI.swift | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Meshtastic/Persistence/UpdateCoreData.swift b/Meshtastic/Persistence/UpdateCoreData.swift index 02c240c6..552e49cd 100644 --- a/Meshtastic/Persistence/UpdateCoreData.swift +++ b/Meshtastic/Persistence/UpdateCoreData.swift @@ -256,6 +256,13 @@ func upsertPositionPacket (packet: MeshPacket, context: NSManagedObjectContext) guard let mutablePositions = fetchedNode[0].positions!.mutableCopy() as? NSMutableOrderedSet else { return } + /// Don't save the same position over and over. + if mutablePositions.count > 0 { + let mostRecent = mutablePositions.lastObject as! PositionEntity + if mostRecent.latitudeI == position.latitudeI && mostRecent.longitudeI == position.latitudeI { + mutablePositions.remove(mostRecent) + } + } mutablePositions.add(position) fetchedNode[0].id = Int64(packet.from) fetchedNode[0].num = Int64(packet.from) diff --git a/Meshtastic/Views/Nodes/Helpers/Map/NodeMapSwiftUI.swift b/Meshtastic/Views/Nodes/Helpers/Map/NodeMapSwiftUI.swift index 863c24c5..0e07c8fd 100644 --- a/Meshtastic/Views/Nodes/Helpers/Map/NodeMapSwiftUI.swift +++ b/Meshtastic/Views/Nodes/Helpers/Map/NodeMapSwiftUI.swift @@ -48,7 +48,7 @@ struct NodeMapSwiftUI: View { var body: some View { let positionArray = node.positions?.array as? [PositionEntity] ?? [] - let mostRecent = node.positions?.lastObject as? PositionEntity + var mostRecent = node.positions?.lastObject as? PositionEntity let lineCoords = positionArray.compactMap({(position) -> CLLocationCoordinate2D in return position.nodeCoordinate ?? LocationHelper.DefaultLocation }) @@ -225,8 +225,12 @@ struct NodeMapSwiftUI: View { } } .onChange(of: node) { - let mostRecent = node.positions?.lastObject as? PositionEntity - position = MapCameraPosition.automatic//.camera(MapCamera(centerCoordinate: mostRecent!.coordinate, distance: 1500, heading: 0, pitch: 0)) + mostRecent = node.positions?.lastObject as? PositionEntity + if node.positions?.count ?? 0 > 1 { + position = .automatic + } else { + position = .camera(MapCamera(centerCoordinate: mostRecent!.coordinate, distance: 150, heading: 0, pitch: 60)) + } if let mostRecent { Task { scene = try? await fetchScene(for: mostRecent.coordinate) @@ -245,6 +249,12 @@ struct NodeMapSwiftUI: View { case .offline: mapStyle = MapStyle.hybrid(elevation: .realistic, pointsOfInterest: showPointsOfInterest ? .all : .excludingAll, showsTraffic: showTraffic) } + mostRecent = node.positions?.lastObject as? PositionEntity + if node.positions?.count ?? 0 > 1 { + position = .automatic + } else { + position = .camera(MapCamera(centerCoordinate: mostRecent!.coordinate, distance: 150, heading: 0, pitch: 60)) + } if self.scene == nil { Task { scene = try? await fetchScene(for: mostRecent!.coordinate)