generated from StanfordSpezi/SpeziTemplateApplication
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added reaction test to assessment (#46)
# Added ReactionTest to Assessment Page ## ♻️ Current situation & Problem Contributed another assessment to the assessment page. ## ⚙️ Release Notes Added ReactionTime Assessment on the Assessment Page with one attempt and connected it to the result visualization. ## 📚 Documentation Utilized ReactionTime from ResearchKit. <img width="150" alt="Screenshot 2024-03-06 at 1 53 32 pm" src="https://github.com/CS342/2024-PICS/assets/137839789/cebb643e-a323-4f4d-a58e-5839390083bb"> <img width="150" alt="Screenshot 2024-03-06 at 1 53 19 pm" src="https://github.com/CS342/2024-PICS/assets/137839789/fab8c26c-e6aa-44ef-b7ea-c1171ee3d163"> ## ✅ Testing ## 📝 Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/CS342/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/CS342/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/CS342/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/CS342/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
Showing
6 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// This source file is part of the PICS based on the Stanford Spezi Template Application project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import ResearchKit | ||
import ResearchKitSwiftUI | ||
import SwiftUI | ||
|
||
struct ReactionTimeView: View { | ||
@AppStorage("reactionTimeResults") private var reactionTimeResults: [AssessmentResult] = [] | ||
@AppStorage("AssessmentsInProgress") private var assessmentsIP = false | ||
@Environment(\.presentationMode) var presentationMode | ||
|
||
var body: some View { | ||
ZStack { | ||
Color(red: 242 / 255, green: 242 / 255, blue: 247 / 255) | ||
.edgesIgnoringSafeArea(.all) | ||
// Displays the ResearchKit ordered task view for the ReactionTime Test. | ||
ORKOrderedTaskView( | ||
tasks: createReactionTimeTask(), | ||
tintColor: .accentColor, | ||
shouldConfirmCancel: true, | ||
result: handleTaskResult | ||
) | ||
} | ||
} | ||
|
||
// Creates the ReactionTime task to be presented to the user | ||
private func createReactionTimeTask() -> ORKOrderedTask { | ||
// Initializes a ReactionTime task with the following specified parameters | ||
let task = ORKOrderedTask.reactionTime( | ||
withIdentifier: "ReactionTimeTask", | ||
intendedUseDescription: nil, | ||
maximumStimulusInterval: 2.0, | ||
minimumStimulusInterval: 1.0, | ||
thresholdAcceleration: 0.8, | ||
numberOfAttempts: 1, | ||
timeout: 5.0, | ||
successSound: 0, | ||
timeoutSound: 0, | ||
failureSound: 0, | ||
options: [.excludeConclusion] | ||
) | ||
return task | ||
} | ||
// Handles the result of the ReactionTime task. | ||
private func handleTaskResult(result: TaskResult) async { | ||
let curTime = ProcessInfo.processInfo.systemUptime | ||
assessmentsIP = false // End the assessment | ||
// Adding this logic to dismiss the view | ||
DispatchQueue.main.async { | ||
self.presentationMode.wrappedValue.dismiss() | ||
} | ||
guard case let .completed(taskResult) = result else { | ||
// Failed or canceled test. Do nothing for current. | ||
return | ||
} | ||
// Fields to record the aggregated test results. | ||
var totalTime: TimeInterval = 0 | ||
// Extract and process the ReactionTime test results. | ||
for result in taskResult.results ?? [] { | ||
if let stepResult = result as? ORKStepResult, | ||
stepResult.identifier == "reactionTime" { | ||
for reactionTimeResult in stepResult.results ?? [] { | ||
if let curResult = reactionTimeResult as? ORKReactionTimeResult { | ||
// Calculates the total time taken to complete the test. | ||
totalTime += curTime - curResult.timestamp | ||
} | ||
} | ||
} | ||
} | ||
reactionTimeResults += [AssessmentResult(testDateTime: Date(), timeSpent: totalTime)] | ||
} | ||
} | ||
|
||
#Preview { | ||
ReactionTimeView() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters