فهرست منبع

Fix navigation arrows in toolbar and set them up conditionally for carbs/fat/protein/bolus textfields in TreatmentsRootview

polscm32 1 سال پیش
والد
کامیت
251f752e46
2فایلهای تغییر یافته به همراه61 افزوده شده و 55 حذف شده
  1. 46 35
      Trio/Sources/Modules/Treatments/View/TreatmentsRootView.swift
  2. 15 20
      Trio/Sources/Views/TextFieldWithToolBar.swift

+ 46 - 35
Trio/Sources/Modules/Treatments/View/TreatmentsRootView.swift

@@ -80,15 +80,16 @@ extension Treatments {
             HStack {
                 HStack {
                     Text("Protein")
-
                     TextFieldWithToolBar(
                         text: $state.protein,
                         placeholder: "0",
                         keyboardType: .numberPad,
                         numberFormatter: mealFormatter,
-                        previousTextField: { focusOnPreviousTextField(index: 2) },
-                        nextTextField: { focusOnNextTextField(index: 2) }
-                    ).focused($focusedField, equals: .protein)
+                        showArrows: true,
+                        previousTextField: { focusedField = previousField(from: .protein) },
+                        nextTextField: { focusedField = nextField(from: .protein) }
+                    )
+                    .focused($focusedField, equals: .protein)
                     Text("g").foregroundColor(.secondary)
                 }
 
@@ -101,9 +102,11 @@ extension Treatments {
                         placeholder: "0",
                         keyboardType: .numberPad,
                         numberFormatter: mealFormatter,
-                        previousTextField: { focusOnPreviousTextField(index: 3) },
-                        nextTextField: { focusOnNextTextField(index: 3) }
-                    ).focused($focusedField, equals: .fat)
+                        showArrows: true,
+                        previousTextField: { focusedField = previousField(from: .fat) },
+                        nextTextField: { focusedField = nextField(from: .fat) }
+                    )
+                    .focused($focusedField, equals: .fat)
                     Text("g").foregroundColor(.secondary)
                 }
             }
@@ -118,39 +121,46 @@ extension Treatments {
                     placeholder: "0",
                     keyboardType: .numberPad,
                     numberFormatter: mealFormatter,
-                    previousTextField: { focusOnPreviousTextField(index: 1) },
-                    nextTextField: { focusOnNextTextField(index: 1) }
-                ).focused($focusedField, equals: .carbs)
-                    .onChange(of: state.carbs) {
-                        handleDebouncedInput()
-                    }
+                    showArrows: true,
+                    previousTextField: { focusedField = previousField(from: .carbs) },
+                    nextTextField: { focusedField = nextField(from: .carbs) }
+                )
+                .focused($focusedField, equals: .carbs)
+                .onChange(of: state.carbs) {
+                    handleDebouncedInput()
+                }
                 Text("g").foregroundColor(.secondary)
             }
         }
 
-        func focusOnPreviousTextField(index: Int) {
-            switch index {
-            case 2:
-                focusedField = .carbs
-            case 3:
-                focusedField = .fat
-            case 4:
-                focusedField = .protein
-            default:
-                break
+        private func nextField(from current: FocusedField) -> FocusedField? {
+            // If fat/protein fields are hidden, skip them in navigation
+            let showFPU = state.useFPUconversion
+
+            switch current {
+            case .fat:
+                return .bolus
+            case .protein:
+                return .fat
+            case .carbs:
+                return showFPU ? .protein : .bolus
+            case .bolus:
+                return .carbs
             }
         }
 
-        func focusOnNextTextField(index: Int) {
-            switch index {
-            case 1:
-                focusedField = .fat
-            case 2:
-                focusedField = .protein
-            case 3:
-                focusedField = .bolus
-            default:
-                break
+        private func previousField(from current: FocusedField) -> FocusedField? {
+            let showFPU = state.useFPUconversion
+
+            switch current {
+            case .fat:
+                return .protein
+            case .protein:
+                return .carbs
+            case .carbs:
+                return .bolus
+            case .bolus:
+                return showFPU ? .fat : .carbs
             }
         }
 
@@ -287,8 +297,9 @@ extension Treatments {
                                     textColor: colorScheme == .dark ? .white : .blue,
                                     maxLength: 5,
                                     numberFormatter: formatter,
-                                    previousTextField: { focusOnPreviousTextField(index: 4) },
-                                    nextTextField: { focusOnNextTextField(index: 4) }
+                                    showArrows: true,
+                                    previousTextField: { focusedField = previousField(from: .bolus) },
+                                    nextTextField: { focusedField = nextField(from: .bolus) }
                                 ).focused($focusedField, equals: .bolus)
                                     .onChange(of: state.amount) {
                                         Task {

+ 15 - 20
Trio/Sources/Views/TextFieldWithToolBar.swift

@@ -33,9 +33,9 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
         textFieldDidBeginEditing: (() -> Void)? = nil,
         numberFormatter: NumberFormatter,
         allowDecimalSeparator: Bool = true,
+        showArrows: Bool = false,
         previousTextField: (() -> Void)? = nil,
-        nextTextField: (() -> Void)? = nil,
-        showArrows: Bool = false
+        nextTextField: (() -> Void)? = nil
     ) {
         _text = text
         self.placeholder = placeholder
@@ -51,9 +51,9 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
         self.numberFormatter = numberFormatter
         self.numberFormatter.numberStyle = .decimal
         self.allowDecimalSeparator = allowDecimalSeparator
+        self.showArrows = showArrows
         self.previousTextField = previousTextField
         self.nextTextField = nextTextField
-        self.showArrows = showArrows
     }
 
     public func makeUIView(context: Context) -> UITextField {
@@ -77,9 +77,19 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
 
         // Add navigation arrows if enabled
         if showArrows {
+            // Add clear button
+            items.append(
+                UIBarButtonItem(
+                    image: UIImage(systemName: "trash"),
+                    style: .plain,
+                    target: context.coordinator,
+                    action: #selector(Coordinator.clearText)
+                )
+            )
+
             if previousTextField != nil {
                 let previousButton = UIBarButtonItem(
-                    image: UIImage(systemName: "chevron.left"),
+                    image: UIImage(systemName: "chevron.up"),
                     style: .plain,
                     target: context.coordinator,
                     action: #selector(Coordinator.previousTextField)
@@ -89,30 +99,15 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
 
             if nextTextField != nil {
                 let nextButton = UIBarButtonItem(
-                    image: UIImage(systemName: "chevron.right"),
+                    image: UIImage(systemName: "chevron.down"),
                     style: .plain,
                     target: context.coordinator,
                     action: #selector(Coordinator.nextTextField)
                 )
                 items.append(nextButton)
             }
-
-            // Add flexible space between arrows and other buttons
-            if !items.isEmpty {
-                items.append(UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil))
-            }
         }
 
-        // Add clear button
-        items.append(
-            UIBarButtonItem(
-                image: UIImage(systemName: "trash"),
-                style: .plain,
-                target: context.coordinator,
-                action: #selector(Coordinator.clearText)
-            )
-        )
-
         // Add flexible space
         items.append(UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil))