Skip to content

Commit

Permalink
Updates fatigue question in daily survey (#54)
Browse files Browse the repository at this point in the history
* Updates fatigue question in daily survey

* Remove redundant code

* Update constants
  • Loading branch information
vishnuravi authored Jan 11, 2025
1 parent 550911c commit e7c9f33
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
6 changes: 3 additions & 3 deletions LifeSpace.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@
CODE_SIGN_ENTITLEMENTS = "LifeSpace/Supporting Files/LifeSpace.entitlements";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 4;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -1034,7 +1034,7 @@
CODE_SIGN_ENTITLEMENTS = "LifeSpace/Supporting Files/LifeSpace.entitlements";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 4;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -1082,7 +1082,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 4;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = "";
Expand Down
12 changes: 12 additions & 0 deletions LifeSpace/SharedContext/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@

/// Constants shared across the application for settings
enum Constants {
// MARK: Survey Settings
/// Each day's survey opens by default in the evening at 7pm every day
/// and closes the next morning at 7am.
///
/// Hour to open the survey daily (in 24 hour time)
static let hourToOpenSurvey = 19
/// Hour to close the survey the following day (in 24 hour time)
static let hourToCloseSurvey = 7

// MARK: Location
/// Minimum distance between locations to record (in meters)
static let minDistanceBetweenPoints = 100.0

// MARK: URLs
/// URL of the privacy policy for the app
static let privacyPolicyURL = "https://michelleodden.com/cardinal-lifespace-privacy-policy/"

// MARK: Collections
/// User collection on Firestore
static let userCollectionName = "ls_users"
/// Location data collection on Firestore
Expand Down
18 changes: 8 additions & 10 deletions LifeSpace/Survey/DailySurveyTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,17 @@ class DailySurveyTask: ORKOrderedTask {
steps.append(question3Step)

// Question 4: Fatigue
let answerFormat4 = ORKAnswerFormat.scale(
withMaximumValue: 4,
minimumValue: 0,
defaultValue: 0,
step: 1,
vertical: false,
maximumValueDescription: "Very much",
minimumValueDescription: "Not at all"
)
let answerFormat4 = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: [
ORKTextChoice(text: "Not at all", value: 0 as NSNumber),
ORKTextChoice(text: "A little bit", value: 1 as NSNumber),
ORKTextChoice(text: "Somewhat", value: 2 as NSNumber),
ORKTextChoice(text: "Quite a bit", value: 3 as NSNumber),
ORKTextChoice(text: "Very much", value: 4 as NSNumber)
])
let question4Step = ORKQuestionStep(
identifier: "PhysicalWellBeingQuestion",
title: "Physical Well-being",
question: "I feel fatigued",
question: "Consider your day today and evaluate your agreement with the following statement.",
answer: answerFormat4
)
steps.append(question4Step)
Expand Down
22 changes: 11 additions & 11 deletions LifeSpace/Survey/DailySurveyTaskView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ struct DailySurveyTaskView: View {
}

Task {
savingSurvey = true
await saveResponse(taskResult: taskResult)
savingSurvey = false
showingSurvey = false
}
}
Expand Down Expand Up @@ -148,10 +146,10 @@ struct DailySurveyTaskView: View {

response.surveyName = "dailySurveyTask"

/// If the user is taking the survey before 7am, the `surveyDate` should reflect the previous day,
/// If the user is taking the survey the morning after, the `surveyDate` should reflect the previous day,
/// otherwise it should reflect the current day.
let surveyDate: Date
if SurveyModule.currentHour < 7 {
if SurveyModule.currentHour < Constants.hourToCloseSurvey {
surveyDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())?.startOfDay ?? Date().startOfDay
} else {
surveyDate = Date().startOfDay
Expand Down Expand Up @@ -182,21 +180,23 @@ struct DailySurveyTaskView: View {
response.emotionalWellBeingQuestion = result?.intValue
}

if let physicalWellBeingQuestion = taskResult.stepResult(forStepIdentifier: "PhysicalWellBeingQuestion")?.results {
let answer = physicalWellBeingQuestion[0] as? ORKScaleQuestionResult
if let result = answer?.scaleAnswer {
response.physicalWellBeingQuestion = Int(truncating: result)
} else {
response.physicalWellBeingQuestion = -1
}
if let physicalWellBeingQuestion = taskResult.stepResult(forStepIdentifier: "PhysicalWellBeingQuestion"),
let result = physicalWellBeingQuestion.firstResult as? ORKChoiceQuestionResult,
let answer = result.choiceAnswers?.first as? NSNumber {
response.physicalWellBeingQuestion = answer.intValue
} else {
response.physicalWellBeingQuestion = -1
}

do {
savingSurvey = true
try await standard.add(response: response)

// Update the last survey date in UserDefaults
UserDefaults.standard.set(surveyDateString, forKey: StorageKeys.lastSurveyDate)
savingSurvey = false
} catch {
savingSurvey = false
self.errorMessage = error.localizedDescription
self.didError.toggle()
}
Expand Down
11 changes: 5 additions & 6 deletions LifeSpace/Survey/SurveyModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ enum SurveyModule {
}

static var isPreviousDaySurvey: Bool {
/// If the user is taking the survey before 7am, they should be informed that they are taking the
/// previous day's survey
currentHour < 7
/// If the user is taking the survey in the morning, they should be informed that their
/// results will apply to the previous day not the current day.
currentHour < Constants.hourToCloseSurvey
}

static var surveyAlreadyTaken: Bool {
let lastSurveyDateString = UserDefaults.standard.string(forKey: StorageKeys.lastSurveyDate)

/// Determine the survey date based on the current time
let surveyDate: Date
if currentHour < 7 {
if currentHour < Constants.hourToCloseSurvey {
surveyDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())?.startOfDay ?? Date().startOfDay
} else {
surveyDate = Date().startOfDay
Expand All @@ -44,7 +44,6 @@ enum SurveyModule {
}

static var shouldShowSurvey: Bool {
/// The survey should only be shown if it between 7pm and 7am
currentHour < 7 || currentHour >= 19
currentHour < Constants.hourToCloseSurvey || currentHour >= Constants.hourToOpenSurvey
}
}

0 comments on commit e7c9f33

Please sign in to comment.