mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
After this change, a developer can now clone the project and run without the build failing due to lint errors! 😃
* I ran `swiftlint --fix` to resolve many auto-correctable issues (mostly whitespace)
* Excluded the `Meshtastic/Protobufs` directory from lint, since that code is automatically generated.
* Converted some single letter method parameters to lowercase.
* Converted several instances `force_cast` to instead use `guard` or `if let` to unwrap optional values. During this change, some of the SwiftUI views became "too complex to be solved in a reasonable time", so I broke up the views into distinct sub-expressions.
I was able to build and run the app on an iOS simulator.
101 lines
3 KiB
Swift
101 lines
3 KiB
Swift
//
|
|
// QueryCoreData.swift
|
|
// Meshtastic
|
|
//
|
|
// Created(c) Garth Vander Houwen 1/16/23.
|
|
//
|
|
|
|
import CoreData
|
|
|
|
public func getNodeInfo(id: Int64, context: NSManagedObjectContext) -> NodeInfoEntity? {
|
|
|
|
let fetchNodeInfoRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "NodeInfoEntity")
|
|
fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(id))
|
|
|
|
do {
|
|
guard let fetchedNode = try context.fetch(fetchNodeInfoRequest) as? [NodeInfoEntity] else {
|
|
return nil
|
|
}
|
|
if fetchedNode.count == 1 {
|
|
return fetchedNode[0]
|
|
}
|
|
} catch {
|
|
return nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
public func getStoreAndForwardMessageIds(seconds: Int, context: NSManagedObjectContext) -> [UInt32] {
|
|
|
|
let time = seconds * -1
|
|
let fetchMessagesRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "MessageEntity")
|
|
let timeRange = Calendar.current.date(byAdding: .minute, value: time, to: Date())
|
|
let milleseconds = Int32(timeRange?.timeIntervalSince1970 ?? 0)
|
|
fetchMessagesRequest.predicate = NSPredicate(format: "receivedTimestamp >= %d", milleseconds)
|
|
|
|
do {
|
|
guard let fetchedMessages = try context.fetch(fetchMessagesRequest) as? [MessageEntity] else {
|
|
return []
|
|
}
|
|
if fetchedMessages.count == 1 {
|
|
return fetchedMessages.map { UInt32($0.messageId) }
|
|
}
|
|
} catch {
|
|
return []
|
|
}
|
|
return []
|
|
}
|
|
|
|
public func getTraceRoute(id: Int64, context: NSManagedObjectContext) -> TraceRouteEntity? {
|
|
|
|
let fetchTraceRouteRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "TraceRouteEntity")
|
|
fetchTraceRouteRequest.predicate = NSPredicate(format: "id == %lld", Int64(id))
|
|
|
|
do {
|
|
guard let fetchedTraceRoute = try context.fetch(fetchTraceRouteRequest) as? [TraceRouteEntity] else {
|
|
return nil
|
|
}
|
|
if fetchedTraceRoute.count == 1 {
|
|
return fetchedTraceRoute[0]
|
|
}
|
|
} catch {
|
|
return nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
public func getUser(id: Int64, context: NSManagedObjectContext) -> UserEntity {
|
|
|
|
let fetchUserRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserEntity")
|
|
fetchUserRequest.predicate = NSPredicate(format: "num == %lld", Int64(id))
|
|
|
|
do {
|
|
guard let fetchedUser = try context.fetch(fetchUserRequest) as? [UserEntity] else {
|
|
return UserEntity(context: context)
|
|
}
|
|
if fetchedUser.count == 1 {
|
|
return fetchedUser[0]
|
|
}
|
|
} catch {
|
|
return UserEntity(context: context)
|
|
}
|
|
return UserEntity(context: context)
|
|
}
|
|
|
|
public func getWaypoint(id: Int64, context: NSManagedObjectContext) -> WaypointEntity {
|
|
|
|
let fetchWaypointRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "WaypointEntity")
|
|
fetchWaypointRequest.predicate = NSPredicate(format: "id == %lld", Int64(id))
|
|
|
|
do {
|
|
guard let fetchedWaypoint = try context.fetch(fetchWaypointRequest) as? [WaypointEntity] else {
|
|
return WaypointEntity(context: context)
|
|
}
|
|
if fetchedWaypoint.count == 1 {
|
|
return fetchedWaypoint[0]
|
|
}
|
|
} catch {
|
|
return WaypointEntity(context: context)
|
|
}
|
|
return WaypointEntity(context: context)
|
|
}
|