CarbEntryEditorView.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // CarbEntryEditorView.swift
  3. // FreeAPS
  4. //
  5. // Created by Marvin Polscheit on 15.01.25.
  6. //
  7. import SwiftUI
  8. struct CarbEntryEditorView: View {
  9. @Environment(\.dismiss) private var dismiss
  10. @Environment(\.colorScheme) var colorScheme
  11. @Environment(AppState.self) var appState
  12. var state: DataTable.StateModel
  13. let carbEntry: CarbEntryStored
  14. @State private var editedAmount: Decimal
  15. @State private var editedFat: Decimal
  16. @State private var editedProtein: Decimal
  17. @State private var editedNote: String
  18. @State private var isFPU: Bool
  19. init(state: DataTable.StateModel, carbEntry: CarbEntryStored) {
  20. self.state = state
  21. self.carbEntry = carbEntry
  22. _editedAmount = State(initialValue: Decimal(carbEntry.carbs))
  23. _editedFat = State(initialValue: 0) // gets updated in the task block
  24. _editedProtein = State(initialValue: 0) // gets updated in the task block
  25. _editedNote = State(initialValue: carbEntry.note ?? "")
  26. _isFPU = State(initialValue: carbEntry.isFPU)
  27. }
  28. var body: some View {
  29. NavigationView {
  30. Form {
  31. Section {
  32. if isFPU {
  33. HStack {
  34. Text("Fat")
  35. TextFieldWithToolBar(
  36. text: $editedFat,
  37. placeholder: "Enter fat",
  38. numberFormatter: Formatter.integerFormatter
  39. )
  40. Text("g").foregroundStyle(.secondary)
  41. }
  42. HStack {
  43. Text("Protein")
  44. TextFieldWithToolBar(
  45. text: $editedProtein,
  46. placeholder: "Enter protein",
  47. numberFormatter: Formatter.integerFormatter
  48. )
  49. Text("g").foregroundStyle(.secondary)
  50. }
  51. } else {
  52. HStack {
  53. Text("Amount")
  54. TextFieldWithToolBar(
  55. text: $editedAmount,
  56. placeholder: "Enter carbs",
  57. numberFormatter: Formatter.decimalFormatterWithOneFractionDigit
  58. )
  59. Text("g").foregroundStyle(.secondary)
  60. }
  61. }
  62. HStack {
  63. Text("Note")
  64. TextField("Optional note", text: $editedNote)
  65. }
  66. }.listRowBackground(Color.chart)
  67. Section {
  68. HStack {
  69. Spacer()
  70. Button("Save") {
  71. let treatmentObjectID = carbEntry.objectID
  72. if isFPU {
  73. state.updateFPUEntry(
  74. treatmentObjectID,
  75. newFat: editedFat,
  76. newProtein: editedProtein,
  77. newNote: editedNote
  78. )
  79. } else {
  80. state.updateCarbEntry(
  81. treatmentObjectID,
  82. newAmount: editedAmount,
  83. newNote: editedNote
  84. )
  85. }
  86. dismiss()
  87. }
  88. Spacer()
  89. }
  90. }
  91. .listRowBackground(Color(.systemBlue))
  92. .tint(.white)
  93. }
  94. .scrollContentBackground(.hidden)
  95. .background(appState.trioBackgroundColor(for: colorScheme))
  96. .navigationTitle("Edit Carbs")
  97. .navigationBarTitleDisplayMode(.inline)
  98. .toolbar {
  99. ToolbarItem(placement: .topBarLeading) {
  100. Button("Cancel") {
  101. dismiss()
  102. }
  103. }
  104. }
  105. }
  106. .task {
  107. if carbEntry.isFPU {
  108. if let originalEntryID = await state.getZeroCarbNonFPUEntry(carbEntry.objectID) {
  109. let context = CoreDataStack.shared.persistentContainer.viewContext
  110. await context.perform {
  111. do {
  112. if let originalEntry = try context.existingObject(with: originalEntryID) as? CarbEntryStored {
  113. editedFat = Decimal(originalEntry.fat)
  114. editedProtein = Decimal(originalEntry.protein)
  115. editedNote = originalEntry.note ?? ""
  116. }
  117. } catch {
  118. debugPrint(
  119. "\(DebuggingIdentifiers.failed) Failed to fetch original entry: \(error.localizedDescription)"
  120. )
  121. }
  122. }
  123. } else {
  124. debugPrint("\(DebuggingIdentifiers.failed) No original entry ID found")
  125. }
  126. }
  127. }
  128. }
  129. }