From 3643fb1828f443574d10a276ce3d41ad0b79438a Mon Sep 17 00:00:00 2001 From: Nguyen Ngo Date: Mon, 8 Jun 2026 17:08:27 -0400 Subject: [PATCH] 06/08 Fixed issue that inspection doesn't show photo after flagging the issue --- .../Dashboard/ExecuteInspectionView.swift | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/JanitorialQC/Views/Dashboard/ExecuteInspectionView.swift b/JanitorialQC/Views/Dashboard/ExecuteInspectionView.swift index fdc82b2..95e40d8 100644 --- a/JanitorialQC/Views/Dashboard/ExecuteInspectionView.swift +++ b/JanitorialQC/Views/Dashboard/ExecuteInspectionView.swift @@ -321,8 +321,20 @@ struct ExecuteInspectionView: View { guard inspection.status == "draft" else { return } if force { isSaving = true } + // Preserve server paths already written by processPhotoQueue — same + // logic as submitInspection(). Auto-save fires every 30 s and would + // overwrite uploads/... with local://... if it runs after a sync that + // was triggered by FlagIssueView uploading the inspection's photos. + let existingFormData = inspection.formData var data: [String: Any] = [:] - for (k, v) in formValues { data[k] = v } + for (k, v) in formValues { + if let s = v as? String, s.hasPrefix("local://"), + let saved = existingFormData[k] as? String, saved.hasPrefix("uploads/") { + data[k] = saved + } else { + data[k] = v + } + } inspection.formData = data inspection.lastModifiedAt = Date() try? context.save() @@ -339,9 +351,27 @@ struct ExecuteInspectionView: View { private func submitInspection() async { isSubmitting = true - // Persist final form data + // Persist final form data. + // IMPORTANT: formValues is an in-memory SwiftUI state dict that is NOT + // updated when processPhotoQueue writes server paths back into + // inspection.formData (e.g. during the sync triggered by FlagIssueView). + // For any field whose formValues entry is still "local://..." (the photo + // hasn't been uploaded yet by THIS submit's sync pass), check whether + // processPhotoQueue already wrote a real server path into inspection.formData + // for that field. If so, preserve it — otherwise the server path gets + // overwritten with "local://..." here and sanitised to "" in APIClient, + // making photos disappear on inspections that also flagged an issue. + let existingFormData = inspection.formData var data: [String: Any] = [:] - for (k, v) in formValues { data[k] = v } + for (k, v) in formValues { + if let s = v as? String, s.hasPrefix("local://"), + let saved = existingFormData[k] as? String, saved.hasPrefix("uploads/") { + // processPhotoQueue already uploaded this photo — keep the server path + data[k] = saved + } else { + data[k] = v + } + } inspection.formData = data inspection.overallScore = inspection.computeScore(fromSchema: formSchema) inspection.status = "completed"