| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- //
- // CarbEntryEditorView.swift
- // FreeAPS
- //
- // Created by Marvin Polscheit on 15.01.25.
- //
- import SwiftUI
- struct CarbEntryEditorView: View {
- @Environment(\.dismiss) private var dismiss
- @Environment(\.colorScheme) var colorScheme
- @Environment(AppState.self) var appState
- var state: DataTable.StateModel
- let carbEntry: CarbEntryStored
- @State private var editedAmount: Decimal
- @State private var editedFat: Decimal
- @State private var editedProtein: Decimal
- @State private var editedNote: String
- @State private var isFPU: Bool
- init(state: DataTable.StateModel, carbEntry: CarbEntryStored) {
- self.state = state
- self.carbEntry = carbEntry
- _editedAmount = State(initialValue: Decimal(carbEntry.carbs))
- _editedFat = State(initialValue: 0) // gets updated in the task block
- _editedProtein = State(initialValue: 0) // gets updated in the task block
- _editedNote = State(initialValue: carbEntry.note ?? "")
- _isFPU = State(initialValue: carbEntry.isFPU)
- }
- var body: some View {
- NavigationView {
- Form {
- Section {
- if isFPU {
- HStack {
- Text("Fat")
- TextFieldWithToolBar(
- text: $editedFat,
- placeholder: "Enter fat",
- numberFormatter: Formatter.integerFormatter
- )
- Text("g").foregroundStyle(.secondary)
- }
- HStack {
- Text("Protein")
- TextFieldWithToolBar(
- text: $editedProtein,
- placeholder: "Enter protein",
- numberFormatter: Formatter.integerFormatter
- )
- Text("g").foregroundStyle(.secondary)
- }
- } else {
- HStack {
- Text("Amount")
- TextFieldWithToolBar(
- text: $editedAmount,
- placeholder: "Enter carbs",
- numberFormatter: Formatter.decimalFormatterWithOneFractionDigit
- )
- Text("g").foregroundStyle(.secondary)
- }
- }
- HStack {
- Text("Note")
- TextField("Optional note", text: $editedNote)
- }
- }.listRowBackground(Color.chart)
- Section {
- HStack {
- Spacer()
- Button("Save") {
- let treatmentObjectID = carbEntry.objectID
- if isFPU {
- state.updateFPUEntry(
- treatmentObjectID,
- newFat: editedFat,
- newProtein: editedProtein,
- newNote: editedNote
- )
- } else {
- state.updateCarbEntry(
- treatmentObjectID,
- newAmount: editedAmount,
- newNote: editedNote
- )
- }
- dismiss()
- }
- Spacer()
- }
- }
- .listRowBackground(Color(.systemBlue))
- .tint(.white)
- }
- .scrollContentBackground(.hidden)
- .background(appState.trioBackgroundColor(for: colorScheme))
- .navigationTitle("Edit Carbs")
- .navigationBarTitleDisplayMode(.inline)
- .toolbar {
- ToolbarItem(placement: .topBarLeading) {
- Button("Cancel") {
- dismiss()
- }
- }
- }
- }
- .task {
- if carbEntry.isFPU {
- if let originalEntryID = await state.getZeroCarbNonFPUEntry(carbEntry.objectID) {
- let context = CoreDataStack.shared.persistentContainer.viewContext
- await context.perform {
- do {
- if let originalEntry = try context.existingObject(with: originalEntryID) as? CarbEntryStored {
- editedFat = Decimal(originalEntry.fat)
- editedProtein = Decimal(originalEntry.protein)
- editedNote = originalEntry.note ?? ""
- }
- } catch {
- debugPrint(
- "\(DebuggingIdentifiers.failed) Failed to fetch original entry: \(error.localizedDescription)"
- )
- }
- }
- } else {
- debugPrint("\(DebuggingIdentifiers.failed) No original entry ID found")
- }
- }
- }
- }
- }
|