From bd04a27b525983ba5ac9d3ab6072400ebe529921 Mon Sep 17 00:00:00 2001 From: Andreas Bauer Date: Fri, 3 Nov 2023 15:57:50 -0700 Subject: [PATCH] Minor adjustments based on the evaluation within SpeziAccount --- .../ReceiveValidationModifier.swift | 6 ++++++ .../ValidationState/ValidationContext.swift | 16 +++++++++++++--- .../ValidationTests/ValidationControls.swift | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Sources/SpeziValidation/ValidationState/ReceiveValidationModifier.swift b/Sources/SpeziValidation/ValidationState/ReceiveValidationModifier.swift index dc3fc7b..5f5d129 100644 --- a/Sources/SpeziValidation/ValidationState/ReceiveValidationModifier.swift +++ b/Sources/SpeziValidation/ValidationState/ReceiveValidationModifier.swift @@ -27,6 +27,12 @@ extension View { } } + public func receiveValidation(in state: ValidationState.Binding) -> some View { + onPreferenceChange(CapturedValidationStateKey.self) { entries in + state.wrappedValue = ValidationContext(entries: entries) + } + } + public func receiveValidation(in state: ValidationState.Binding, focus: FocusState.Binding) -> some View { onPreferenceChange(CapturedValidationStateKey.self) { entries in state.wrappedValue = ValidationContext(entries: entries, focus: focus) diff --git a/Sources/SpeziValidation/ValidationState/ValidationContext.swift b/Sources/SpeziValidation/ValidationState/ValidationContext.swift index de0399d..5da3ebf 100644 --- a/Sources/SpeziValidation/ValidationState/ValidationContext.swift +++ b/Sources/SpeziValidation/ValidationState/ValidationContext.swift @@ -23,7 +23,7 @@ public struct ValidationContext { /// /// Please refer to the documentation of ``ValidationEngine/inputValid``. @MainActor - public var inputValid: Bool { + public var allInputValid: Bool { entries.allSatisfy { $0.inputValid } } @@ -46,6 +46,14 @@ public struct ValidationContext { result.append(contentsOf: state.displayedValidationResults) } } + + /// Flag that indicates if ``displayedValidationResults`` returns any ``FailedValidationResult``. + /// + /// Please refer to the documentation of ``ValidationEngine/isDisplayingValidationErrors``. + @MainActor + public var isDisplayingValidationErrors: Bool { + entries.contains { $0.isDisplayingValidationErrors } + } init() { @@ -76,15 +84,17 @@ public struct ValidationContext { /// If provided, the `FocusState` will be automatically set to the first field /// that reported a failed validation result. /// + /// - Parameter switchFocus: Flag that controls the automatic focus switching mechanisms. Default turned on. /// - Returns: Returns `true` if all subviews reported valid data. Returns `false` if at least one /// subview reported invalid data. @MainActor @discardableResult - public func validateSubviews() -> Bool { + public func validateSubviews(switchFocus: Bool = true) -> Bool { let failedFields = collectFailedFields() if let first = failedFields.first { - if let focusState, + if switchFocus, + let focusState, let field = first.field { focusState.wrappedValue = field } diff --git a/Tests/UITests/TestApp/ValidationTests/ValidationControls.swift b/Tests/UITests/TestApp/ValidationTests/ValidationControls.swift index cec7dbc..dfcce7d 100644 --- a/Tests/UITests/TestApp/ValidationTests/ValidationControls.swift +++ b/Tests/UITests/TestApp/ValidationTests/ValidationControls.swift @@ -17,7 +17,7 @@ struct ValidationControls: View { var body: some View { Text("Has Engines: \(!validation.isEmpty ? "Yes" : "No")") - Text("Input Valid: \(validation.inputValid ? "Yes" : "No")") + Text("Input Valid: \(validation.allInputValid ? "Yes" : "No")") if let lastValid { Text("Last state: \(lastValid ? "valid" : "invalid")") }