Explorar el Código

PR feedback
- set variables to false in case of an error
- rename NSPredicate to 'tempTargetsForMainChart
- add docstrings
- change function name in TextFieldWithToolbar to actual english

polscm32 hace 1 año
padre
commit
3fc111d5bc

+ 1 - 1
Model/Helper/TempTargetStored+Helper.swift

@@ -15,7 +15,7 @@ extension NSPredicate {
         )
     }
 
-    static var currentTempTarget: NSPredicate {
+    static var tempTargetsForMainChart: NSPredicate {
         let date = Date.oneDayAgo
         return NSPredicate(
             format: "(date >= %@ AND enabled == %@) OR (date >= %@ AND enabled == %@)",

+ 8 - 0
Trio/Sources/Modules/DataTable/DataTableStateModel.swift

@@ -117,6 +117,10 @@ extension DataTable {
 
                 } catch {
                     debug(.default, "\(DebuggingIdentifiers.failed) Failed to delete carbs: \(error.localizedDescription)")
+                    await MainActor.run {
+                        carbEntryDeleted = false
+                        waitForSuggestion = false
+                    }
                 }
             }
         }
@@ -254,6 +258,10 @@ extension DataTable {
                 debugPrint(
                     "\(DebuggingIdentifiers.failed) \(#file) \(#function) Error while Insulin Deletion Task: \(error.localizedDescription)"
                 )
+                await MainActor.run {
+                    insulinEntryDeleted = false
+                    waitForSuggestion = false
+                }
             }
         }
 

+ 1 - 1
Trio/Sources/Modules/Home/HomeStateModel+Setup/TempTargetSetup.swift

@@ -22,7 +22,7 @@ extension Home.StateModel {
         let results = try await CoreDataStack.shared.fetchEntitiesAsync(
             ofType: TempTargetStored.self,
             onContext: tempTargetFetchContext,
-            predicate: NSPredicate.currentTempTarget,
+            predicate: NSPredicate.tempTargetsForMainChart,
             key: "date",
             ascending: false
         )

+ 14 - 0
Trio/Sources/Modules/Treatments/View/TreatmentsRootView.swift

@@ -133,6 +133,13 @@ extension Treatments {
             }
         }
 
+        /// Determines the next field to focus on based on the current focused field.
+        ///
+        /// This function handles the tab order navigation between input fields,
+        /// taking into account whether fat/protein fields are visible based on user settings.
+        ///
+        /// - Parameter current: The currently focused field
+        /// - Returns: The next field that should receive focus, or nil if there is no next field
         private func nextField(from current: FocusedField) -> FocusedField? {
             // If fat/protein fields are hidden, skip them in navigation
             let showFPU = state.useFPUconversion
@@ -149,6 +156,13 @@ extension Treatments {
             }
         }
 
+        /// Determines the previous field to focus on based on the current focused field.
+        ///
+        /// This function handles the reverse tab order navigation between input fields,
+        /// taking into account whether fat/protein fields are visible based on user settings.
+        ///
+        /// - Parameter current: The currently focused field
+        /// - Returns: The previous field that should receive focus, or nil if there is no previous field
         private func previousField(from current: FocusedField) -> FocusedField? {
             let showFPU = state.useFPUconversion
 

+ 14 - 4
Trio/Sources/Views/TextFieldWithToolBar.swift

@@ -59,7 +59,7 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
     public func makeUIView(context: Context) -> UITextField {
         let textField = UITextField()
         context.coordinator.textField = textField
-        textField.inputAccessoryView = isDismissible ? makeDoneToolbar(for: textField, context: context) : nil
+        textField.inputAccessoryView = isDismissible ? createToolbar(for: textField, context: context) : nil
         textField.addTarget(context.coordinator, action: #selector(Coordinator.editingDidBegin), for: .editingDidBegin)
         textField.delegate = context.coordinator
         if text == 0 { /// show no value initially, i.e. empty String
@@ -71,7 +71,12 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
         return textField
     }
 
-    private func makeDoneToolbar(for _: UITextField, context: Context) -> UIToolbar {
+    /// Creates and configures a toolbar for the text field with navigation and action buttons.
+    /// - Parameters:
+    ///   - _: The text field for which the toolbar is being created (unused parameter).
+    ///   - context: The SwiftUI context that contains the coordinator for handling button actions.
+    /// - Returns: A configured UIToolbar with appropriate buttons based on the view's configuration.
+    private func createToolbar(for _: UITextField, context: Context) -> UIToolbar {
         let toolbar = UIToolbar()
         var items: [UIBarButtonItem] = []
 
@@ -313,7 +318,7 @@ public struct TextFieldWithToolBarString: UIViewRepresentable {
     public func makeUIView(context: Context) -> UITextField {
         let textField = UITextField()
         context.coordinator.textField = textField
-        textField.inputAccessoryView = isDismissible ? makeDoneToolbar(for: textField, context: context) : nil
+        textField.inputAccessoryView = isDismissible ? createToolbar(for: textField, context: context) : nil
         textField.addTarget(context.coordinator, action: #selector(Coordinator.editingDidBegin), for: .editingDidBegin)
         textField.delegate = context.coordinator
         textField.text = text
@@ -326,7 +331,12 @@ public struct TextFieldWithToolBarString: UIViewRepresentable {
         return textField
     }
 
-    private func makeDoneToolbar(for textField: UITextField, context: Context) -> UIToolbar {
+    /// Creates and configures a toolbar for the text field with clear and dismiss buttons.
+    /// - Parameters:
+    ///   - textField: The text field for which the toolbar is being created.
+    ///   - context: The SwiftUI context that contains the coordinator for handling button actions.
+    /// - Returns: A configured UIToolbar with clear and dismiss buttons.
+    private func createToolbar(for textField: UITextField, context: Context) -> UIToolbar {
         let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
         let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
         let doneButton = UIBarButtonItem(