Skip to content

feat: Add card to display ResearchKitSwiftUI surveys - #41

Merged
cbaker6 merged 8 commits into
mainfrom
researchKitSwiftUI
Apr 14, 2025
Merged

cbaker6 merged 8 commits into
mainfrom
researchKitSwiftUI

Conversation

@cbaker6

@cbaker6 cbaker6 commented Apr 14, 2025 •

Copy link
Copy Markdown
Member

Adds support for ResearchSurveyView which is a CareKit card that supports presenting ResearchKitSwiftUI surveys and convert survey results to OCKOutcome's for OCKAnyEvents.

  • Add new views ResearchSurveyView (CareKit Card) and ResearchCareForm (wrapping view that wraps a ResearchForm and provides functionality to convert and save any ResearchFormResult for a OCKAnyEvent
  • Add new models SurveyStep and SurveyQuestion along with new OCKTask property called var surveySteps: [SurveyStep] which allows survey steps to be added to OCKTask's
image image

An example use case is below:

// Setup Survey as part of OCKTask
let textChoiceYesText = String(localized: "ANSWER_YES")
let textChoiceNoText = String(localized: "ANSWER_NO")
let yesValue = "Yes"
let noValue = "No"
		let choices: [TextChoice] = [
			.init(
				id: "\(qualityOfLifeTaskId)_0",
				choiceText: textChoiceYesText,
				value: yesValue
			),
			.init(
				id: "\(qualityOfLifeTaskId)_1",
				choiceText: textChoiceNoText,
				value: noValue
			)

		]
		let questionOne = SurveyQuestion(
			id: "\(qualityOfLifeTaskId)-managing-time",
			type: .multipleChoice,
			required: true,
			title: String(localized: "QUALITY_OF_LIFE_TIME"),
			textChoices: choices,
			choiceSelectionLimit: .single
		)
		let questionTwo = SurveyQuestion(
			id: qualityOfLifeTaskId,
			type: .slider,
			required: false,
			title: String(localized: "QUALITY_OF_LIFE_STRESS"),
			detail: String(localized: "QUALITY_OF_LIFE_STRESS_DETAIL"),
			integerRange: 0...10,
			sliderStepValue: 1
		)
		let questions = [questionOne, questionTwo]
                let taskAsset = ""
                let taskTitle = String(localized: "QUALITY_OF_LIFE")
                let taskAsset = "brain.head.profile"
		let stepOne = SurveyStep(
			id: "\(qualityOfLifeTaskId)-step-1",
			questions: questions,
                        asset: taskAsset,
                        title: taskTitle,
                        subtitle: String(localized: "ANSWER_HONESTLY")
		)
		var qualityOfLife = OCKTask(
			id: "\(qualityOfLifeTaskId)-stress",
			title: taskTitle,
			carePlanUUID: carePlanUUID,
			schedule: qualityOfLifeSchedule
		)
		qualityOfLife.impactsAdherence = true
		qualityOfLife.asset = taskAsset
		qualityOfLife.surveySteps = [stepOne]
// Persist this task

Now setup your SwiftUI view

import CareKit
import CareKitEssentials
import CareKitStore
import CareKitUI
import ResearchKitSwiftUI
import SwiftUI

struct CareView: View {
   @Environment(\.tintColor) var tintColor
   @CareStoreFetchRequest(query: query()) private var events

   var body: some View {
        ScrollView {
          ForEach(events) { event in
            if let steps = event.task.surveySteps {
              // A ResearchKitSwiftUI survey task, prepare survey
              ResearchSurveyView(event: event) {
                ResearchCareForm(
                  event: eventResult
                ) {
                  ForEach(steps) { step in	
                    ResearchFormStep(
                      image: step.image,
                      title: step.title,		
                      subtitle: step.subtitle	
                    ) {				
                      ForEach(step.questions) { question in				
                        question.view()	
                      }		
                      .accentColor(tintColor)
                    }
                  }
                }
              }
              .accentColor(tintColor)
            } else {
              // Some other task that's not a survey
              // setup view as you typically would...
            }
          }
        }
   }

   static var query: OCKEventQuery {
        let query = OCKEventQuery(for: Date())
        return query
   }
}

@codecov

codecov Bot commented Apr 14, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 0% with 85 lines in your changes missing coverage. Please review.

Project coverage is 8.54%. Comparing base (c5260e5) to head (1f56147).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...Shared/ResearchSurveyView/ResearchSurveyView.swift 0.00% 78 Missing ⚠️
...ssentials/Cards/Shared/InformationHeaderView.swift 0.00% 7 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main     #41      +/-   ##
========================================
- Coverage   8.76%   8.54%   -0.23%     
========================================
  Files         71      72       +1     
  Lines       3172    3255      +83     
========================================
  Hits         278     278              
- Misses      2894    2977      +83     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cbaker6
cbaker6 merged commit 2a4cb23 into main Apr 14, 2025
@cbaker6
cbaker6 deleted the researchKitSwiftUI branch April 14, 2025 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant