Meshtastic-Apple/Meshtastic/Persistence/QueryCoreData.swift

67 lines
1.8 KiB
Swift
Raw Normal View History

2023-01-16 17:40:28 -08:00
//
// QueryCoreData.swift
// Meshtastic
//
// Created(c) Garth Vander Houwen 1/16/23.
//
2026-04-16 12:10:00 -07:00
import Foundation
import SwiftData
func getNodeInfo(id: Int64, context: ModelContext) -> NodeInfoEntity? {
let num = id
var descriptor = FetchDescriptor<NodeInfoEntity>(
predicate: #Predicate { $0.num == num }
)
descriptor.fetchLimit = 1
return try? context.fetch(descriptor).first
2023-01-31 10:50:17 -08:00
}
2026-04-16 12:10:00 -07:00
func getStoreAndForwardMessageIds(seconds: Int, context: ModelContext) -> [UInt32] {
2023-10-01 12:25:38 -07:00
let time = seconds * -1
let timeRange = Calendar.current.date(byAdding: .minute, value: time, to: Date())
let milleseconds = Int32(timeRange?.timeIntervalSince1970 ?? 0)
2026-04-16 12:10:00 -07:00
let descriptor = FetchDescriptor<MessageEntity>(
predicate: #Predicate { $0.messageTimestamp >= milleseconds }
)
let fetchedMessages = (try? context.fetch(descriptor)) ?? []
return fetchedMessages.map { UInt32($0.messageId) }
2023-10-01 12:25:38 -07:00
}
2026-04-16 12:10:00 -07:00
func getTraceRoute(id: Int64, context: ModelContext) -> TraceRouteEntity? {
let traceId = id
var descriptor = FetchDescriptor<TraceRouteEntity>(
predicate: #Predicate { $0.id == traceId }
)
descriptor.fetchLimit = 1
return try? context.fetch(descriptor).first
2023-12-08 11:41:29 -08:00
}
2023-10-01 12:25:38 -07:00
2026-04-16 12:10:00 -07:00
func getUser(id: Int64, context: ModelContext) -> UserEntity {
let userNum = id
let descriptor = FetchDescriptor<UserEntity>(
predicate: #Predicate { $0.num == userNum }
)
if let existing = try? context.fetch(descriptor).first {
return existing
2023-01-31 10:50:17 -08:00
}
2026-04-16 12:10:00 -07:00
let newUser = UserEntity()
newUser.num = id
context.insert(newUser)
return newUser
2023-01-31 10:50:17 -08:00
}
2026-04-16 12:10:00 -07:00
func getWaypoint(id: Int64, context: ModelContext) -> WaypointEntity {
let waypointId = id
let descriptor = FetchDescriptor<WaypointEntity>(
predicate: #Predicate { $0.id == waypointId }
)
if let existing = try? context.fetch(descriptor).first {
return existing
2023-01-16 17:40:28 -08:00
}
2026-04-16 12:10:00 -07:00
let newWaypoint = WaypointEntity()
newWaypoint.id = id
context.insert(newWaypoint)
return newWaypoint
2023-01-16 17:40:28 -08:00
}