Selaa lähdekoodia

Small UI tweaks; move bolus confirmation to own file

Deniz Cengiz 1 vuosi sitten
vanhempi
commit
4eea4c08df

+ 93 - 0
Trio Watch App Extension/Views/BolusConfirmationView.swift

@@ -0,0 +1,93 @@
+import Foundation
+import SwiftUI
+import WatchKit
+
+struct BolusConfirmationView: View {
+    @Environment(\.dismiss) var dismiss
+
+    let bolusAmount: Double
+    @Binding var progress: Double
+    let state: WatchState
+
+    @FocusState private var isCrownFocused: Bool
+
+    var body: some View {
+        VStack(spacing: 10) {
+            Spacer()
+
+            VStack {
+                if state.carbsAmount > 0 {
+                    HStack {
+                        Text("Carbs:")
+                        Spacer()
+                        Text(String(format: "%.1f g", state.carbsAmount))
+                            .bold()
+                            .foregroundStyle(.orange)
+                    }.padding(.horizontal)
+                }
+
+                HStack {
+                    Text("Bolus")
+                    Spacer()
+                    Text(String(format: "%.1f U", bolusAmount))
+                        .bold()
+                        .foregroundStyle(.blue)
+                }.padding(.horizontal)
+            }
+
+            ProgressView(value: progress, total: 1.0)
+                .tint(progress >= 1.0 ? .green : .gray)
+                .padding(.horizontal)
+
+            Spacer()
+
+            Button("Cancel") {
+                if state.carbsAmount > 0 {
+                    state.carbsAmount = 0 // reset carbs in state
+                }
+                dismiss()
+            }
+            .buttonStyle(.bordered)
+            .tint(.blue)
+            .disabled(!(bolusAmount > 0.0))
+        }
+        .focusable(true)
+        .focused($isCrownFocused)
+        .digitalCrownRotation(
+            $progress,
+            from: 0.0,
+            through: 1.0,
+            by: 0.05,
+            sensitivity: .medium,
+            isContinuous: false,
+            isHapticFeedbackEnabled: true
+        )
+        .onAppear {
+            isCrownFocused = true
+        }
+        .onChange(of: progress) { _, newValue in
+            if newValue >= 1.0 {
+                WKInterfaceDevice.current().play(.success)
+
+                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
+                    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 {
+                WKInterfaceDevice.current().play(.click)
+            }
+        }
+        .navigationTitle("Confirm")
+        .toolbar {
+            ToolbarItem(placement: .topBarTrailing) {
+                Image(systemName: "digitalcrown.arrow.counterclockwise.fill")
+                    .symbolRenderingMode(.hierarchical)
+                    .foregroundStyle(Color.white)
+            }
+        }
+    }
+}

+ 73 - 163
Trio Watch App Extension/Views/BolusInputView.swift

@@ -5,9 +5,8 @@ import WatchKit
 // MARK: - Bolus Input View
 
 struct BolusInputView: View {
-    @Environment(\.dismiss) var dismiss
     @State private var bolusAmount = 0.0
-    @State private var showingConfirmation = false
+    @State private var navigateToConfirmation = false
     @State private var confirmationProgress = 0.0
 
     let state: WatchState
@@ -15,184 +14,95 @@ struct BolusInputView: View {
     @FocusState private var isCrownFocused: Bool
 
     var body: some View {
-        if showingConfirmation {
-            BolusConfirmationView(
-                bolusAmount: bolusAmount,
-                progress: $confirmationProgress,
-                state: state,
-                dismiss: dismiss
-            )
-            .navigationTitle("Confirm")
-            .navigationBarBackButtonHidden(true)
-            .toolbar {
-                ToolbarItem(placement: .topBarLeading) {
-                    Button {
-                        if state.carbsAmount > 0 {
-                            state.carbsAmount = 0 // reset carbs in state
-                        }
-                        showingConfirmation.toggle()
-                    } label: {
-                        Image(systemName: "xmark")
-                    }
-                    .buttonStyle(.bordered)
-                    .clipShape(Circle())
-                }
-
-                ToolbarItem(placement: .topBarTrailing) {
-                    Image(systemName: "digitalcrown.arrow.counterclockwise.fill")
-                        .symbolRenderingMode(.hierarchical)
-                        .foregroundStyle(Color.white)
-                }
-            }
-        } else {
-            VStack {
-                if state.carbsAmount > 0 {
-                    HStack {
-                        Text("Carbs:").bold().font(.subheadline).padding(.leading)
-                        Text(String(format: "%.0f g", state.carbsAmount)).font(.subheadline).foregroundStyle(Color.orange)
-                        Spacer()
-                    }
-                }
-
-                Spacer()
-
+        VStack {
+            if state.carbsAmount > 0 {
                 HStack {
-                    // "-" Button
-                    Button(action: {
-                        if bolusAmount > 0 { bolusAmount -= 1 }
-                    }) {
-                        Image(systemName: "minus.circle.fill")
-                            .font(.title3)
-                            .foregroundColor(.blue)
-                    }
-                    .buttonStyle(.borderless)
-                    .disabled(bolusAmount < 1)
-
+                    Text("Carbs:").bold().font(.subheadline).padding(.leading)
+                    Text(String(format: "%.0f g", state.carbsAmount)).font(.subheadline).foregroundStyle(Color.orange)
                     Spacer()
+                }
+            }
 
-                    // Display the current carb amount
-                    Text(String(format: "%.2f U", bolusAmount))
-                        .fontWeight(.bold)
-                        .font(.system(.title2, design: .rounded))
-                        .foregroundColor(.primary)
-                        .focusable(true)
-                        .focused($isCrownFocused)
-                        .digitalCrownRotation(
-                            $bolusAmount,
-                            from: 0,
-                            through: 150.0, // TODO: use maxBolus here
-                            by: 1, // TODO: use pump increment here
-                            sensitivity: .medium,
-                            isContinuous: false,
-                            isHapticFeedbackEnabled: true
-                        )
+            Spacer()
 
-                    Spacer()
+            HStack {
+                // "-" Button
+                Button(action: {
+                    if bolusAmount > 0 { bolusAmount -= 1 }
+                }) {
+                    Image(systemName: "minus.circle.fill")
+                        .font(.title3)
+                        .foregroundColor(.blue)
+                }
+                .buttonStyle(.borderless)
+                .disabled(bolusAmount < 1)
 
-                    // TODO: introduce maxBolus here, disable button if bolusAmount > maxBolus
-                    // "+" Button
-                    Button(action: {
-                        bolusAmount += 1
-                    }) {
-                        Image(systemName: "plus.circle.fill")
-                            .font(.title3)
-                            .foregroundColor(.blue)
-                    }
-                    .buttonStyle(.borderless)
-                }.padding(.horizontal)
+                Spacer()
 
-                Text("Insulin")
-                    .font(.subheadline)
-                    .foregroundColor(.secondary)
-                    .padding(.bottom)
+                // Display the current carb amount
+                Text(String(format: "%.2f U", bolusAmount))
+                    .fontWeight(.bold)
+                    .font(.system(.title2, design: .rounded))
+                    .foregroundColor(.primary)
+                    .focusable(true)
+                    .focused($isCrownFocused)
+                    .digitalCrownRotation(
+                        $bolusAmount,
+                        from: 0,
+                        through: 150.0, // TODO: use maxBolus here
+                        by: 1, // TODO: use pump increment here
+                        sensitivity: .medium,
+                        isContinuous: false,
+                        isHapticFeedbackEnabled: true
+                    )
 
                 Spacer()
 
-                Button("Log Bolus") {
-                    showingConfirmation = true
+                // TODO: introduce maxBolus here, disable button if bolusAmount > maxBolus
+                // "+" Button
+                Button(action: {
+                    bolusAmount += 1
+                }) {
+                    Image(systemName: "plus.circle.fill")
+                        .font(.title3)
+                        .foregroundColor(.blue)
                 }
-                .buttonStyle(.bordered)
-                .tint(.blue)
-                .disabled(!(bolusAmount > 0.0))
-            }
-            .toolbar {
-                ToolbarItem(placement: .topBarTrailing) {
-                    Image(systemName: "syringe.fill")
-                        .resizable()
-                        .aspectRatio(contentMode: .fit)
-                        .frame(width: 14, height: 14)
-                        .padding()
-                        .background(Color.blue)
-                        .foregroundStyle(.white)
-                        .clipShape(Circle())
-                }
-            }
-        }
-    }
-}
+                .buttonStyle(.borderless)
+            }.padding(.horizontal)
 
-struct BolusConfirmationView: View {
-    let bolusAmount: Double
-    @Binding var progress: Double
-    let state: WatchState
-    let dismiss: DismissAction
+            Text("Insulin")
+                .font(.subheadline)
+                .foregroundColor(.secondary)
+                .padding(.bottom)
 
-    @FocusState private var isCrownFocused: Bool
+            Spacer()
 
-    var body: some View {
-        VStack(spacing: 10) {
-            if state.carbsAmount > 0 {
-                HStack {
-                    Text("Carbs:")
-                    Spacer()
-                    Text(String(format: "%.1f g", state.carbsAmount))
-                        .bold()
-                        .foregroundStyle(.orange)
-                }.padding(.horizontal)
+            Button("Log Bolus") {
+                navigateToConfirmation = true
             }
-
-            HStack {
-                Text("Bolus")
-                Spacer()
-                Text(String(format: "%.1f U", bolusAmount))
-                    .bold()
-                    .foregroundStyle(.blue)
-            }.padding(.horizontal)
-
-            ProgressView(value: progress, total: 1.0)
-                .tint(progress >= 1.0 ? .green : .gray)
-                .padding(.horizontal)
+            .buttonStyle(.bordered)
+            .tint(.blue)
+            .disabled(!(bolusAmount > 0.0))
         }
-        .focusable(true)
-        .focused($isCrownFocused)
-        .digitalCrownRotation(
-            $progress,
-            from: 0.0,
-            through: 1.0,
-            by: 0.05,
-            sensitivity: .medium,
-            isContinuous: false,
-            isHapticFeedbackEnabled: true
-        )
-        .onAppear {
-            isCrownFocused = true
-        }
-        .onChange(of: progress) { _, newValue in
-            if newValue >= 1.0 {
-                WKInterfaceDevice.current().play(.success)
-
-                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
-                    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 {
-                WKInterfaceDevice.current().play(.click)
+        .toolbar {
+            ToolbarItem(placement: .topBarTrailing) {
+                Image(systemName: "syringe.fill")
+                    .resizable()
+                    .aspectRatio(contentMode: .fit)
+                    .frame(width: 14, height: 14)
+                    .padding()
+                    .background(Color.blue)
+                    .foregroundStyle(.white)
+                    .clipShape(Circle())
             }
         }
+        .navigationDestination(isPresented: $navigateToConfirmation) {
+            BolusConfirmationView(
+                bolusAmount: bolusAmount,
+                progress: $confirmationProgress,
+                state: state
+            )
+        }
     }
 }
 

+ 4 - 0
Trio.xcodeproj/project.pbxproj

@@ -464,6 +464,7 @@
 		DD6B7CBB2C7FBBFA00B75029 /* ReviewInsulinActionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6B7CBA2C7FBBFA00B75029 /* ReviewInsulinActionView.swift */; };
 		DD6D67E42C9C253500660C9B /* ColorSchemeOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6D67E32C9C253500660C9B /* ColorSchemeOption.swift */; };
 		DD6F63CC2D27F615007D94CF /* TreatmentMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6F63CB2D27F606007D94CF /* TreatmentMenuView.swift */; };
+		DD8262CB2D289297009F6F62 /* BolusConfirmationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8262CA2D289297009F6F62 /* BolusConfirmationView.swift */; };
 		DD88C8E22C50420800F2D558 /* DefinitionRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD88C8E12C50420800F2D558 /* DefinitionRow.swift */; };
 		DD940BAA2CA7585D000830A5 /* GlucoseColorScheme.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD940BA92CA7585D000830A5 /* GlucoseColorScheme.swift */; };
 		DD940BAC2CA75889000830A5 /* DynamicGlucoseColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD940BAB2CA75889000830A5 /* DynamicGlucoseColor.swift */; };
@@ -1175,6 +1176,7 @@
 		DD6B7CBA2C7FBBFA00B75029 /* ReviewInsulinActionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReviewInsulinActionView.swift; sourceTree = "<group>"; };
 		DD6D67E32C9C253500660C9B /* ColorSchemeOption.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorSchemeOption.swift; sourceTree = "<group>"; };
 		DD6F63CB2D27F606007D94CF /* TreatmentMenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TreatmentMenuView.swift; sourceTree = "<group>"; };
+		DD8262CA2D289297009F6F62 /* BolusConfirmationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BolusConfirmationView.swift; sourceTree = "<group>"; };
 		DD88C8E12C50420800F2D558 /* DefinitionRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefinitionRow.swift; sourceTree = "<group>"; };
 		DD940BA92CA7585D000830A5 /* GlucoseColorScheme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlucoseColorScheme.swift; sourceTree = "<group>"; };
 		DD940BAB2CA75889000830A5 /* DynamicGlucoseColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicGlucoseColor.swift; sourceTree = "<group>"; };
@@ -2472,6 +2474,7 @@
 		BDA25F1A2D26BCE800035F34 /* Views */ = {
 			isa = PBXGroup;
 			children = (
+				DD8262CA2D289297009F6F62 /* BolusConfirmationView.swift */,
 				BDA25F212D26D62200035F34 /* BolusInputView.swift */,
 				BDA25F1F2D26D5FB00035F34 /* CarbsInputView.swift */,
 				BDA25F1D2D26D5D800035F34 /* GlucoseChartView.swift */,
@@ -3979,6 +3982,7 @@
 				BDA25F1E2D26D5DD00035F34 /* GlucoseChartView.swift in Sources */,
 				DD6F63CC2D27F615007D94CF /* TreatmentMenuView.swift in Sources */,
 				DD246F062D2836AA0027DDE0 /* GlucoseTrendView.swift in Sources */,
+				DD8262CB2D289297009F6F62 /* BolusConfirmationView.swift in Sources */,
 				BD54A9712D281A8100F9C1EE /* TempTargetPresetsView.swift in Sources */,
 				BDA25EE62D260D5E00035F34 /* WatchState.swift in Sources */,
 			);