Skip to content

Commit

Permalink
Past data (#78)
Browse files Browse the repository at this point in the history
# *Allows you to load most recent filled out form*

## ♻️ Current situation & Problem
*Link any open issues or pull requests (PRs) related to this PR. Please
ensure that all non-trivial PRs are first tracked and discussed in an
existing GitHub issue or discussion.*


## ⚙️ Release Notes 
*This PR adds functionality to store your most recent form on your
phone, so when you open up the app, you have the option to load your
most recent form instead of refilling it


## 📚 Documentation
*Please ensure that you properly document any additions in conformance
to [Spezi Documentation
Guide](https://github.com/StanfordSpezi/.github/blob/main/DOCUMENTATIONGUIDE.md).*
*You can use this section to describe your solution, but we encourage
contributors to document your reasoning and changes using in-line
documentation.*


## ✅ Testing
*Please ensure that the PR meets the testing requirements set by CodeCov
and that new functionality is appropriately tested.*
*This section describes important information about the tests and why
some elements might not be testable.*


## 📝 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):
- [ ] 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
apgupta3303 authored Mar 13, 2024
1 parent 5c68ae9 commit ff294b3
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/StanfordSpezi/SpeziMedication.git",
"state" : {
"revision" : "95ca9aebbd23f3842639d6e322785a0ff3620aac",
"version" : "0.4.0"
"revision" : "03d82c275be588970eda4d5ab335609a8260014b",
"version" : "0.4.1"
}
},
{
Expand Down
11 changes: 0 additions & 11 deletions Intake/Allergy Records/AllergyRecords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ import SpeziLLM
import SpeziLLMOpenAI
import SwiftUI

struct AllergyItem: Identifiable, Equatable {
let id = UUID()
var allergy: String
var reaction: [ReactionItem]

static func == (lhs: AllergyItem, rhs: AllergyItem) -> Bool {
lhs.allergy == rhs.allergy
}
}


struct ChatButton: View {
// Use @Binding to create a two-way binding to the parent view's showingChat state
@Binding var showingChat: Bool
Expand Down
5 changes: 0 additions & 5 deletions Intake/Allergy Records/ReactionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import Foundation
import SpeziFHIR
import SwiftUI

struct ReactionItem: Identifiable {
var id = UUID()
var reaction: String
}

struct ReactionView: View {
@State private var reactionRecords: [ReactionItem]
@State private var name: String
Expand Down
85 changes: 83 additions & 2 deletions Intake/Home.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct StartButton: View {
Button(action: {
navigationPath.append(NavigationViews.general)
}) {
Text("Start")
Text("Create New Form")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(.white)
Expand All @@ -45,6 +45,57 @@ struct StartButton: View {
}
}

struct LoadLastButton: View {
@Binding var navigationPath: NavigationPath
@Binding var disabled: Bool
@Environment(DataStore.self) private var data

var body: some View {
Button(action: {
let fetchData = loadDataStore()
if let loadedData = fetchData {
data.allergyData = loadedData.allergyData
data.generalData = loadedData.generalData
data.surgeries = loadedData.surgeries
data.conditionData = loadedData.conditionData
data.menstrualHistory = loadedData.menstrualHistory
data.smokingHistory = loadedData.smokingHistory
data.chiefComplaint = loadedData.chiefComplaint
data.surgeriesLoaded = loadedData.surgeriesLoaded
data.medicationData = loadedData.medicationData
navigationPath.append(NavigationViews.pdfs)
}
}) {
Text("Load Latest Form")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(Color.white)
.padding()
.background(disabled ? Color.blue.opacity(0.5) : Color.blue)
.cornerRadius(10)
}
.disabled(disabled)
}

func loadDataStore() -> DataStore? {
let decoder = JSONDecoder()
if let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let pathWithFilename = documentDirectory.appendingPathComponent("DataStore3.json")
if let data = try? Data(contentsOf: pathWithFilename) {
do {
let dataStore = try decoder.decode(DataStore.self, from: data)
print("successfully loaded")
return dataStore
} catch {
print("Failed to load DataStore: \(error)")
}
}
}
return nil
}
}


struct SettingsButton: View {
@Binding var showSettings: Bool

Expand Down Expand Up @@ -72,7 +123,8 @@ struct HomeView: View {

@State private var presentingAccount = false
@State private var showSettings = false

@State var isButtonDisabled = true

@Environment(NavigationPathWrapper.self) private var navigationPath
@Environment(DataStore.self) private var data

Expand Down Expand Up @@ -107,7 +159,11 @@ struct HomeView: View {
homeLogo
homeTitle
Spacer()
LoadLastButton(navigationPath: $navigationPath.path, disabled: $isButtonDisabled)
.padding(.bottom, 10)
StartButton(navigationPath: $navigationPath.path)
.padding(.top, 10)
Spacer()
}

.toolbar {
Expand Down Expand Up @@ -143,6 +199,31 @@ struct HomeView: View {
AccountSheet()
}
.verifyRequiredAccountDetails(Self.accountEnabled)
.onAppear {
let fetchData = loadDataStore()
if fetchData != nil {
isButtonDisabled = false
} else {
isButtonDisabled = true
}
}
}

func loadDataStore() -> DataStore? {
let decoder = JSONDecoder()
if let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let pathWithFilename = documentDirectory.appendingPathComponent("DataStore3.json")
if let data = try? Data(contentsOf: pathWithFilename) {
do {
let dataStore = try decoder.decode(DataStore.self, from: data)
print("successfully loaded")
return dataStore
} catch {
print("Failed to load DataStore: \(error)")
}
}
}
return nil
}
}

Expand Down
44 changes: 40 additions & 4 deletions Intake/Intake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,64 @@ class NavigationPathWrapper {
var path = NavigationPath()
}

struct PatientData {
struct PatientData: Codable {
var name: String
var birthdate: String
var age: String
var sex: String
}

struct MenstrualHistoryItem: Equatable {
struct ReactionItem: Identifiable, Codable {
var id = UUID()
var reaction: String
}


struct AllergyItem: Identifiable, Equatable, Codable {
var id = UUID()
var allergy: String
var reaction: [ReactionItem]

static func == (lhs: AllergyItem, rhs: AllergyItem) -> Bool {
lhs.allergy == rhs.allergy
}
}

struct MedicalHistoryItem: Identifiable, Equatable, Codable {
var id = UUID()
var condition: String
var active: Bool
}


struct MenstrualHistoryItem: Codable {
var startDate: Date
var endDate: Date
var additionalDetails: String
}

struct SmokingHistoryItem {
struct SmokingHistoryItem: Codable {
var hasSmokedOrSmoking: Bool
var currentlySmoking: Bool
var smokedInThePast: Bool
var additionalDetails: String
}

struct SurgeryItem: Identifiable, Equatable, Codable {
var id = UUID()
var surgeryName: String = ""
var date: String = ""
var endDate: String = ""
var status: String = ""
var location: String = ""
var notes: [String] = []
var bodySites: [String] = []
var complications: [String] = []
}


@Observable
class DataStore {
class DataStore: Codable {
var allergyData: [AllergyItem] = []
var conditionData: [MedicalHistoryItem] = []
var medicationData: Set<IntakeMedicationInstance> = []
Expand Down
6 changes: 0 additions & 6 deletions Intake/Medical History/MedicalHistoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import SpeziLLM
import SpeziLLMOpenAI
import SwiftUI

struct MedicalHistoryItem: Identifiable, Equatable {
var id = UUID()
var condition: String
var active: Bool
}

struct MedicalHistoryView: View {
@Environment(FHIRStore.self) private var fhirStore
@Environment(NavigationPathWrapper.self) private var navigationPath
Expand Down
2 changes: 1 addition & 1 deletion Intake/Medication View/IntakeDosage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
import Foundation
import SpeziMedication

struct IntakeDosage: Dosage {
struct IntakeDosage: Dosage, Codable {
var localizedDescription: String
}
2 changes: 1 addition & 1 deletion Intake/Medication View/IntakeMedication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import Foundation
import SpeziMedication

struct IntakeMedication: Medication, Comparable {
struct IntakeMedication: Medication, Comparable, Codable {
var localizedDescription: String
var dosages: [IntakeDosage]
}
2 changes: 1 addition & 1 deletion Intake/Medication View/IntakeMedicationInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import Foundation
import SpeziMedication

struct IntakeMedicationInstance: MedicationInstance, MedicationInstanceInitializable {
struct IntakeMedicationInstance: MedicationInstance, MedicationInstanceInitializable, Codable {
let id: UUID
let type: IntakeMedication
var dosage: IntakeDosage
Expand Down
2 changes: 1 addition & 1 deletion Intake/Medication View/MedicationContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct MedicationContentView: View {
var body: some View {
VStack {
if let medicationSettingsViewModel {
MedicationSettings(allowEmtpySave: true, medicationSettingsViewModel: medicationSettingsViewModel) {
MedicationSettings(allowEmptySave: true, medicationSettingsViewModel: medicationSettingsViewModel) {
data.medicationData = medicationSettingsViewModel.medicationInstances
navigationPath.path.append(NavigationViews.allergies)
}
Expand Down
9 changes: 6 additions & 3 deletions Intake/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@
}
}
}
},
"Create New Form" : {

},
"Currently Smoking:" : {

Expand Down Expand Up @@ -391,6 +394,9 @@
},
"List your current medications." : {

},
"Load Latest Form" : {

},
"Location" : {

Expand Down Expand Up @@ -739,9 +745,6 @@
},
"Social History" : {

},
"Start" : {

},
"Start Date:" : {

Expand Down
66 changes: 17 additions & 49 deletions Intake/ScrollablePDF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ struct ScrollablePDF: View {

private struct ExportButton: View {
@Environment(NavigationPathWrapper.self) private var navigationPath
@Environment(DataStore.self) private var data

var body: some View {
Button(action: {
saveDataStore(dataStore: data)
navigationPath.path.append(NavigationViews.export)
}) {
Text("Share")
Expand All @@ -71,6 +73,21 @@ struct ScrollablePDF: View {
.cornerRadius(8)
}
}

func saveDataStore(dataStore: DataStore) {
let encoder = JSONEncoder()
do {
let data = try encoder.encode(dataStore)
// You can also use UserDefaults if the data is small enough, but file storage is recommended for larger data
if let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let pathWithFilename = documentDirectory.appendingPathComponent("DataStore3.json")
try data.write(to: pathWithFilename)
print("successfully stored")
}
} catch {
print("Failed to save DataStore: \(error)")
}
}
}

private struct SurgerySection: View {
Expand Down Expand Up @@ -160,55 +177,6 @@ struct ScrollablePDF: View {
}
}

// private struct Allergy: View {
// @Environment(DataStore.self) private var data
// @State private var showingReaction = false
// @State private var selectedIndex = 0
// var body: some View {
// Section(header: HeaderTitle(title: "Allergy", nextView: NavigationViews.allergies)) {
// List {
// ForEach(0..<data.allergyData.count, id: \.self) { index in
// allergyButton(index: index)
// }
// }
// .sheet(isPresented: $showingReaction, content: reactionPDFView)
// List(data.allergyData, id: \.id) { item in
// HStack {
// Text(item.allergy)
// Spacer()
// Text(item.date)
// .foregroundColor(.secondary)
// }
// }
// }
// }
//
// private func reactionPDFView() -> some View {
// ReactionPDF(index: selectedIndex, showingReaction: $showingReaction)
// }
//
// private func allergyButton(index: Int) -> some View {
// Button(action: {
// self.selectedIndex = index
// self.showingReaction = true
// }) {
// HStack {
// Text(data.allergyData[index].allergy)
// .foregroundColor(.black)
// Spacer()
// Image(systemName: "chevron.right")
// .foregroundColor(.gray)
// .accessibilityLabel(Text("DETAILS"))
// }
// }
// }
//
// func concatenate(strings: [ReactionItem]) -> String {
// let names = strings.map { $0.reaction }
// return names.joined(separator: ", ")
// }
// }

private struct AllergySection: View {
@Environment(DataStore.self) private var data
@Environment(NavigationPathWrapper.self) private var navigationPath
Expand Down
Loading

0 comments on commit ff294b3

Please sign in to comment.