Meshtastic-Apple/Meshtastic/Extensions/CoreData/PositionEntityExtension.swift

95 lines
3 KiB
Swift
Raw Normal View History

//
// PersistenceEntityExtenstion.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 11/28/21.
//
2021-12-16 14:13:54 -08:00
import CoreData
import CoreLocation
import MapKit
import MeshtasticProtobufs
2021-12-16 14:13:54 -08:00
import SwiftUI
extension PositionEntity {
2025-01-21 09:19:14 -08:00
@MainActor static func allPositionsFetchRequest() -> NSFetchRequest<PositionEntity> {
2024-03-23 09:01:44 -07:00
let request: NSFetchRequest<PositionEntity> = PositionEntity.fetchRequest()
2024-05-28 12:47:06 -07:00
request.fetchLimit = 1000
2024-05-28 12:39:30 -07:00
request.returnsObjectsAsFaults = false
request.includesSubentities = true
2024-03-23 09:01:44 -07:00
request.returnsDistinctResults = true
2024-03-23 18:01:20 -07:00
request.sortDescriptors = [NSSortDescriptor(key: "time", ascending: false)]
2024-04-02 11:16:32 -07:00
let positionPredicate = NSPredicate(format: "nodePosition != nil && (nodePosition.user.shortName != nil || nodePosition.user.shortName != '') && latest == true")
2025-01-21 09:19:14 -08:00
let pointOfInterest = LocationsHandler.currentLocation
2025-01-21 09:19:14 -08:00
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
let deltaLatitude = d / r * 180 / .pi
let deltaLongitude = d / (r * cos(meanLatitidue)) * 180 / .pi
let minLatitude: Double = pointOfInterest.latitude - deltaLatitude
let maxLatitude: Double = pointOfInterest.latitude + deltaLatitude
let minLongitude: Double = pointOfInterest.longitude - deltaLongitude
let maxLongitude: Double = pointOfInterest.longitude + deltaLongitude
let distancePredicate = NSPredicate(format: "(%lf <= (longitudeI / 1e7)) AND ((longitudeI / 1e7) <= %lf) AND (%lf <= (latitudeI / 1e7)) AND ((latitudeI / 1e7) <= %lf)", minLongitude, maxLongitude, minLatitude, maxLatitude)
2024-03-24 13:13:05 -07:00
request.predicate = NSCompoundPredicate(type: .and, subpredicates: [positionPredicate, distancePredicate])
} else {
request.predicate = positionPredicate
}
2024-03-23 09:01:44 -07:00
return request
}
2021-12-25 23:48:12 -08:00
2021-12-16 14:13:54 -08:00
var latitude: Double? {
let d = Double(latitudeI)
if d == 0 {
2021-12-18 20:49:50 -08:00
return 0
2021-12-16 14:13:54 -08:00
}
return d / 1e7
}
2021-12-25 23:48:12 -08:00
2021-12-16 14:13:54 -08:00
var longitude: Double? {
let d = Double(longitudeI)
if d == 0 {
2021-12-18 20:49:50 -08:00
return 0
2021-12-16 14:13:54 -08:00
}
return d / 1e7
}
2021-12-25 23:48:12 -08:00
var nodeCoordinate: CLLocationCoordinate2D? {
2021-12-20 22:29:28 -08:00
if latitudeI != 0 && longitudeI != 0 {
2021-12-16 14:13:54 -08:00
let coord = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!)
return coord
} else {
return nil
}
}
2023-03-06 10:33:18 -08:00
2023-01-27 20:22:17 -08:00
var nodeLocation: CLLocation? {
if latitudeI != 0 && longitudeI != 0 {
let location = CLLocation(latitude: latitude!, longitude: longitude!)
return location
} else {
return nil
}
}
2023-03-06 10:33:18 -08:00
2021-12-16 14:13:54 -08:00
var annotaton: MKPointAnnotation {
let pointAnn = MKPointAnnotation()
if nodeCoordinate != nil {
pointAnn.coordinate = nodeCoordinate!
2021-12-16 14:13:54 -08:00
}
return pointAnn
}
}
extension PositionEntity: MKAnnotation {
2025-01-21 09:19:14 -08:00
public var coordinate: CLLocationCoordinate2D { nodeCoordinate ?? LocationsHandler.DefaultLocation }
public var title: String? { nodePosition?.user?.shortName ?? "unknown".localized }
public var subtitle: String? { time?.formatted() }
}