diff --git a/PlantGuide/Presentation/Scenes/Identification/IdentificationView.swift b/PlantGuide/Presentation/Scenes/Identification/IdentificationView.swift index 5e1c2a6..2bb6194 100644 --- a/PlantGuide/Presentation/Scenes/Identification/IdentificationView.swift +++ b/PlantGuide/Presentation/Scenes/Identification/IdentificationView.swift @@ -19,6 +19,11 @@ struct IdentificationView: View { /// Tracks whether we've announced results to avoid duplicate announcements @State private var hasAnnouncedResults = false + /// Local state to reliably drive the save button's enabled/disabled state. + /// Works around an @Observable + @State tracking issue where computed + /// properties in view modifiers don't always trigger re-renders. + @State private var saveEnabled = false + // MARK: - Scaled Metrics for Dynamic Type @ScaledMetric(relativeTo: .body) private var closeIconSize: CGFloat = 16 @@ -76,8 +81,10 @@ struct IdentificationView: View { announceStateChange(from: oldValue, to: newValue) } .onChange(of: viewModel.selectedPrediction?.id) { _, _ in - // Force view update when selection changes - // This ensures SwiftUI tracks @Observable property changes correctly + saveEnabled = viewModel.canSaveToCollection + } + .onChange(of: viewModel.saveState) { _, _ in + saveEnabled = viewModel.canSaveToCollection } .accessibilityIdentifier(AccessibilityIdentifiers.Identification.identificationView) .alert("Plant Saved!", isPresented: .init( @@ -406,12 +413,12 @@ struct IdentificationView: View { .padding(.vertical, 14) .background( RoundedRectangle(cornerRadius: 12) - .fill(viewModel.canSaveToCollection ? Color.accentColor : Color.gray) + .fill(saveEnabled ? Color.accentColor : Color.gray) ) } - .disabled(!viewModel.canSaveToCollection) + .disabled(!saveEnabled) .accessibilityLabel(viewModel.saveState == .saving ? "Saving plant" : "Save to Collection") - .accessibilityHint(viewModel.canSaveToCollection ? "Saves the selected plant to your collection" : "Select a plant first") + .accessibilityHint(saveEnabled ? "Saves the selected plant to your collection" : "Select a plant first") .accessibilityIdentifier(AccessibilityIdentifiers.Identification.saveToCollectionButton) } .padding(.horizontal, 20)