06/18 Update Inspection details view layout
This commit is contained in:
@@ -418,12 +418,16 @@ class SyncManager: ObservableObject {
|
||||
}
|
||||
|
||||
for apiFacility in uniqueFacilities {
|
||||
let localFacility: LocalFacility
|
||||
if let existing = facilityMap[apiFacility.id] {
|
||||
existing.update(from: apiFacility)
|
||||
localFacility = existing
|
||||
} else {
|
||||
context.insert(LocalFacility(from: apiFacility))
|
||||
let newFacility = LocalFacility(from: apiFacility)
|
||||
context.insert(newFacility)
|
||||
localFacility = newFacility
|
||||
}
|
||||
try await upsertAreas(for: apiFacility.id, context: context)
|
||||
try await upsertAreas(for: apiFacility.id, facility: localFacility, context: context)
|
||||
}
|
||||
|
||||
let existingTemplates = try context.fetch(FetchDescriptor<LocalTemplate>())
|
||||
@@ -443,6 +447,17 @@ class SyncManager: ObservableObject {
|
||||
)
|
||||
}
|
||||
|
||||
// Delete any cached templates the server no longer returns.
|
||||
// The server endpoint now only returns active templates, so any
|
||||
// locally cached template not in the response was deactivated.
|
||||
// Deleting ensures they never appear in the picker even offline.
|
||||
let returnedTemplateIds = Set(templatesData.templates.map { $0.id })
|
||||
for existing in existingTemplates {
|
||||
if !returnedTemplateIds.contains(existing.serverId) {
|
||||
context.delete(existing)
|
||||
}
|
||||
}
|
||||
|
||||
try context.save()
|
||||
|
||||
} catch APIError.notAuthenticated {
|
||||
@@ -464,7 +479,7 @@ class SyncManager: ObservableObject {
|
||||
|
||||
// ── Private Helpers ───────────────────────────────────────────────────
|
||||
|
||||
private func upsertAreas(for facilityId: Int, context: ModelContext) async throws {
|
||||
private func upsertAreas(for facilityId: Int, facility: LocalFacility, context: ModelContext) async throws {
|
||||
let areasData: AreasResponseData = try await APIClient.shared.request(
|
||||
"/api/v1/facilities/\(facilityId)/areas"
|
||||
)
|
||||
@@ -473,8 +488,18 @@ class SyncManager: ObservableObject {
|
||||
let areaMap = Dictionary(existing.map { ($0.serverId, $0) },
|
||||
uniquingKeysWith: { a, _ in a })
|
||||
for apiArea in areasData.areas {
|
||||
if let ex = areaMap[apiArea.id] { ex.update(from: apiArea) }
|
||||
else { context.insert(LocalArea(from: apiArea)) }
|
||||
if let ex = areaMap[apiArea.id] {
|
||||
ex.update(from: apiArea)
|
||||
// Re-wire relationship in case it was lost (e.g. cache clear)
|
||||
if ex.facility == nil { ex.facility = facility }
|
||||
} else {
|
||||
let newArea = LocalArea(from: apiArea)
|
||||
// Wire the inverse relationship so LocalFacility.areas is populated.
|
||||
// Without this assignment SwiftData never links the area into the
|
||||
// facility's areas array and selectedFacility?.areas returns [].
|
||||
newArea.facility = facility
|
||||
context.insert(newArea)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user