06/23 Fix Medium//Low-impact issues
This commit is contained in:
@@ -32,16 +32,60 @@ final class LocalIssue {
|
||||
private static let jsonDecoder = JSONDecoder()
|
||||
private static let jsonEncoder = JSONEncoder()
|
||||
|
||||
// Lightweight decode cache — avoids re-parsing identical JSON strings.
|
||||
// SwiftData may call the getter multiple times per render pass (once for
|
||||
// isEmpty, once for count, once for ForEach). Caching the last-decoded
|
||||
// value by JSON string identity means the JSON parse only happens when
|
||||
// the underlying data actually changes.
|
||||
// @Transient tells SwiftData not to persist these — they're in-memory only.
|
||||
// Lightweight decode cache — split into key+value pairs because SwiftData's
|
||||
// @Transient macro does not support tuple types. Two separate @Transient
|
||||
// properties per cache entry achieve the same result with no schema impact.
|
||||
@Transient private var _cachedLocalKey: String = ""
|
||||
@Transient private var _cachedLocalValue: [String] = []
|
||||
@Transient private var _cachedServerKey: String = ""
|
||||
@Transient private var _cachedServerValue: [String] = []
|
||||
|
||||
/// Decoded local photo paths (up to 5)
|
||||
var photoLocalPaths: [String] {
|
||||
get { (try? Self.jsonDecoder.decode([String].self, from: Data(photoLocalPathsJSON.utf8))) ?? [] }
|
||||
set { photoLocalPathsJSON = (try? String(data: Self.jsonEncoder.encode(newValue), encoding: .utf8)) ?? "[]" }
|
||||
get {
|
||||
if _cachedLocalKey == photoLocalPathsJSON, !_cachedLocalKey.isEmpty {
|
||||
return _cachedLocalValue
|
||||
}
|
||||
let decoded = (try? Self.jsonDecoder.decode([String].self,
|
||||
from: Data(photoLocalPathsJSON.utf8))) ?? []
|
||||
_cachedLocalKey = photoLocalPathsJSON
|
||||
_cachedLocalValue = decoded
|
||||
return decoded
|
||||
}
|
||||
set {
|
||||
let encoded = (try? String(data: Self.jsonEncoder.encode(newValue),
|
||||
encoding: .utf8)) ?? "[]"
|
||||
photoLocalPathsJSON = encoded
|
||||
_cachedLocalKey = encoded
|
||||
_cachedLocalValue = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// Decoded server photo paths
|
||||
var photoServerPaths: [String] {
|
||||
get { (try? Self.jsonDecoder.decode([String].self, from: Data(photoServerPathsJSON.utf8))) ?? [] }
|
||||
set { photoServerPathsJSON = (try? String(data: Self.jsonEncoder.encode(newValue), encoding: .utf8)) ?? "[]" }
|
||||
get {
|
||||
if _cachedServerKey == photoServerPathsJSON, !_cachedServerKey.isEmpty {
|
||||
return _cachedServerValue
|
||||
}
|
||||
let decoded = (try? Self.jsonDecoder.decode([String].self,
|
||||
from: Data(photoServerPathsJSON.utf8))) ?? []
|
||||
_cachedServerKey = photoServerPathsJSON
|
||||
_cachedServerValue = decoded
|
||||
return decoded
|
||||
}
|
||||
set {
|
||||
let encoded = (try? String(data: Self.jsonEncoder.encode(newValue),
|
||||
encoding: .utf8)) ?? "[]"
|
||||
photoServerPathsJSON = encoded
|
||||
_cachedServerKey = encoded
|
||||
_cachedServerValue = newValue
|
||||
}
|
||||
}
|
||||
|
||||
var createdAt: Date
|
||||
|
||||
@@ -15,6 +15,10 @@ final class LocalTemplate {
|
||||
var formSchemaJSON: String
|
||||
var lastSyncedAt: Date
|
||||
var isActive: Bool = true // phase21 — false templates excluded from picker
|
||||
/// Timestamp of the last successful schema fetch (GET /api/v1/templates/{id}).
|
||||
/// Nil when the schema has never been fetched (e.g. template just inserted).
|
||||
/// Used to skip redundant detail calls when summary fields are unchanged.
|
||||
var schemaFetchedAt: Date? = nil
|
||||
|
||||
init(from summary: APITemplateSummary) {
|
||||
self.serverId = summary.id
|
||||
@@ -26,12 +30,20 @@ final class LocalTemplate {
|
||||
self.isActive = summary.isActive
|
||||
}
|
||||
|
||||
func updateSummary(from summary: APITemplateSummary) {
|
||||
/// Update summary fields and return whether any field changed.
|
||||
/// Used by pullReferenceData to skip schema re-fetching when nothing changed.
|
||||
@discardableResult
|
||||
func updateSummary(from summary: APITemplateSummary) -> Bool {
|
||||
let changed = name != summary.name
|
||||
|| templateDescription != summary.description
|
||||
|| frequency != summary.frequency
|
||||
|| isActive != summary.isActive
|
||||
self.name = summary.name
|
||||
self.templateDescription = summary.description
|
||||
self.frequency = summary.frequency
|
||||
self.isActive = summary.isActive
|
||||
self.lastSyncedAt = Date()
|
||||
return changed
|
||||
}
|
||||
|
||||
func updateSchema(from template: APITemplate) {
|
||||
@@ -43,7 +55,8 @@ final class LocalTemplate {
|
||||
let str = String(data: data, encoding: .utf8) {
|
||||
self.formSchemaJSON = str
|
||||
}
|
||||
self.lastSyncedAt = Date()
|
||||
self.schemaFetchedAt = Date()
|
||||
self.lastSyncedAt = Date()
|
||||
}
|
||||
|
||||
/// Decode stored JSON back into [[String: Any]] for the form renderer
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
// Models/SyncQueueEntry.swift
|
||||
// ---------------------------
|
||||
// SwiftData model for the outbox sync queue.
|
||||
// Every offline write (inspection, issue, photo) enqueues an entry here.
|
||||
// SyncManager processes entries in FIFO order when connectivity is restored.
|
||||
// LEGACY — This model is no longer used. The original design enqueued every
|
||||
// offline write here and processed in FIFO order; the current architecture uses
|
||||
// LocalInspection.syncStatus / LocalIssue.syncStatus / PendingPhoto.uploadStatus
|
||||
// directly (simpler, fewer moving parts, no double-bookkeeping).
|
||||
//
|
||||
// The model is kept registered in the SwiftData container solely to maintain
|
||||
// schema compatibility with existing installs — removing it from modelContainer
|
||||
// would trigger a migration failure on devices that already have the table.
|
||||
// A future dedicated migration (phase N) can drop the table explicitly using
|
||||
// op.execute("DROP TABLE IF EXISTS SyncQueueEntry") once it's safe to do so.
|
||||
//
|
||||
// DO NOT add new code that reads or writes this model.
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
Reference in New Issue
Block a user