From d0ab3b4e4a15d96fd4dd8783800accd28b5361c8 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 19 Mar 2023 21:09:02 -0700 Subject: [PATCH] Bug fix for weather --- .../Views/Map/Custom/MapViewSwiftUI.swift | 47 ++++++++++--------- Meshtastic/Views/Nodes/NodeDetail.swift | 30 +++++++++++- Meshtastic/Views/Nodes/NodeMap.swift | 3 +- 3 files changed, 56 insertions(+), 24 deletions(-) diff --git a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift index 435e62ac..44fc5c5c 100644 --- a/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift +++ b/Meshtastic/Views/Map/Custom/MapViewSwiftUI.swift @@ -21,7 +21,8 @@ struct MapViewSwiftUI: UIViewRepresentable { let mapViewType: MKMapType let userTrackingMode: MKUserTrackingMode let centeringMode: CenteringMode - let showBreadcrumbLines: Bool + let showRouteLines: Bool + let showNodeHistory: Bool let centerOnPositionsOnly: Bool @AppStorage("meshMapRecentering") private var recenter: Bool = false @@ -44,26 +45,28 @@ struct MapViewSwiftUI: UIViewRepresentable { let region = MKCoordinateRegion(center: center, span: span) mapView.setRegion(region, animated: true) // Set user (phone gps) tracking options + let latest = positions.filter { $0.latest == true } mapView.setUserTrackingMode(userTrackingMode, animated: true) - if userTrackingMode != MKUserTrackingMode.none { - mapView.showsUserLocation = true - } else { + if userTrackingMode == MKUserTrackingMode.none { mapView.showsUserLocation = false - } - switch centeringMode { - case .allAnnotations: - mapView.addAnnotations(positions) - if userTrackingMode == MKUserTrackingMode.none { - mapView.fitAllAnnotations() - } - case .allPositions: - if userTrackingMode == MKUserTrackingMode.none { - mapView.fit(annotations: positions, andShow: true) - } else { - mapView.addAnnotations(positions) + switch centeringMode { + case .allAnnotations: + mapView.addAnnotations(showNodeHistory ? positions : latest) + if userTrackingMode == MKUserTrackingMode.none { + mapView.fitAllAnnotations() + } + case .allPositions: + if userTrackingMode == MKUserTrackingMode.none { + mapView.addAnnotations(showNodeHistory ? positions : latest) + mapView.fit(annotations: positions, andShow: false) + } else { + mapView.addAnnotations(showNodeHistory ? positions : latest) + } } + } else { + mapView.addAnnotations(showNodeHistory ? positions : latest) + mapView.showsUserLocation = true } - // Other MKMapView Settings mapView.preferredConfiguration.elevationStyle = .realistic// .flat mapView.isPitchEnabled = true @@ -121,7 +124,7 @@ struct MapViewSwiftUI: UIViewRepresentable { self.presentCustomMapOverlayHash = self.customMapOverlay self.loadedLastUpdatedLocalMapFile = self.lastUpdatedLocalMapFile - if showBreadcrumbLines { + if showRouteLines { let nodePositions = positions.filter { $0.time! >= Calendar.current.startOfDay(for: Date()) } let lineCoords = nodePositions.map ({ (position) -> CLLocationCoordinate2D in @@ -146,20 +149,20 @@ struct MapViewSwiftUI: UIViewRepresentable { mapView.showsUserLocation = false switch centeringMode { case .allAnnotations: - mapView.addAnnotations(positions) + mapView.addAnnotations(showNodeHistory ? positions : latest) if recenter && userTrackingMode == MKUserTrackingMode.none { mapView.fitAllAnnotations() } case .allPositions: if recenter && userTrackingMode == MKUserTrackingMode.none { - mapView.fit(annotations: positions, andShow: true) + mapView.fit(annotations: showNodeHistory ? positions : latest, andShow: true) } else { - mapView.addAnnotations(positions) + mapView.addAnnotations(showNodeHistory ? positions : latest) } } } else { // Centering Done by tracking mode - mapView.addAnnotations(positions) + mapView.addAnnotations(latest) mapView.showsUserLocation = true } } diff --git a/Meshtastic/Views/Nodes/NodeDetail.swift b/Meshtastic/Views/Nodes/NodeDetail.swift index bb24243d..3f3f518a 100644 --- a/Meshtastic/Views/Nodes/NodeDetail.swift +++ b/Meshtastic/Views/Nodes/NodeDetail.swift @@ -17,6 +17,7 @@ struct NodeDetail: View { @State private var mapType: MKMapType = .standard @State var waypointCoordinate: CLLocationCoordinate2D? @State var editingWaypoint: Int = 0 + @State private var loadedWeather: Bool = false @State private var showingDetailsPopover = false @State private var showingForecast = false @State private var showingShutdownConfirm: Bool = false @@ -71,7 +72,8 @@ struct NodeDetail: View { mapViewType: mapType, userTrackingMode: MKUserTrackingMode.none, centeringMode: .allPositions, - showBreadcrumbLines: false, + showRouteLines: false, + showNodeHistory: true, centerOnPositionsOnly: true, customMapOverlay: self.customMapOverlay, overlays: self.overlays @@ -486,6 +488,32 @@ struct NodeDetail: View { mapType = .hybridFlyover } } + .task(id: node.num) { + if !loadedWeather { + do { + + if node.positions?.count ?? 0 > 0 { + + let mostRecent = node.positions?.lastObject as? PositionEntity + + let weather = try await WeatherService.shared.weather(for: mostRecent?.nodeLocation ?? CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude)) + condition = weather.currentWeather.condition + temperature = weather.currentWeather.temperature + humidity = Int(weather.currentWeather.humidity * 100) + symbolName = weather.currentWeather.symbolName + + let attribution = try await WeatherService.shared.attribution + attributionLink = attribution.legalPageURL + attributionLogo = colorScheme == .light ? attribution.combinedMarkLightURL : attribution.combinedMarkDarkURL + loadedWeather = true + } + } catch { + print("Could not gather weather information...", error.localizedDescription) + condition = .clear + symbolName = "cloud.fill" + } + } + } } } } diff --git a/Meshtastic/Views/Nodes/NodeMap.swift b/Meshtastic/Views/Nodes/NodeMap.swift index 7dbad566..297d8136 100644 --- a/Meshtastic/Views/Nodes/NodeMap.swift +++ b/Meshtastic/Views/Nodes/NodeMap.swift @@ -84,7 +84,8 @@ struct NodeMap: View { mapViewType: mapType, userTrackingMode: userTrackingMode, centeringMode: mapCenteringMode, - showBreadcrumbLines: false, + showRouteLines: false, + showNodeHistory: true, centerOnPositionsOnly: false, customMapOverlay: self.customMapOverlay, overlays: self.overlays