diff --git a/Meshtastic/Extensions/CoreData/PositionEntityExtension.swift b/Meshtastic/Extensions/CoreData/PositionEntityExtension.swift index 90b15b5e..a9eea507 100644 --- a/Meshtastic/Extensions/CoreData/PositionEntityExtension.swift +++ b/Meshtastic/Extensions/CoreData/PositionEntityExtension.swift @@ -13,7 +13,7 @@ import SwiftUI extension PositionEntity { - static func allPositionsFetchRequest() -> NSFetchRequest { + @MainActor static func allPositionsFetchRequest() -> NSFetchRequest { let request: NSFetchRequest = PositionEntity.fetchRequest() request.fetchLimit = 1000 request.returnsObjectsAsFaults = false @@ -22,9 +22,9 @@ extension PositionEntity { request.sortDescriptors = [NSSortDescriptor(key: "time", ascending: false)] let positionPredicate = NSPredicate(format: "nodePosition != nil && (nodePosition.user.shortName != nil || nodePosition.user.shortName != '') && latest == true") - let pointOfInterest = LocationHelper.currentLocation + let pointOfInterest = LocationsHandler.currentLocation - if pointOfInterest.latitude != LocationHelper.DefaultLocation.latitude && pointOfInterest.longitude != LocationHelper.DefaultLocation.longitude { + if pointOfInterest.latitude != LocationsHandler.DefaultLocation.latitude && pointOfInterest.longitude != LocationsHandler.DefaultLocation.longitude { let d: Double = UserDefaults.meshMapDistance * 1.1 let r: Double = 6371009 let meanLatitidue = pointOfInterest.latitude * .pi / 180 @@ -88,7 +88,7 @@ extension PositionEntity { } extension PositionEntity: MKAnnotation { - public var coordinate: CLLocationCoordinate2D { nodeCoordinate ?? LocationHelper.DefaultLocation } + public var coordinate: CLLocationCoordinate2D { nodeCoordinate ?? LocationsHandler.DefaultLocation } public var title: String? { nodePosition?.user?.shortName ?? "unknown".localized } public var subtitle: String? { time?.formatted() } } diff --git a/Meshtastic/Extensions/CoreData/WaypointEntityExtension.swift b/Meshtastic/Extensions/CoreData/WaypointEntityExtension.swift index b17ccecf..2f538b62 100644 --- a/Meshtastic/Extensions/CoreData/WaypointEntityExtension.swift +++ b/Meshtastic/Extensions/CoreData/WaypointEntityExtension.swift @@ -60,7 +60,7 @@ extension WaypointEntity { } extension WaypointEntity: MKAnnotation { - public var coordinate: CLLocationCoordinate2D { waypointCoordinate ?? LocationHelper.DefaultLocation } + public var coordinate: CLLocationCoordinate2D { waypointCoordinate ?? LocationsHandler.DefaultLocation } public var title: String? { name ?? "Dropped Pin" } public var subtitle: String? { (longDescription ?? "") + diff --git a/Meshtastic/Helpers/LocationsHandler.swift b/Meshtastic/Helpers/LocationsHandler.swift index a215667b..99d8b41a 100644 --- a/Meshtastic/Helpers/LocationsHandler.swift +++ b/Meshtastic/Helpers/LocationsHandler.swift @@ -113,6 +113,12 @@ import OSLog } static let DefaultLocation = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090) + static var currentLocation: CLLocationCoordinate2D { + guard let location = shared.manager.location else { + return DefaultLocation + } + return location.coordinate + } static var satsInView: Int { var sats = 0 @@ -139,4 +145,6 @@ import OSLog } return sats } + + } diff --git a/Meshtastic/Views/Helpers/Weather/NodeWeatherForecast.swift b/Meshtastic/Views/Helpers/Weather/NodeWeatherForecast.swift index 15198c50..5fec377e 100644 --- a/Meshtastic/Views/Helpers/Weather/NodeWeatherForecast.swift +++ b/Meshtastic/Views/Helpers/Weather/NodeWeatherForecast.swift @@ -211,7 +211,7 @@ struct NodeWeatherForecast { struct NodeWeatherForecastView_Previews: PreviewProvider { static var previews: some View { - NodeWeatherForecastView(location: CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude) ) + NodeWeatherForecastView(location: CLLocation(latitude: LocationsHandler.currentLocation.latitude, longitude: LocationsHandler.currentLocation.longitude) ) .aspectRatio(2, contentMode: .fit) .padding() .previewLayout(.sizeThatFits) diff --git a/Meshtastic/Views/Messages/UserList.swift b/Meshtastic/Views/Messages/UserList.swift index a8986250..b0602d5f 100644 --- a/Meshtastic/Views/Messages/UserList.swift +++ b/Meshtastic/Views/Messages/UserList.swift @@ -346,9 +346,9 @@ struct UserList: View { } /// Distance if distanceFilter { - let pointOfInterest = LocationHelper.currentLocation + let pointOfInterest = LocationsHandler.currentLocation - if pointOfInterest.latitude != LocationHelper.DefaultLocation.latitude && pointOfInterest.longitude != LocationHelper.DefaultLocation.longitude { + if pointOfInterest.latitude != LocationsHandler.DefaultLocation.latitude && pointOfInterest.longitude != LocationsHandler.DefaultLocation.longitude { let d: Double = maxDistance * 1.1 let r: Double = 6371009 let meanLatitidue = pointOfInterest.latitude * .pi / 180 diff --git a/Meshtastic/Views/Nodes/Helpers/Map/MapContent/MeshMapContent.swift b/Meshtastic/Views/Nodes/Helpers/Map/MapContent/MeshMapContent.swift index dd49484a..6a0374f2 100644 --- a/Meshtastic/Views/Nodes/Helpers/Map/MapContent/MeshMapContent.swift +++ b/Meshtastic/Views/Nodes/Helpers/Map/MapContent/MeshMapContent.swift @@ -85,7 +85,7 @@ struct MeshMapContent: MapContent { let nodePositions = Array(positions) as? [PositionEntity] { if showRouteLines { let routeCoords = nodePositions.compactMap({(pos) -> CLLocationCoordinate2D in - return pos.nodeCoordinate ?? LocationHelper.DefaultLocation + return pos.nodeCoordinate ?? LocationsHandler.DefaultLocation }) let gradient = LinearGradient( colors: [Color(nodeColor.lighter().lighter()), Color(nodeColor.lighter()), Color(nodeColor)], @@ -148,9 +148,9 @@ struct MeshMapContent: MapContent { ForEach(routes) { route in if let routeLocations = route.locations, let locations = Array(routeLocations) as? [LocationEntity] { let routeCoords = locations.compactMap {(loc) -> CLLocationCoordinate2D in - return loc.locationCoordinate ?? LocationHelper.DefaultLocation + return loc.locationCoordinate ?? LocationsHandler.DefaultLocation } - Annotation("Start", coordinate: routeCoords.first ?? LocationHelper.DefaultLocation) { + Annotation("Start", coordinate: routeCoords.first ?? LocationsHandler.DefaultLocation) { ZStack { Circle() .fill(Color(.green)) @@ -159,7 +159,7 @@ struct MeshMapContent: MapContent { } } .annotationTitles(.automatic) - Annotation("Finish", coordinate: routeCoords.last ?? LocationHelper.DefaultLocation) { + Annotation("Finish", coordinate: routeCoords.last ?? LocationsHandler.DefaultLocation) { ZStack { Circle() .fill(Color(.black)) diff --git a/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift b/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift index 71d62590..2c2c267a 100644 --- a/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift +++ b/Meshtastic/Views/Nodes/Helpers/Map/WaypointForm.swift @@ -38,7 +38,7 @@ struct WaypointForm: View { .font(.largeTitle) Divider() Form { - let distance = CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude).distance(from: CLLocation(latitude: waypoint.coordinate.latitude, longitude: waypoint.coordinate.longitude )) + let distance = CLLocation(latitude: LocationsHandler.currentLocation.latitude, longitude: LocationsHandler.currentLocation.longitude).distance(from: CLLocation(latitude: waypoint.coordinate.latitude, longitude: waypoint.coordinate.longitude )) Section(header: Text("Coordinate") ) { HStack { Text("Location:") @@ -335,8 +335,8 @@ struct WaypointForm: View { .padding(.bottom, 5) } /// Distance - if LocationHelper.currentLocation.distance(from: LocationHelper.DefaultLocation) > 0.0 { - let metersAway = waypoint.coordinate.distance(from: LocationHelper.currentLocation) + if LocationsHandler.currentLocation.distance(from: LocationsHandler.DefaultLocation) > 0.0 { + let metersAway = waypoint.coordinate.distance(from: LocationsHandler.currentLocation) Label { Text("distance".localized + ": \(distanceFormatter.string(fromDistance: Double(metersAway)))") .foregroundColor(.primary) diff --git a/Meshtastic/Views/Nodes/NodeList.swift b/Meshtastic/Views/Nodes/NodeList.swift index 3bf88f7a..32e01bb2 100644 --- a/Meshtastic/Views/Nodes/NodeList.swift +++ b/Meshtastic/Views/Nodes/NodeList.swift @@ -418,9 +418,9 @@ struct NodeList: View { } /// Distance if distanceFilter { - let pointOfInterest = LocationHelper.currentLocation + let pointOfInterest = LocationsHandler.currentLocation - if pointOfInterest.latitude != LocationHelper.DefaultLocation.latitude && pointOfInterest.longitude != LocationHelper.DefaultLocation.longitude { + if pointOfInterest.latitude != LocationsHandler.DefaultLocation.latitude && pointOfInterest.longitude != LocationsHandler.DefaultLocation.longitude { let d: Double = maxDistance * 1.1 let r: Double = 6371009 let meanLatitidue = pointOfInterest.latitude * .pi / 180 diff --git a/Meshtastic/Views/Settings/Routes.swift b/Meshtastic/Views/Settings/Routes.swift index 174221d2..74f9ac72 100644 --- a/Meshtastic/Views/Settings/Routes.swift +++ b/Meshtastic/Views/Settings/Routes.swift @@ -165,7 +165,7 @@ struct Routes: View { if selectedRoute != nil { let locationArray = selectedRoute?.locations?.array as? [LocationEntity] ?? [] let lineCoords = locationArray.compactMap({(location) -> CLLocationCoordinate2D in - return location.locationCoordinate ?? LocationHelper.DefaultLocation + return location.locationCoordinate ?? LocationsHandler.DefaultLocation }) Form { TextField( @@ -248,7 +248,7 @@ struct Routes: View { hasChanges = true } Map { - Annotation("Start", coordinate: lineCoords.first ?? LocationHelper.DefaultLocation) { + Annotation("Start", coordinate: lineCoords.first ?? LocationsHandler.DefaultLocation) { ZStack { Circle() .fill(Color(.green)) @@ -257,7 +257,7 @@ struct Routes: View { } } .annotationTitles(.automatic) - Annotation("Finish", coordinate: lineCoords.last ?? LocationHelper.DefaultLocation) { + Annotation("Finish", coordinate: lineCoords.last ?? LocationsHandler.DefaultLocation) { ZStack { Circle() .fill(Color(.black))