Meshtastic-Apple/Meshtastic/Extensions/CoreData/PositionEntityExtension.swift
Blake McAnally 58da532d32 Extract the generated protobufs into its own Swift package
This change modifies the process for generating and integrating the Meshtastic protobufs into the client application.

* The generated Swift code is now in a local SPM package `MeshtasticProtobufs`
* An Xcode Workspace file `Meshtastic.xcworkspace` was created to more easily manage the new build targets.
* The code generation script for the protos was modified to generate the Swift code into the new location.
* The README.md was updated to reflect these changes.

NOTE: After merging this PR, do not open the project file `Meshtastic.xcodeproj`. You must use the workspace `Meshtastic.xcworkspace`

Extracting out the generated protobuf code into its own library enables several opportunities for the project. This is just a first step, but with some more modularization, a standalone Apple Watch app or other targets starts to become a little bit more achievable to implement.

After extracting the protobufs into a Swift package, I validate these changes by building and running the Meshtastic app to an iPhone 15 Pro Max, and tried changing some settings on a local node. I then messaged back and forth using two local nodes connected to two different iOS devices.
2024-06-28 11:11:01 -05:00

94 lines
2.9 KiB
Swift

//
// PersistenceEntityExtenstion.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 11/28/21.
//
import CoreData
import CoreLocation
import MapKit
import MeshtasticProtobufs
import SwiftUI
extension PositionEntity {
static func allPositionsFetchRequest() -> NSFetchRequest<PositionEntity> {
let request: NSFetchRequest<PositionEntity> = PositionEntity.fetchRequest()
request.fetchLimit = 1000
request.returnsObjectsAsFaults = false
request.includesSubentities = true
request.returnsDistinctResults = true
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
if pointOfInterest.latitude != LocationHelper.DefaultLocation.latitude && pointOfInterest.longitude != LocationHelper.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)
request.predicate = NSCompoundPredicate(type: .and, subpredicates: [positionPredicate, distancePredicate])
} else {
request.predicate = positionPredicate
}
return request
}
var latitude: Double? {
let d = Double(latitudeI)
if d == 0 {
return 0
}
return d / 1e7
}
var longitude: Double? {
let d = Double(longitudeI)
if d == 0 {
return 0
}
return d / 1e7
}
var nodeCoordinate: CLLocationCoordinate2D? {
if latitudeI != 0 && longitudeI != 0 {
let coord = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!)
return coord
} else {
return nil
}
}
var nodeLocation: CLLocation? {
if latitudeI != 0 && longitudeI != 0 {
let location = CLLocation(latitude: latitude!, longitude: longitude!)
return location
} else {
return nil
}
}
var annotaton: MKPointAnnotation {
let pointAnn = MKPointAnnotation()
if nodeCoordinate != nil {
pointAnn.coordinate = nodeCoordinate!
}
return pointAnn
}
}
extension PositionEntity: MKAnnotation {
public var coordinate: CLLocationCoordinate2D { nodeCoordinate ?? LocationHelper.DefaultLocation }
public var title: String? { nodePosition?.user?.shortName ?? "unknown".localized }
public var subtitle: String? { time?.formatted() }
}