2023-03-26 09:08:08 -07:00
|
|
|
//
|
|
|
|
|
// 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 SwiftUI
|
|
|
|
|
|
|
|
|
|
extension PositionEntity {
|
2024-03-23 09:01:44 -07:00
|
|
|
|
|
|
|
|
static func allPositionsFetchRequest() -> NSFetchRequest<PositionEntity> {
|
|
|
|
|
let request: NSFetchRequest<PositionEntity> = PositionEntity.fetchRequest()
|
2024-03-23 18:22:10 -07:00
|
|
|
request.fetchLimit = 50
|
2024-03-23 18:01:20 -07:00
|
|
|
//request.fetchBatchSize = 1
|
|
|
|
|
//request.returnsObjectsAsFaults = true
|
2024-03-23 09:01:44 -07:00
|
|
|
//request.includesSubentities = false
|
|
|
|
|
request.returnsDistinctResults = true
|
2024-03-23 18:01:20 -07:00
|
|
|
request.sortDescriptors = [NSSortDescriptor(key: "time", ascending: false)]
|
2024-03-23 18:22:10 -07:00
|
|
|
request.predicate = NSPredicate(format: "nodePosition != nil && latest == true && time >= %@", Calendar.current.date(byAdding: .day, value: -2, to: Date())! as NSDate)
|
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
|
|
|
|
2023-01-10 06:49:19 -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()
|
2023-01-10 06:49:19 -08:00
|
|
|
if nodeCoordinate != nil {
|
|
|
|
|
pointAnn.coordinate = nodeCoordinate!
|
2021-12-16 14:13:54 -08:00
|
|
|
}
|
|
|
|
|
return pointAnn
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-10 06:49:19 -08:00
|
|
|
|
|
|
|
|
extension PositionEntity: MKAnnotation {
|
2023-01-12 10:51:51 -08:00
|
|
|
public var coordinate: CLLocationCoordinate2D { nodeCoordinate ?? LocationHelper.DefaultLocation }
|
2023-05-05 09:27:24 -07:00
|
|
|
public var title: String? { nodePosition?.user?.shortName ?? "unknown".localized }
|
2023-01-11 13:53:50 -08:00
|
|
|
public var subtitle: String? { time?.formatted() }
|
2023-01-10 06:49:19 -08:00
|
|
|
}
|