### GAD-7 Questionnaire Example Source: https://github.com/stanfordspezi/speziquestionnaire/blob/main/Sources/SpeziQuestionnaire/SpeziQuestionnaire.docc/SpeziQuestionnaire.md An example of how to create a SwiftUI view with a button that displays a QuestionnaireSheet for answering the GAD-7 questionnaire. ```swift import SpeziQuestionnaire import SwiftUI struct GAS7QuestionnaireView: View { @State var activeQuestionnaire: Questionnaire? var body: some View { Button("Answer GAD-7") { activeQuestionnaire = .gad7 } .sheet(item: $activeQuestionnaire) { item in QuestionnaireSheet(questionnaire: item) { result in switch result { case .completed(let responses): // ... save the response to your data store case .cancelled: break } } } } } ``` -------------------------------- ### Example Questionnaire View Source: https://github.com/stanfordspezi/speziquestionnaire/blob/main/README.md This example demonstrates how to create a SwiftUI view with a button that displays a sample questionnaire from the FHIRQuestionnaires package using QuestionnaireView. It also shows how to handle the completion of the questionnaire. ```swift import FHIRQuestionnaires import SpeziQuestionnaire import SwiftUI struct ExampleQuestionnaireView: View { @State var displayQuestionnaire = false var body: some View { Button("Display Questionnaire") { displayQuestionnaire.toggle() } .sheet(isPresented: $displayQuestionnaire) { QuestionnaireView( questionnaire: Questionnaire.gcs ) { result in guard case let .completed(response) = result else { return // user cancelled } // ... save the FHIR response to your data store } } } } ``` -------------------------------- ### Annotate Image Question Kind Example Source: https://github.com/stanfordspezi/speziquestionnaire/blob/main/Sources/SpeziQuestionnaireFHIR/SpeziQuestionnaireFHIR.docc/SpeziQuestionnaireFHIR.md Example FHIR R4 JSON definition of a question asking the user to highlight, on a bodymap image, the areas where they feel pain or stiffness. ```json { "linkId": "pain-leg", "text": "In each leg, where do you feel pain?", "type": "attachment", "extension": [ { "url": "http://hl7.org/fhir/StructureDefinition/questionnaire-itemControl", "valueCodeableConcept": { "coding": [ { "system": "http://spezi.stanford.edu/fhir/CodeSystem/questionnaire-item-control", "code": "annotate-image" } ] } }, { "url": "http://spezi.stanford.edu/fhir/CodeSystem/questionnaire-item-control/annotate-image/input-image", "valueString": "bodymap.png" }, { "url": "http://spezi.stanford.edu/fhir/CodeSystem/questionnaire-item-control/annotate-image/region", "extension": [ { "url": "label", "valueString": "Pain" }, { "url": "color", "valueString": "red" } ] }, { "url": "http://spezi.stanford.edu/fhir/CodeSystem/questionnaire-item-control/annotate-image/region", "extension": [ { "url": "label", "valueString": "Stiffness" }, { "url": "color", "valueString": "blue" } ] } ] } ``` -------------------------------- ### Acknowledge Disclaimer Question Kind Example Source: https://github.com/stanfordspezi/speziquestionnaire/blob/main/Sources/SpeziQuestionnaire/SpeziQuestionnaire.docc/QuestionKinds.md An example of a custom Swift struct conforming to `QuestionKindDefinition` to create an 'acknowledge disclaimer' question kind. It includes configuration for disclaimer text and button title, a `makeView` function to render the UI with a `Toggle`, and a `validate` function to ensure the user agrees. ```swift /// A question kind that asks the user to consent to a statement. struct AcknowledgeDisclaimerQuestionKind: QuestionKindDefinition { // The config is what allows each instantiation of the question kind to customize the question's input. // In this case, it allows us to specify the question's consent text, and the title of the "I Agree" button. struct Config: QuestionKindConfig { let disclaimerText: String let consentButtonTitle: String } // Creates the view used to display an Acknowledge Disclaimer question within a `QuestionnaireSheet`. // In this case, we simply display the text we want the user to acknowledge, and a `Toggle` collecting the user response. static func makeView(for task: Questionnaire.Task, using config: Config, response: Binding) -> some View { Text(config.disclaimerText) Toggle(config.consentButtonTitle, isOn: Binding { response.value.boolValue.wrappedValue ?? false } set: { newValue in response.value.boolValue.wrappedValue = newValue }) .bold() .onChange(of: response.value.wrappedValue == .none, initial: true) { _, newValue in if newValue { response.value.boolValue.wrappedValue = false } } } // We can also customize the question kind's response validation logic. // In this case, we require that the user consent to the disclaimer, so we reject all non-yes responses. // This effectively prevents the user from completing the questionnaire until they have consented to the disclaimer, // and the UI displays our validation message next to the question, explaining what's going on. static func validate(response: QuestionnaireResponses.Response, for config: Config) -> QuestionnaireResponses.ResponseValidationResult { switch response.value.boolValue { case true: .ok case false, nil: .invalid(message: "Must agree in order to continue in questionnaire") } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.