05/02 Phase C 2

This commit is contained in:
Nguyen Ngo
2026-05-03 09:29:12 -04:00
parent 59e698b5f7
commit 51d2923e6d
6 changed files with 439 additions and 309 deletions
+15 -16
View File
@@ -1,7 +1,7 @@
// Models/LocalTemplate.swift
// --------------------------
// SwiftData model for locally cached inspection templates.
// The form_schema is stored as a raw JSON string and decoded on demand.
// form_schema stored as raw JSON string, decoded on demand via formSchema property.
import Foundation
import SwiftData
@@ -12,17 +12,16 @@ final class LocalTemplate {
var name: String
var templateDescription: String
var frequency: String
/// Raw JSON string of the form_schema array decoded on demand
var formSchemaJSON: String
var lastSyncedAt: Date
init(from summary: APITemplateSummary) {
self.serverId = summary.id
self.name = summary.name
self.templateDescription = summary.description
self.frequency = summary.frequency
self.formSchemaJSON = "[]"
self.lastSyncedAt = Date()
self.serverId = summary.id
self.name = summary.name
self.templateDescription = summary.description
self.frequency = summary.frequency
self.formSchemaJSON = "[]"
self.lastSyncedAt = Date()
}
func updateSummary(from summary: APITemplateSummary) {
@@ -33,20 +32,20 @@ final class LocalTemplate {
}
func updateSchema(from template: APITemplate) {
// Re-serialize the form_schema to JSON for local storage
if let data = try? JSONSerialization.data(withJSONObject: template.formSchema.map({ dict in
dict.mapValues { $0.value }
})),
let str = String(data: data, encoding: .utf8) {
// Convert [[String: JSONValue]] JSON string via anyValue bridge
let raw = template.formSchema.map { dict in
dict.mapValues { $0.anyValue }
}
if let data = try? JSONSerialization.data(withJSONObject: raw),
let str = String(data: data, encoding: .utf8) {
self.formSchemaJSON = str
}
self.lastSyncedAt = Date()
}
/// Decode the stored JSON string back into an array of field dictionaries.
/// Returns an empty array if the JSON is invalid.
/// Decode stored JSON back into [[String: Any]] for the form renderer
var formSchema: [[String: Any]] {
guard let data = formSchemaJSON.data(using: .utf8),
guard let data = formSchemaJSON.data(using: .utf8),
let array = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]]
else { return [] }
return array