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

106 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 {
@MainActor
static func allPositionsFetchRequest() -> NSFetchRequest<PositionEntity> {
2024-03-23 09:01:44 -07:00
let request: NSFetchRequest<PositionEntity> = PositionEntity.fetchRequest()
2024-03-23 18:01:20 -07:00
request.sortDescriptors = [NSSortDescriptor(key: "time", ascending: false)]
let positionPredicate = NSPredicate(format: "nodePosition != nil AND nodePosition.user != nil AND latest == true AND nodePosition.user.shortName != ''")
request.predicate = positionPredicate
// Distance Predicate
if let cl = LocationsHandler.currentLocation {
let d: Double = UserDefaults.meshMapDistance * 1.1
let r: Double = 6371009 // Earth's mean radius in meters
// Calculate Bounding Box
let meanLatitidue = cl.latitude * .pi / 180
let deltaLatitude = d / r * 180 / .pi
let deltaLongitude = d / (r * cos(meanLatitidue)) * 180 / .pi
let minLatitude: Double = cl.latitude - deltaLatitude
let maxLatitude: Double = cl.latitude + deltaLatitude
let minLongitude: Double = cl.longitude - deltaLongitude
let maxLongitude: Double = cl.longitude + deltaLongitude
// Scale bounding box values by 1e7 and use integer attributes (longitudeI, latitudeI)
let scale: Double = 1e7
let minLongitudeI = Int(minLongitude * scale)
let maxLongitudeI = Int(maxLongitude * scale)
let minLatitudeI = Int(minLatitude * scale)
let maxLatitudeI = Int(maxLatitude * scale)
// Use integer comparison in the predicate
let distancePredicate = NSPredicate(format: "(%ld <= longitudeI) AND (longitudeI <= %ld) AND (%ld <= latitudeI) AND (latitudeI <= %ld)",
minLongitudeI, maxLongitudeI, minLatitudeI, maxLatitudeI)
request.predicate = NSCompoundPredicate(type: .and, subpredicates: [positionPredicate, distancePredicate])
}
2024-03-23 09:01:44 -07:00
return request
}
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 }
2025-04-27 16:19:10 -07:00
public var title: String? { nodePosition?.user?.shortName ?? "Unknown".localized }
public var subtitle: String? { time?.formatted() }
}