소스 검색

Link treatment context with entry views

Deniz Cengiz 1 년 전
부모
커밋
026444f892

+ 45 - 22
Trio Watch App Extension/Views/BolusInputView.swift

@@ -7,23 +7,50 @@ import WatchKit
 struct BolusInputView: View {
     @Environment(\.dismiss) var dismiss
     @State private var bolusAmount = 0.0
-    @State private var isExternalInsulin = false
     @State private var showingConfirmation = false
     @State private var confirmationProgress = 0.0
     let state: WatchState
 
     var body: some View {
-        NavigationView {
+        NavigationStack {
             if showingConfirmation {
                 BolusConfirmationView(
-                    amount: bolusAmount,
-                    isExternal: isExternalInsulin,
+                    bolusAmount: bolusAmount,
                     progress: $confirmationProgress,
                     state: state,
                     dismiss: dismiss
                 )
+                .navigationTitle("Confirm")
+                .navigationBarBackButtonHidden(true)
+                .toolbar {
+                    ToolbarItem(placement: .topBarLeading) {
+                        Button {
+                            if state.carbsAmount > 0 {
+                                state.carbsAmount = 0
+                            }
+                            dismiss()
+                        } label: {
+                            Image(systemName: "xmark")
+                        }
+                        .buttonStyle(.bordered)
+                        .clipShape(Circle())
+                    }
+
+                    ToolbarItem(placement: .topBarTrailing) {
+                        Image(systemName: "digitalcrown.arrow.counterclockwise.fill")
+                            .foregroundStyle(Color.white)
+                    }
+                }
             } else {
                 VStack {
+                    if state.carbsAmount > 0 {
+                        HStack {
+                            Text("Carbs: \(state.carbsAmount) g").font(.subheadline).padding(.bottom)
+                            Spacer()
+                        }
+                    }
+
+                    // TODO: handle bolus recommendation
                     Picker("Bolus", selection: $bolusAmount) {
                         ForEach(0 ... 100, id: \.self) { number in
                             Text(String(format: "%.1f U", Double(number) / 10))
@@ -31,11 +58,7 @@ struct BolusInputView: View {
                         }
                     }
 
-                    Toggle("External Insulin", isOn: $isExternalInsulin)
-                        .toggleStyle(.switch)
-                        .padding(.horizontal)
-
-                    Button(isExternalInsulin ? "Add External Insulin" : "Add Bolus") {
+                    Button("Add Bolus") {
                         showingConfirmation = true
                     }
                     .buttonStyle(.bordered)
@@ -48,8 +71,7 @@ struct BolusInputView: View {
 }
 
 struct BolusConfirmationView: View {
-    let amount: Double
-    let isExternal: Bool
+    let bolusAmount: Double
     @Binding var progress: Double
     let state: WatchState
     let dismiss: DismissAction
@@ -58,17 +80,15 @@ struct BolusConfirmationView: View {
 
     var body: some View {
         VStack(spacing: 10) {
-            Text("Confirm \(isExternal ? "External Insulin" : "Bolus")")
-                .font(.headline)
+            if state.carbsAmount > 0 {
+                Text(String(format: "%.1f g", state.carbsAmount))
+                    .bold()
+                    .foregroundStyle(.orange)
+            }
 
-            Text(String(format: "%.1f U", amount))
-                .font(.title2)
+            Text(String(format: "%.1f U", bolusAmount))
                 .bold()
-
-            Text("Scroll crown down\nto confirm")
-                .multilineTextAlignment(.center)
-                .font(.caption2)
-                .foregroundStyle(.secondary)
+                .foregroundStyle(.blue)
 
             ProgressView(value: progress, total: 1.0)
                 .tint(progress >= 1.0 ? .green : .blue)
@@ -78,7 +98,6 @@ struct BolusConfirmationView: View {
                 .font(.caption2)
                 .foregroundStyle(.secondary)
         }
-        .navigationBarBackButtonHidden(true)
         .focusable(true)
         .focused($isCrownFocused)
         .digitalCrownRotation(
@@ -98,7 +117,11 @@ struct BolusConfirmationView: View {
                 WKInterfaceDevice.current().play(.success)
 
                 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
-                    state.sendBolusRequest(Decimal(amount), isExternal: isExternal)
+                    if state.carbsAmount > 0 {
+                        state.sendCarbsRequest(state.carbsAmount, Date())
+                        state.carbsAmount = 0 // reset carbs in state
+                    }
+                    state.sendBolusRequest(Decimal(bolusAmount))
                     dismiss()
                 }
             } else if newValue > 0 {

+ 18 - 4
Trio Watch App Extension/Views/CarbsInputView.swift

@@ -6,10 +6,16 @@ import SwiftUI
 struct CarbsInputView: View {
     @Environment(\.dismiss) var dismiss
     @State private var carbsAmount = 0
+    @State private var navigateToBolus = false // Track navigation to BolusInputView
+
     let state: WatchState
+    let continueToBolus: Bool
 
     var body: some View {
-        NavigationView {
+        let buttonLabel = continueToBolus ? "Continue to Bolus" : "Add Carbs"
+
+        // TODO: introduce meal setting fpu enablement to conditional handle FPU
+        NavigationStack {
             VStack {
                 Picker("Carbs", selection: $carbsAmount) {
                     ForEach(0 ... 100, id: \.self) { amount in
@@ -17,14 +23,22 @@ struct CarbsInputView: View {
                     }
                 }
 
-                Button("Add Carbs") {
-                    state.sendCarbsRequest(carbsAmount)
-                    dismiss()
+                Button(buttonLabel) {
+                    if continueToBolus {
+                        state.carbsAmount = carbsAmount
+                        navigateToBolus = true
+                    } else {
+                        state.sendCarbsRequest(carbsAmount)
+                        dismiss()
+                    }
                 }
                 .buttonStyle(.bordered)
                 .tint(.orange)
             }
             .navigationTitle("Add Carbs")
+            .navigationDestination(isPresented: $navigateToBolus) {
+                BolusInputView(state: state) // Navigate to BolusInputView
+            }
         }
     }
 }

+ 3 - 6
Trio Watch App Extension/Views/GlucoseTrendView.swift

@@ -14,6 +14,8 @@ struct GlucoseTrendView: View {
             ZStack {
                 TrendShape(rotationDegrees: rotationDegrees, isSmallDevice: is40mm)
                     .animation(.spring(response: 0.5, dampingFraction: 0.6), value: rotationDegrees)
+                    // TODO: set loop colors conditionally, not hard coded
+                    .shadow(color: .green, radius: is40mm ? 8 : 12)
 
                 VStack(alignment: .center) {
                     Text(state.currentGlucose)
@@ -31,12 +33,7 @@ struct GlucoseTrendView: View {
 
             Spacer()
 
-            // TODO: set loop colors conditionally, not hard coded
-            HStack {
-                Image(systemName: "circle")
-                    .foregroundStyle(.green)
-                Text(state.lastLoopTime ?? "--")
-            }.font(is40mm ? .footnote : .caption)
+            Text(state.lastLoopTime ?? "--").font(is40mm ? .footnote : .caption)
 
             Spacer()
 

+ 6 - 7
Trio Watch App Extension/Views/TreatmentMenuView.swift

@@ -2,15 +2,16 @@ import SwiftUI
 
 struct TreatmentMenuView: View {
     @Environment(\.presentationMode) var presentationMode
-    let treatments = TreatmentOptions.allCases
-    @State private var selectedOption: TreatmentOptions? = nil
+    @Binding var selectedTreatment: TreatmentOption?
+
+    let treatments = TreatmentOption.allCases
 
     var body: some View {
         NavigationView {
             List {
                 ForEach(treatments) { treatment in
                     Button(action: {
-                        selectedOption = treatment
+                        selectedTreatment = treatment
                         presentationMode.wrappedValue.dismiss() // Close after selecting
                     }) {
                         HStack(alignment: .center) {
@@ -37,10 +38,8 @@ struct TreatmentMenuView: View {
                     }
                     .buttonStyle(PressableIconButtonStyle())
                 }.listRowBackground(Color.clear)
-            }
-            .navigationTitle("Pick Treatment")
+            }.navigationTitle("Pick Treatment")
         }
-        .background(.clear)
     }
 
     var mealIcon: some View {
@@ -64,7 +63,7 @@ struct TreatmentMenuView: View {
     }
 }
 
-enum TreatmentOptions: String, CaseIterable, Identifiable {
+enum TreatmentOption: String, CaseIterable, Identifiable {
     var id: String { rawValue }
 
     case mealBolusCombo

+ 33 - 10
Trio Watch App Extension/Views/TrioMainWatchView.swift

@@ -11,12 +11,13 @@ struct TrioMainWatchView: View {
 
     // view visbility
     @State private var showingTreatmentMenuSheet: Bool = false
-    @State private var showingCarbsInputView: Bool = false
-    @State private var showingInsulinInputView: Bool = false
     @State private var showingOverrideSheet: Bool = false
+    @State private var navigateToCarbsInput = false
+    @State private var navigateToBolusInput = false
+    @State private var continueToBolus = false
 
     // treatments
-    @State private var selectedTreatment: TreatmentOptions?
+    @State private var selectedTreatment: TreatmentOption?
 
     private var trioBackgroundColor = LinearGradient(
         gradient: Gradient(colors: [Color.bgDarkBlue, Color.bgDarkerDarkBlue]),
@@ -90,7 +91,10 @@ struct TrioMainWatchView: View {
                 }
             }
             .sheet(isPresented: $showingTreatmentMenuSheet) {
-                TreatmentMenuView()
+                TreatmentMenuView(selectedTreatment: $selectedTreatment)
+                    .onDisappear {
+                        handleTreatmentSelection()
+                    }
             }
             .sheet(isPresented: $showingOverrideSheet) {
                 OverridePresetsView(
@@ -98,12 +102,18 @@ struct TrioMainWatchView: View {
                     state: state
                 )
             }
-        }
-        .sheet(isPresented: $showingTempTargetSheet) {
-            TempTargetPresetsView(
-                tempTargetPresets: state.tempTargetPresets,
-                state: state
-            )
+            .sheet(isPresented: $showingTempTargetSheet) {
+                TempTargetPresetsView(
+                    tempTargetPresets: state.tempTargetPresets,
+                    state: state
+                )
+            }
+            .navigationDestination(isPresented: $navigateToCarbsInput) {
+                CarbsInputView(state: state, continueToBolus: continueToBolus)
+            }
+            .navigationDestination(isPresented: $navigateToBolusInput) {
+                BolusInputView(state: state)
+            }
         }
     }
 
@@ -153,6 +163,19 @@ struct TrioMainWatchView: View {
             rotationDegrees = 0
         }
     }
+
+    private func handleTreatmentSelection() {
+        guard let treatment = selectedTreatment else { return }
+        switch treatment {
+        case .mealBolusCombo:
+            navigateToCarbsInput = true
+            continueToBolus = true
+        case .meal:
+            navigateToCarbsInput = true
+        case .bolus:
+            navigateToBolusInput = true
+        }
+    }
 }
 
 extension Binding where Value == Int {

+ 8 - 3
Trio Watch App Extension/WatchState.swift

@@ -21,6 +21,12 @@ import WatchConnectivity
     var overridePresets: [OverridePresetWatch] = []
     var tempTargetPresets: [TempTargetPresetWatch] = []
 
+    /// treatments inputs
+    /// used to store carbs for combined meal-bolus-treatments
+    var carbsAmount: Int = 0
+    var fatAmount: Int = 0
+    var proteinAmount: Int = 0
+
     override init() {
         super.init()
         setupSession()
@@ -44,12 +50,11 @@ import WatchConnectivity
     /// - Parameters:
     ///   - amount: The insulin amount to be delivered
     ///   - isExternal: Indicates if the bolus is from an external source
-    func sendBolusRequest(_ amount: Decimal, isExternal: Bool) {
+    func sendBolusRequest(_ amount: Decimal) {
         guard let session = session, session.isReachable else { return }
 
         let message: [String: Any] = [
-            "bolus": amount,
-            "isExternal": isExternal
+            "bolus": amount
         ]
 
         session.sendMessage(message, replyHandler: nil) { error in