From 92324c91954a8ffa1a555e923c3fe7542b6c97a3 Mon Sep 17 00:00:00 2001 From: Zoya Garg <125168583+zoyagarg@users.noreply.github.com> Date: Sat, 9 Mar 2024 14:07:42 -0800 Subject: [PATCH] Fixsmoking (#60) # *FIX SMOKING BEFORE AKASH* --- Intake/Resources/Localizable.xcstrings | 44 ++---- Intake/SocialHistory/MenstrualHistory.swift | 164 +++----------------- Intake/SocialHistory/SmokingHistory.swift | 123 +++++++++++---- 3 files changed, 127 insertions(+), 204 deletions(-) diff --git a/Intake/Resources/Localizable.xcstrings b/Intake/Resources/Localizable.xcstrings index dbe3a7b..042c3fd 100644 --- a/Intake/Resources/Localizable.xcstrings +++ b/Intake/Resources/Localizable.xcstrings @@ -78,9 +78,6 @@ }, "Additional Details" : { - }, - "Additional details: %@" : { - }, "Additional Symptoms" : { @@ -105,6 +102,9 @@ }, "Auto-fill Intake Form" : { + }, + "Calculation" : { + }, "Chat" : { @@ -217,6 +217,9 @@ }, "DETAILS" : { + }, + "Do you currently smoke or have you smoked in the past?" : { + }, "Download your medical records from your health system." : { @@ -319,6 +322,12 @@ }, "It looks like an unexpected view was appended to the NavigationPath!" : { + }, + "Last period's end date" : { + + }, + "Last period's start date" : { + }, "LICENSE_INFO_TITLE" : { "localizations" : { @@ -433,6 +442,9 @@ }, "Next" : { + }, + "No" : { + }, "NOTIFICATION_PERMISSIONS_BUTTON" : { "extractionState" : "stale", @@ -595,18 +607,6 @@ } } } - }, - "Select End Date" : { - - }, - "Select Start Date" : { - - }, - "Select your last period's end date" : { - - }, - "Select your last period's start date" : { - }, "SETTINGS" : { @@ -631,12 +631,6 @@ }, "Start" : { - }, - "Start Date: %@" : { - - }, - "START_CALENDAR" : { - }, "Status" : { @@ -658,9 +652,6 @@ }, "Surgical History" : { - }, - "Symptoms: %@" : { - }, "TASK_CONTEXT_ACTION_QUESTIONNAIRE" : { "localizations" : { @@ -733,10 +724,7 @@ }, "Your Responses" : { - - }, - "YYYY-MM-DD" : { - + } }, "version" : "1.0" diff --git a/Intake/SocialHistory/MenstrualHistory.swift b/Intake/SocialHistory/MenstrualHistory.swift index 82ee93a..b0e692e 100644 --- a/Intake/SocialHistory/MenstrualHistory.swift +++ b/Intake/SocialHistory/MenstrualHistory.swift @@ -14,152 +14,37 @@ import Foundation import HealthKit import SwiftUI - -struct SectionHeader: View { - let title: String - let subtitle: String - - var body: some View { - VStack(alignment: .leading) { - Text(title) - .font(.title) - .multilineTextAlignment(.leading) - .padding(.bottom, 2) - - if !subtitle.isEmpty { - Text(subtitle) - .font(.subheadline) - .foregroundColor(.gray) - .multilineTextAlignment(.leading) - .padding(.bottom) - } - } - } -} - struct SocialHistoryQuestionView: View { - struct SectionHeader: View { - let title: String - - var body: some View { - VStack(alignment: .leading) { - Text(title) - .font(.headline) - } - } - } - - private var menstrualCycleButtons: some View { - VStack { - Button(action: { - isSelectingStartDate = true - }) { - HStack { - Text("Select your last period's start date") - Spacer() // Add Spacer here for white space - Image(systemName: "calendar") - .accessibilityLabel(Text("START_CALENDAR")) - } - } - .buttonStyle(BorderlessButtonStyle()) - - Spacer().frame(height: 16) - - Button(action: { - isSelectingEndDate = true - }) { - HStack { - Text("Select your last period's end date") - Spacer() - Image(systemName: "calendar") - .accessibilityLabel(Text("END_CALENDAR")) - } - } - .buttonStyle(BorderlessButtonStyle()) - } - } - - private var menstrualCycleInformationSection: some View { - Group { - Section(header: Text("Menstrual Information").foregroundColor(.gray)) { - menstrualCycleButtons - } - - Section(header: Text("Additional Symptoms")) { - TextField("Ex: Heavy bleeding on second day, fatigue...", text: $additionalDetails) - } - if shouldDisplayResponses { - Section(header: Text("Your Responses").foregroundColor(.gray)) { - VStack(alignment: .leading) { - Text("Start Date: \(formatDate(startDate))") - Text("End Date: \(formatDate(endDate))") - if !additionalDetails.isEmpty { - Text("Symptoms: \(additionalDetails)") - } - } - } - } - } - } - - @State private var dateString: String = "" @State private var additionalDetails: String = "" - @State private var isFemale = false - @State private var showMaleSlide = false - @State private var healthStore = HKHealthStore() - - @State private var isSelectingStartDate = false - @State private var isSelectingEndDate = false @State private var startDate = Date() @State private var endDate = Date() - @State private var lastPeriodDate: Date? - + @State private var healthStore = HKHealthStore() + @State private var isFemale = false + @State private var showMaleSlide = false @Environment(NavigationPathWrapper.self) private var navigationPath - - private var shouldDisplayResponses: Bool { - !additionalDetails.isEmpty || startDate != Date() || endDate != Date() - } - - - let numberFormatter: NumberFormatter = { - let formatter = NumberFormatter() - formatter.numberStyle = .decimal - formatter.minimum = 0 - return formatter - }() - + var body: some View { - NavigationView { // swiftlint:disable:this closure_body_length - VStack { // swiftlint:disable:this closure_body_length + NavigationView { + VStack { Form { - menstrualCycleInformationSection + Section(header: Text("Menstrual Information").foregroundColor(.gray)) { + DatePicker("Last period's start date", selection: $startDate, in: ...Date(), displayedComponents: .date) + .datePickerStyle(DefaultDatePickerStyle()) + + DatePicker("Last period's end date", selection: $endDate, in: ...Date(), displayedComponents: .date) + .datePickerStyle(DefaultDatePickerStyle()) + } + + Section(header: Text("Additional Symptoms").foregroundColor(.gray)) { + TextField("Ex: Heavy bleeding on second day, fatigue...", text: $additionalDetails) + } } .navigationTitle("Social History") .onAppear { fetchHealthKitData() } - .sheet(isPresented: $isSelectingStartDate, content: { - VStack { - DatePicker("Select Start Date", selection: $startDate, displayedComponents: .date) - .datePickerStyle(GraphicalDatePickerStyle()) - - Button("Save") { - lastPeriodDate = startDate - isSelectingStartDate = false - } - } - }) - .sheet(isPresented: $isSelectingEndDate, content: { - VStack { - DatePicker("Select End Date", selection: $endDate, displayedComponents: .date) - .datePickerStyle(GraphicalDatePickerStyle()) - - Button("Save") { - isSelectingEndDate = false - } - } - }) - Spacer() + + // Adding the Submit button outside the Form Button(action: { navigationPath.path.append(NavigationViews.smoking) }) { @@ -170,17 +55,12 @@ struct SocialHistoryQuestionView: View { .background(Color.blue) .cornerRadius(8) } - .padding() + .padding(.horizontal) + .padding(.bottom) } } } - - private func formatDate(_ date: Date) -> String { - let formatter = DateFormatter() - formatter.dateStyle = .medium - return formatter.string(from: date) - } - + private func fetchHealthKitData() { let infoToRead = Set([HKObjectType.characteristicType(forIdentifier: .biologicalSex)].compactMap { $0 }) diff --git a/Intake/SocialHistory/SmokingHistory.swift b/Intake/SocialHistory/SmokingHistory.swift index 7ba14cb..5eea334 100644 --- a/Intake/SocialHistory/SmokingHistory.swift +++ b/Intake/SocialHistory/SmokingHistory.swift @@ -10,10 +10,29 @@ // // SPDX-License-Identifier: MIT -import SwiftUI +import SwiftUI struct SmokingHistoryView: View { + struct YesNoButtonStyle: ButtonStyle { + var isSelected: Bool + + func makeBody(configuration: Self.Configuration) -> some View { + configuration.label + .padding() + .background(isSelected ? Color.blue : Color.white) + .foregroundColor(isSelected ? .white : .blue) + .cornerRadius(8) + .overlay( + RoundedRectangle(cornerRadius: 8) + .stroke(Color.blue, lineWidth: 2) + ) + .scaleEffect(configuration.isPressed ? 0.95 : 1) + } + } + + + @State private var hasSmoked: Bool? // swiftlint:disable:this discouraged_optional_boolean @State private var daysPerYear: String = "" @State private var packsPerDay: String = "" @State private var packYears: Double = 0 @@ -22,48 +41,84 @@ struct SmokingHistoryView: View { var body: some View { NavigationView { VStack { - Form { - Section(header: Text("Smoking History").foregroundColor(.gray)) { - TextField("How many days a year do you smoke?", text: $daysPerYear) - .keyboardType(.decimalPad) - .onChange(of: daysPerYear) { calculatePackYears() } - .padding(.bottom, 8) - - TextField("How many packs do you smoke a day?", text: $packsPerDay) - .keyboardType(.decimalPad) - .onChange(of: packsPerDay) { calculatePackYears() } - .padding(.bottom, 8) - } - - Section(header: Text("Additional Details").foregroundColor(.gray)) { - TextField("Ex: Smoked for 10 years, quit 5 years ago...", text: $additionalDetails) + VStack { + yesNoQuestionView + if hasSmoked == true { + smokingDetailsForm } + } + Spacer() + submitButton + } + .navigationTitle("Social History") + .background(Color(UIColor.systemGroupedBackground)) + } + } + + private var yesNoQuestionView: some View { + VStack { + Text("Do you currently smoke or have you smoked in the past?") + .foregroundColor(.gray) + .padding() + + HStack { + Button("Yes") { + hasSmoked = true + } + .buttonStyle(YesNoButtonStyle(isSelected: hasSmoked == true)) + + Button("No") { + hasSmoked = false + } + .buttonStyle(YesNoButtonStyle(isSelected: hasSmoked == false)) + } + .padding(.horizontal) + } + .padding() + .background(Color(UIColor.systemGroupedBackground)) + } + + private var smokingDetailsForm: some View { + VStack { + Form { + Section(header: Text("Smoking History").foregroundColor(.gray)) { + TextField("How many days a year do you smoke?", text: $daysPerYear) + .keyboardType(.decimalPad) + .onChange(of: daysPerYear) { calculatePackYears() } + .padding(.bottom, 8) - // This section will automatically update when values are entered - Section(header: Text("Your Responses").foregroundColor(.gray)) { + TextField("How many packs do you smoke a day?", text: $packsPerDay) + .keyboardType(.decimalPad) + .onChange(of: packsPerDay) { calculatePackYears() } + .padding(.bottom, 8) + } + Section(header: Text("Additional Details").foregroundColor(.gray)) { + TextField("Ex: Smoked for 10 years, quit 5 years ago...", text: $additionalDetails) + } + if hasSmoked == true { + Section(header: Text("Calculation").foregroundColor(.gray)) { Text("Pack years: \(packYears, specifier: "%.2f")") - if !additionalDetails.isEmpty { - Text("Additional details: \(additionalDetails)") - } } } - .navigationTitle("Social History") - - // The Submit button can remain for explicit submission, if required - Button("Submit") { - calculatePackYears() - } - .foregroundColor(.white) - .padding() - .frame(maxWidth: .infinity) - .background(Color.blue) - .cornerRadius(8) - .padding(.horizontal) - .padding(.bottom) } } } + private var submitButton: some View { + Button("Submit") { + // Implement submission logic + // If hasSmoked is nil or false, submit "No" as the answer + // If hasSmoked is true, submit the form details + } + .foregroundColor(.white) + .padding() + .frame(maxWidth: .infinity) + .background(Color.blue) + .cornerRadius(8) + .padding(.horizontal) + .padding(.bottom) + } + func calculatePackYears() { let days = Double(daysPerYear) ?? 0 let packs = Double(packsPerDay) ?? 0