Explorar el Código

Introduce navigation paths; refactor logic accordingly

Deniz Cengiz hace 1 año
padre
commit
dcb501a1fa

+ 15 - 12
Trio Watch App Extension/Views/BolusConfirmationView.swift

@@ -3,11 +3,10 @@ import SwiftUI
 import WatchKit
 import WatchKit
 
 
 struct BolusConfirmationView: View {
 struct BolusConfirmationView: View {
-    @Environment(\.dismiss) var dismiss
-
-    let bolusAmount: Double
-    @Binding var progress: Double
+    @ObservedObject var navigationState: NavigationState
     let state: WatchState
     let state: WatchState
+    @Binding var bolusAmount: Double
+    @Binding var confirmationProgress: Double
 
 
     @FocusState private var isCrownFocused: Bool
     @FocusState private var isCrownFocused: Bool
 
 
@@ -35,8 +34,8 @@ struct BolusConfirmationView: View {
                 }.padding(.horizontal)
                 }.padding(.horizontal)
             }
             }
 
 
-            ProgressView(value: progress, total: 1.0)
-                .tint(progress >= 1.0 ? .green : .gray)
+            ProgressView(value: confirmationProgress, total: 1.0)
+                .tint(confirmationProgress >= 1.0 ? .green : .gray)
                 .padding(.horizontal)
                 .padding(.horizontal)
 
 
             Spacer()
             Spacer()
@@ -45,16 +44,16 @@ struct BolusConfirmationView: View {
                 if state.carbsAmount > 0 {
                 if state.carbsAmount > 0 {
                     state.carbsAmount = 0 // reset carbs in state
                     state.carbsAmount = 0 // reset carbs in state
                 }
                 }
-                dismiss()
+                bolusAmount = 0 // reset bolus in state
+                confirmationProgress = 0 // reset auth progress
+                navigationState.resetToRoot()
             }
             }
             .buttonStyle(.bordered)
             .buttonStyle(.bordered)
-            .tint(.blue)
-            .disabled(!(bolusAmount > 0.0))
         }
         }
         .focusable(true)
         .focusable(true)
         .focused($isCrownFocused)
         .focused($isCrownFocused)
         .digitalCrownRotation(
         .digitalCrownRotation(
-            $progress,
+            $confirmationProgress,
             from: 0.0,
             from: 0.0,
             through: 1.0,
             through: 1.0,
             by: 0.05,
             by: 0.05,
@@ -65,7 +64,7 @@ struct BolusConfirmationView: View {
         .onAppear {
         .onAppear {
             isCrownFocused = true
             isCrownFocused = true
         }
         }
-        .onChange(of: progress) { _, newValue in
+        .onChange(of: confirmationProgress) { _, newValue in
             if newValue >= 1.0 {
             if newValue >= 1.0 {
                 WKInterfaceDevice.current().play(.success)
                 WKInterfaceDevice.current().play(.success)
 
 
@@ -75,7 +74,11 @@ struct BolusConfirmationView: View {
                         state.carbsAmount = 0 // reset carbs in state
                         state.carbsAmount = 0 // reset carbs in state
                     }
                     }
                     state.sendBolusRequest(Decimal(bolusAmount))
                     state.sendBolusRequest(Decimal(bolusAmount))
-                    dismiss()
+                    bolusAmount = 0 // reset bolus in state
+                    confirmationProgress = 0 // reset auth progress
+                    navigationState.resetToRoot()
+
+                    // TODO: add a fancy success animation
                 }
                 }
             } else if newValue > 0 {
             } else if newValue > 0 {
                 WKInterfaceDevice.current().play(.click)
                 WKInterfaceDevice.current().play(.click)

+ 4 - 10
Trio Watch App Extension/Views/BolusInputView.swift

@@ -5,9 +5,9 @@ import WatchKit
 // MARK: - Bolus Input View
 // MARK: - Bolus Input View
 
 
 struct BolusInputView: View {
 struct BolusInputView: View {
+    @ObservedObject var navigationState: NavigationState
     @State private var bolusAmount = 0.0
     @State private var bolusAmount = 0.0
     @State private var navigateToConfirmation = false
     @State private var navigateToConfirmation = false
-    @State private var confirmationProgress = 0.0
 
 
     let state: WatchState
     let state: WatchState
 
 
@@ -78,7 +78,8 @@ struct BolusInputView: View {
             Spacer()
             Spacer()
 
 
             Button("Log Bolus") {
             Button("Log Bolus") {
-                navigateToConfirmation = true
+                state.bolusAmount = bolusAmount
+                navigationState.path.append(NavigationDestinations.bolusConfirm)
             }
             }
             .buttonStyle(.bordered)
             .buttonStyle(.bordered)
             .tint(.blue)
             .tint(.blue)
@@ -96,16 +97,9 @@ struct BolusInputView: View {
                     .clipShape(Circle())
                     .clipShape(Circle())
             }
             }
         }
         }
-        .navigationDestination(isPresented: $navigateToConfirmation) {
-            BolusConfirmationView(
-                bolusAmount: bolusAmount,
-                progress: $confirmationProgress,
-                state: state
-            )
-        }
     }
     }
 }
 }
 
 
 #Preview {
 #Preview {
-    BolusInputView(state: WatchState())
+    BolusInputView(navigationState: NavigationState(), state: WatchState())
 }
 }

+ 5 - 6
Trio Watch App Extension/Views/CarbsInputView.swift

@@ -4,7 +4,7 @@ import SwiftUI
 // MARK: - Carbs Input View
 // MARK: - Carbs Input View
 
 
 struct CarbsInputView: View {
 struct CarbsInputView: View {
-    @Environment(\.dismiss) var dismiss
+    @ObservedObject var navigationState: NavigationState
     @State private var carbsAmount: Double = 0.0 // Needs to be Double due to .digitalCrownRotation() stride
     @State private var carbsAmount: Double = 0.0 // Needs to be Double due to .digitalCrownRotation() stride
     @State private var navigateToBolus = false // Track navigation to BolusInputView
     @State private var navigateToBolus = false // Track navigation to BolusInputView
     @FocusState private var isCrownFocused: Bool // Manage crown focus
     @FocusState private var isCrownFocused: Bool // Manage crown focus
@@ -74,10 +74,12 @@ struct CarbsInputView: View {
             Button(buttonLabel) {
             Button(buttonLabel) {
                 if continueToBolus {
                 if continueToBolus {
                     state.carbsAmount = Int(carbsAmount)
                     state.carbsAmount = Int(carbsAmount)
-                    navigateToBolus = true
+                    navigationState.path.append(NavigationDestinations.bolusInput)
                 } else {
                 } else {
                     state.sendCarbsRequest(Int(carbsAmount))
                     state.sendCarbsRequest(Int(carbsAmount))
-                    dismiss()
+
+                    // TODO: add a fancy success animation
+                    navigationState.resetToRoot()
                 }
                 }
             }
             }
             .buttonStyle(.bordered)
             .buttonStyle(.bordered)
@@ -96,8 +98,5 @@ struct CarbsInputView: View {
                     .clipShape(Circle())
                     .clipShape(Circle())
             }
             }
         }
         }
-        .navigationDestination(isPresented: $navigateToBolus) {
-            BolusInputView(state: state) // Navigate to BolusInputView
-        }
     }
     }
 }
 }

+ 2 - 1
Trio Watch App Extension/Views/TreatmentMenuView.swift

@@ -3,6 +3,7 @@ import SwiftUI
 struct TreatmentMenuView: View {
 struct TreatmentMenuView: View {
     @Environment(\.dismiss) var dismiss
     @Environment(\.dismiss) var dismiss
     @Binding var selectedTreatment: TreatmentOption?
     @Binding var selectedTreatment: TreatmentOption?
+    var onSelect: () -> Void // Callback to handle selection and dismiss the sheet
 
 
     let treatments = TreatmentOption.allCases
     let treatments = TreatmentOption.allCases
 
 
@@ -21,7 +22,7 @@ struct TreatmentMenuView: View {
                 ForEach(treatments) { treatment in
                 ForEach(treatments) { treatment in
                     Button(action: {
                     Button(action: {
                         selectedTreatment = treatment
                         selectedTreatment = treatment
-                        dismiss() // Close after selecting
+                        onSelect()
                     }) {
                     }) {
                         HStack(spacing: 10) {
                         HStack(spacing: 10) {
                             switch treatment {
                             switch treatment {

+ 51 - 23
Trio Watch App Extension/Views/TrioMainWatchView.swift

@@ -1,7 +1,22 @@
 import Charts
 import Charts
 import SwiftUI
 import SwiftUI
 
 
+class NavigationState: ObservableObject {
+    @Published var path = NavigationPath() // Tracks the navigation stack
+
+    func resetToRoot() {
+        path.removeLast(path.count) // Clears the navigation stack to return to root
+    }
+}
+
+enum NavigationDestinations: String {
+    case carbInput
+    case bolusInput
+    case bolusConfirm
+}
+
 struct TrioMainWatchView: View {
 struct TrioMainWatchView: View {
+    @StateObject private var navigationState = NavigationState() // Shared navigation state
     @State private var state = WatchState()
     @State private var state = WatchState()
 
 
     // misc
     // misc
@@ -12,9 +27,7 @@ struct TrioMainWatchView: View {
     // view visbility
     // view visbility
     @State private var showingTreatmentMenuSheet: Bool = false
     @State private var showingTreatmentMenuSheet: Bool = false
     @State private var showingOverrideSheet: Bool = false
     @State private var showingOverrideSheet: Bool = false
-    /// navigation flags
-    @State private var navigateToCarbsInput = false
-    @State private var navigateToBolusInput = false
+    // navigation flag for meal bolus combo
     @State private var continueToBolus = false
     @State private var continueToBolus = false
 
 
     // treatments
     // treatments
@@ -27,7 +40,7 @@ struct TrioMainWatchView: View {
     )
     )
 
 
     var body: some View {
     var body: some View {
-        NavigationStack {
+        NavigationStack(path: $navigationState.path) {
             TabView(selection: $currentPage) {
             TabView(selection: $currentPage) {
                 // Page 1: Current glucose trend in "BG bobble"
                 // Page 1: Current glucose trend in "BG bobble"
                 GlucoseTrendView(state: state, rotationDegrees: rotationDegrees)
                 GlucoseTrendView(state: state, rotationDegrees: rotationDegrees)
@@ -45,6 +58,9 @@ struct TrioMainWatchView: View {
                     updateRotation(for: newTrend)
                     updateRotation(for: newTrend)
                 }
                 }
             }
             }
+            .onAppear {
+                state.confirmationProgress = 0 // reset auth progress
+            }
             .toolbar {
             .toolbar {
                 ToolbarItem(placement: .topBarLeading) {
                 ToolbarItem(placement: .topBarLeading) {
                     HStack {
                     HStack {
@@ -92,15 +108,12 @@ struct TrioMainWatchView: View {
                 }
                 }
             }
             }
             .sheet(isPresented: $showingTreatmentMenuSheet) {
             .sheet(isPresented: $showingTreatmentMenuSheet) {
-                TreatmentMenuView(selectedTreatment: $selectedTreatment)
-                    .onAppear {
-                        navigateToCarbsInput = false
-                        navigateToBolusInput = false
-                        continueToBolus = false
-                    }
-                    .onDisappear {
-                        handleTreatmentSelection()
-                    }
+                TreatmentMenuView(selectedTreatment: $selectedTreatment) {
+                    handleTreatmentSelection()
+                }
+                .onAppear {
+                    continueToBolus = false
+                }
             }
             }
             .sheet(isPresented: $showingOverrideSheet) {
             .sheet(isPresented: $showingOverrideSheet) {
                 OverridePresetsView(
                 OverridePresetsView(
@@ -114,11 +127,24 @@ struct TrioMainWatchView: View {
                     state: state
                     state: state
                 )
                 )
             }
             }
-            .navigationDestination(isPresented: $navigateToCarbsInput) {
-                CarbsInputView(state: state, continueToBolus: continueToBolus)
-            }
-            .navigationDestination(isPresented: $navigateToBolusInput) {
-                BolusInputView(state: state)
+            .navigationDestination(for: NavigationDestinations.self) { destination in
+                switch destination {
+                case .carbInput:
+                    CarbsInputView(
+                        navigationState: navigationState,
+                        state: state,
+                        continueToBolus: selectedTreatment == .mealBolusCombo
+                    )
+                case .bolusInput:
+                    BolusInputView(navigationState: navigationState, state: state)
+                case .bolusConfirm:
+                    BolusConfirmationView(
+                        navigationState: navigationState,
+                        state: state,
+                        bolusAmount: $state.bolusAmount,
+                        confirmationProgress: $state.confirmationProgress
+                    )
+                }
             }
             }
         }
         }
     }
     }
@@ -171,15 +197,17 @@ struct TrioMainWatchView: View {
     }
     }
 
 
     private func handleTreatmentSelection() {
     private func handleTreatmentSelection() {
+        showingTreatmentMenuSheet = false // Dismiss the sheet
+
         guard let treatment = selectedTreatment else { return }
         guard let treatment = selectedTreatment else { return }
+
         switch treatment {
         switch treatment {
-        case .mealBolusCombo:
-            navigateToCarbsInput = true
-            continueToBolus = true
         case .meal:
         case .meal:
-            navigateToCarbsInput = true
+            navigationState.path.append(NavigationDestinations.carbInput)
         case .bolus:
         case .bolus:
-            navigateToBolusInput = true
+            navigationState.path.append(NavigationDestinations.bolusInput)
+        case .mealBolusCombo:
+            navigationState.path.append(NavigationDestinations.carbInput)
         }
         }
     }
     }
 }
 }

+ 2 - 0
Trio Watch App Extension/WatchState.swift

@@ -26,6 +26,8 @@ import WatchConnectivity
     var carbsAmount: Int = 0
     var carbsAmount: Int = 0
     var fatAmount: Int = 0
     var fatAmount: Int = 0
     var proteinAmount: Int = 0
     var proteinAmount: Int = 0
+    var bolusAmount = 0.0
+    var confirmationProgress = 0.0
 
 
     override init() {
     override init() {
         super.init()
         super.init()