Skip to content

Commit

Permalink
HandleTaskResult Update for ReactionTest (#51)
Browse files Browse the repository at this point in the history
# Updating HandleTaskResult for ReactionTest

## ♻️ Current situation & Problem
- Use decodable to parse together DeviceMotion data to find average
reaction time from 5 attempts
- Also fixed Medication Page so user must upload picture before
continuing

## ⚙️ Release Notes 
- User can now complete the ReactionTime test with 5 attempts and get
data visualizations on average reaction time
- Note: ReactionTime test does not work on the simulator so there is a
constant value of 0.5 sections set for the reaction time if run on the
simulator


## 📚 Documentation
- Added DeviceMotionModel file to model the data 


## ✅ 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
czhang771 authored Mar 8, 2024
1 parent a7c6c16 commit 3be7d9a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 29 deletions.
27 changes: 10 additions & 17 deletions PICS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
22201EE82B9259B800E38F47 /* EQ5D5L.json in Resources */ = {isa = PBXBuildFile; fileRef = 22201EE52B9259B800E38F47 /* EQ5D5L.json */; };
22201EE92B9259B800E38F47 /* PHQ-4.json in Resources */ = {isa = PBXBuildFile; fileRef = 22201EE62B9259B800E38F47 /* PHQ-4.json */; };
22201EEA2B9259B800E38F47 /* Self-MNA.json in Resources */ = {isa = PBXBuildFile; fileRef = 22201EE72B9259B800E38F47 /* Self-MNA.json */; };
22306A2A2B7C4E8E000A8EC1 /* OnboardingQuestionnaire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22306A292B7C4E8E000A8EC1 /* OnboardingQuestionnaire.swift */; };
22306A3E2B8F0097000A8EC1 /* OnboardingSurveyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22306A3D2B8F0097000A8EC1 /* OnboardingSurveyView.swift */; };
22306A412B8F20C4000A8EC1 /* AccountQuestionnaire.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22306A402B8F20C4000A8EC1 /* AccountQuestionnaire.swift */; };
22306A462B8FB173000A8EC1 /* OnboardingQuestionnaireDashboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22306A452B8FB173000A8EC1 /* OnboardingQuestionnaireDashboard.swift */; };
22333FFB2B9A577F008670AE /* DeviceMotionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22333FFA2B9A577F008670AE /* DeviceMotionModel.swift */; };
22365B782B902C9100C4528E /* Onboarding-Questionnaire.json in Resources */ = {isa = PBXBuildFile; fileRef = 22365B772B902C9100C4528E /* Onboarding-Questionnaire.json */; };
226C16F02B69DF3500FBA97D /* HeightKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 226C16EF2B69DF3500FBA97D /* HeightKey.swift */; };
226C16F22B6C820C00FBA97D /* WeightKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 226C16F12B6C820B00FBA97D /* WeightKey.swift */; };
Expand Down Expand Up @@ -82,7 +81,6 @@
748117172B74BC3E00801A9A /* EQ5D5L.json in Resources */ = {isa = PBXBuildFile; fileRef = 748117152B74BC3E00801A9A /* EQ5D5L.json */; };
748117182B74BC3E00801A9A /* EQ5D5L.json.license in Resources */ = {isa = PBXBuildFile; fileRef = 748117162B74BC3E00801A9A /* EQ5D5L.json.license */; };
7481171A2B74BC5B00801A9A /* Self-MNA.json in Resources */ = {isa = PBXBuildFile; fileRef = 748117192B74BC5B00801A9A /* Self-MNA.json */; };
7481171C2B74BC6D00801A9A /* PHQ-4.json in Resources */ = {isa = PBXBuildFile; fileRef = 7481171B2B74BC6D00801A9A /* PHQ-4.json */; };
74AE05882B918DAF00AB5287 /* StroopTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74AE05872B918DAF00AB5287 /* StroopTest.swift */; };
74AE05932B98620300AB5287 /* AssessmentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74AE05922B98620300AB5287 /* AssessmentsTests.swift */; };
74C593952B720781002D0274 /* (null) in Resources */ = {isa = PBXBuildFile; };
Expand Down Expand Up @@ -138,6 +136,7 @@
22306A3D2B8F0097000A8EC1 /* OnboardingSurveyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingSurveyView.swift; sourceTree = "<group>"; };
22306A402B8F20C4000A8EC1 /* AccountQuestionnaire.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountQuestionnaire.swift; sourceTree = "<group>"; };
22306A452B8FB173000A8EC1 /* OnboardingQuestionnaireDashboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingQuestionnaireDashboard.swift; sourceTree = "<group>"; };
22333FFA2B9A577F008670AE /* DeviceMotionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceMotionModel.swift; sourceTree = "<group>"; };
22365B772B902C9100C4528E /* Onboarding-Questionnaire.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "Onboarding-Questionnaire.json"; sourceTree = "<group>"; };
226C16EF2B69DF3500FBA97D /* HeightKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeightKey.swift; sourceTree = "<group>"; };
226C16F12B6C820B00FBA97D /* WeightKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeightKey.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -482,6 +481,7 @@
A459932B2B906C3B00A98C95 /* ResultsViz.swift */,
74AE05872B918DAF00AB5287 /* StroopTest.swift */,
22C75CD92B969B89008986AF /* ReactionTime.swift */,
22333FFA2B9A577F008670AE /* DeviceMotionModel.swift */,
);
path = Assessment;
sourceTree = "<group>";
Expand Down Expand Up @@ -602,7 +602,7 @@
attributes = {
BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1340;
LastUpgradeCheck = 1520;
LastUpgradeCheck = 1530;
TargetAttributes = {
653A254C283387FE005D4D48 = {
CreatedOnToolsVersion = 13.4;
Expand Down Expand Up @@ -672,17 +672,9 @@
2FA0BFED2ACC977500E0EF83 /* Localizable.xcstrings in Resources */,
741110F22B5767DF00DFC79E /* PROM-Questionnaire.json in Resources */,
74C593972B720B5E002D0274 /* (null) in Resources */,
22201EEA2B9259B800E38F47 /* Self-MNA.json in Resources */,
2FC3439229EE634B002D773C /* ConsentDocument.md in Resources */,
653A255528338800005D4D48 /* Assets.xcassets in Resources */,
22201EE92B9259B800E38F47 /* PHQ-4.json in Resources */,
2FC3439029EE6346002D773C /* SocialSupportQuestionnaire.json in Resources */,
22201EE82B9259B800E38F47 /* EQ5D5L.json in Resources */,
22365B782B902C9100C4528E /* Onboarding-Questionnaire.json in Resources */,
2FA0BFED2ACC977500E0EF83 /* Localizable.xcstrings in Resources */,
741110F22B5767DF00DFC79E /* PROM-Questionnaire.json in Resources */,
2F6025CB29BBE70F0045459E /* GoogleService-Info.plist in Resources */,
7481171C2B74BC6D00801A9A /* PHQ-4.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -757,6 +749,7 @@
8644E69B2B722708001218D0 /* Appointments.swift in Sources */,
8644E6842B72046B001218D0 /* GettingThere.swift in Sources */,
2FE5DC4F29EDD7FA004B9AB4 /* EventContext.swift in Sources */,
22333FFB2B9A577F008670AE /* DeviceMotionModel.swift in Sources */,
226C16F02B69DF3500FBA97D /* HeightKey.swift in Sources */,
864909422B8AA1C300054C9A /* TrailMakingTest.swift in Sources */,
2FE5DC5029EDD7FA004B9AB4 /* EventContextView.swift in Sources */,
Expand Down Expand Up @@ -901,7 +894,7 @@
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 2CJ9STHUFD;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "PICS/Supporting Files/Info.plist";
Expand Down Expand Up @@ -933,7 +926,7 @@
"SWIFT_ELicenseRef-PICS_LOC_STRINGS" = YES;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 1;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Test;
};
Expand Down Expand Up @@ -1101,7 +1094,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = "PICS/Supporting Files/PICS.entitlements";
CODE_SIGN_ENTITLEMENTS = "PICS/Supporting Files/PICSDebug.entitlements";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
Expand Down Expand Up @@ -1139,7 +1132,7 @@
"SWIFT_ELicenseRef-PICS_LOC_STRINGS" = YES;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 1;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
Expand Down Expand Up @@ -1189,7 +1182,7 @@
"SWIFT_ELicenseRef-PICS_LOC_STRINGS" = YES;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 1;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
Expand Down
2 changes: 1 addition & 1 deletion PICS.xcodeproj/xcshareddata/xcschemes/PICS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1520"
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
17 changes: 17 additions & 0 deletions PICS/Assessment/DeviceMotionModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// 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 Foundation

struct DeviceMotionItem: Decodable {
let timestamp: TimeInterval
}

struct DeviceMotion: Decodable {
let items: [DeviceMotionItem]
}
25 changes: 20 additions & 5 deletions PICS/Assessment/ReactionTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct ReactionTimeView: View {
maximumStimulusInterval: 2.0,
minimumStimulusInterval: 1.0,
thresholdAcceleration: 0.8,
numberOfAttempts: 1,
numberOfAttempts: 5,
timeout: 5.0,
successSound: 0,
timeoutSound: 0,
Expand All @@ -49,7 +49,6 @@ struct ReactionTimeView: View {
}
// 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 {
Expand All @@ -67,13 +66,29 @@ struct ReactionTimeView: View {
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
#if targetEnvironment(simulator)
// adding fixed size because ReactionTime does not work on simulator
totalTime += 0.5
#else
// get DeviceMotion data to find elapsed time
let jsonURL = curResult.fileResult.fileURL
if let jsonURL = jsonURL, let jsonData = try? Data(contentsOf: jsonURL) {
let decoder = JSONDecoder()
do {
let deviceMotion = try decoder.decode(DeviceMotion.self, from: jsonData)
if let lastItem = deviceMotion.items.last {
totalTime += (lastItem.timestamp - curResult.timestamp)
}
} catch {
print("Error parsing JSON:", error)
}
}
#endif
}
}
}
}
reactionTimeResults += [AssessmentResult(testDateTime: Date(), timeSpent: totalTime)]
reactionTimeResults += [AssessmentResult(testDateTime: Date(), timeSpent: totalTime / 5)]
}
}

Expand Down
4 changes: 2 additions & 2 deletions PICS/Onboarding/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftUI


struct ContentView: View {
@State var image: UIImage?
@Binding var image: UIImage?
@Environment(PICSStandard.self) private var standard

private var swiftUIImage: some View {
Expand Down Expand Up @@ -48,6 +48,6 @@ struct ContentView: View {

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
ContentView(image: .constant(nil))
}
}
5 changes: 3 additions & 2 deletions PICS/Onboarding/Medication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SwiftUI

struct Medication: View {
@Environment(OnboardingNavigationPath.self) private var onboardingNavigationPath

@State var image: UIImage?
var body: some View {
OnboardingView(
contentView: {
Expand All @@ -23,7 +23,7 @@ struct Medication: View {
subtitle: "MEDICATION_SUBTITLE"
)
Spacer()
ContentView()
ContentView(image: $image)
}
}, actionView: {
OnboardingActionsView(
Expand All @@ -32,6 +32,7 @@ struct Medication: View {
onboardingNavigationPath.nextStep()
}
)
.disabled(image == nil)
}
)

Expand Down
4 changes: 2 additions & 2 deletions PICS/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleAllowMixedLocalizations</key>
<true/>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>UIApplicationSceneManifest</key>
Expand All @@ -11,7 +13,5 @@
<key>UISceneConfigurations</key>
<dict/>
</dict>
<key>CFBundleAllowMixedLocalizations</key>
<true/>
</dict>
</plist>

0 comments on commit 3be7d9a

Please sign in to comment.