Przeglądaj źródła

add ability to also edit the meal date

polscm32 aka Marvout 1 rok temu
rodzic
commit
1030033cad

+ 16 - 8
FreeAPS/Sources/Modules/DataTable/DataTableStateModel.swift

@@ -146,7 +146,7 @@ extension DataTable {
             // For FPU deletions, first get the corresponding carb entry
             if isFPUDeletion {
                 guard let correspondingEntry: (
-                    entryValues: (carbs: Decimal, fat: Decimal, protein: Decimal, note: String)?,
+                    entryValues: (carbs: Decimal, fat: Decimal, protein: Decimal, note: String, date: Date)?,
                     entryID: NSManagedObjectID?
                 ) = await handleFPUEntry(treatmentObjectID),
                     let nsManagedObjectID = correspondingEntry.entryID
@@ -281,12 +281,14 @@ extension DataTable {
         ///   - newFat: The new fat value
         ///   - newProtein: The new protein value
         ///   - newNote: The new note text
+        ///   - newDate: The new date for the entry
         func updateEntry(
             _ treatmentObjectID: NSManagedObjectID,
             newCarbs: Decimal,
             newFat: Decimal,
             newProtein: Decimal,
-            newNote: String
+            newNote: String,
+            newDate: Date
         ) {
             Task {
                 // Get original date from entry to re-create the entry later with the updated values and the same date
@@ -303,8 +305,7 @@ extension DataTable {
                 )
 
                 await createNewEntries(
-                    originalDate: originalEntry.entryValues?.date ?? Date(),
-                    // TODO: should we add this to the guard or is nullish coalesce safe enough?
+                    originalDate: newDate,
                     newCarbs: newCarbs,
                     newFat: newFat,
                     newProtein: newProtein,
@@ -424,18 +425,22 @@ extension DataTable {
         /// - Parameter objectID: The ID of the entry to load
         /// - Returns: A tuple containing the entry's values, or nil if not found
         func loadEntryValues(from objectID: NSManagedObjectID) async
-            -> (carbs: Decimal, fat: Decimal, protein: Decimal, note: String)?
+            -> (carbs: Decimal, fat: Decimal, protein: Decimal, note: String, date: Date)?
         {
             let context = CoreDataStack.shared.persistentContainer.viewContext
 
             return await context.perform {
                 do {
-                    guard let entry = try context.existingObject(with: objectID) as? CarbEntryStored else { return nil }
+                    guard let entry = try context.existingObject(with: objectID) as? CarbEntryStored,
+                          let entryDate = entry.date
+                    else { return nil }
+
                     return (
                         carbs: Decimal(entry.carbs),
                         fat: Decimal(entry.fat),
                         protein: Decimal(entry.protein),
-                        note: entry.note ?? ""
+                        note: entry.note ?? "",
+                        date: entryDate
                     )
                 } catch {
                     debugPrint("\(DebuggingIdentifiers.failed) Failed to load entry: \(error.localizedDescription)")
@@ -455,7 +460,10 @@ extension DataTable {
         /// - Parameter objectID: The ID of the FPU entry
         /// - Returns: A tuple containing the entry values and ID, or nil if not found
         func handleFPUEntry(_ objectID: NSManagedObjectID) async
-            -> (entryValues: (carbs: Decimal, fat: Decimal, protein: Decimal, note: String)?, entryID: NSManagedObjectID?)?
+            -> (
+                entryValues: (carbs: Decimal, fat: Decimal, protein: Decimal, note: String, date: Date)?,
+                entryID: NSManagedObjectID?
+            )?
         {
             // Case 1: FPU entry WITH carbs
             if let correspondingCarbEntryID = await getCorrespondingCarbEntry(objectID) {

+ 14 - 1
FreeAPS/Sources/Modules/DataTable/View/CarbEntryEditorView.swift

@@ -26,6 +26,7 @@ struct CarbEntryEditorView: View {
     @State private var editedProtein: Decimal
     @State private var editedNote: String
     @State private var isFPU: Bool
+    @State private var editedDate: Date
 
     init(state: DataTable.StateModel, carbEntry: CarbEntryStored) {
         self.state = state
@@ -36,6 +37,7 @@ struct CarbEntryEditorView: View {
         _editedNote = State(initialValue: carbEntry.note ?? "")
         _isFPU = State(initialValue: carbEntry.isFPU)
         _entryToEdit = State(initialValue: nil)
+        _editedDate = State(initialValue: Date())
     }
 
     private var carbLimitExceeded: Bool {
@@ -99,7 +101,8 @@ struct CarbEntryEditorView: View {
                         newCarbs: editedCarbs,
                         newFat: editedFat,
                         newProtein: editedProtein,
-                        newNote: editedNote
+                        newNote: editedNote,
+                        newDate: editedDate
                     )
                     dismiss()
                 }, label: {
@@ -161,6 +164,14 @@ struct CarbEntryEditorView: View {
                         TextFieldWithToolBarString(text: $editedNote, placeholder: "Note...", maxLength: 25)
                     }
                 }.listRowBackground(Color.chart)
+
+                Section {
+                    DatePicker(
+                        "Time",
+                        selection: $editedDate,
+                        displayedComponents: [.date, .hourAndMinute]
+                    )
+                }
             }
             .safeAreaInset(
                 edge: .bottom,
@@ -195,6 +206,7 @@ struct CarbEntryEditorView: View {
                     editedProtein = result.entryValues?.protein ?? 0
                     editedNote = result.entryValues?.note ?? ""
                     entryToEdit = result.entryID
+                    editedDate = result.entryValues?.date ?? Date()
                 }
                 /*
                  User taps on a carb entry in the DataTable list. There are again two cases which don't need explicit handling:
@@ -208,6 +220,7 @@ struct CarbEntryEditorView: View {
                     editedFat = values.fat
                     editedProtein = values.protein
                     editedNote = values.note
+                    editedDate = values.date
                     entryToEdit = carbEntry.objectID
                 }
             }