| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import Foundation
- import SwiftUI
- 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 confirmationProgress = 0.0
- let state: WatchState
- 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
- }
- 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))
- .tag(Double(number) / 10)
- }
- }
- Button("Add Bolus") {
- showingConfirmation = true
- }
- .buttonStyle(.bordered)
- .tint(.blue)
- }
- .navigationTitle("Add Insulin")
- }
- }
- }
- struct BolusConfirmationView: View {
- let bolusAmount: Double
- @Binding var progress: Double
- let state: WatchState
- let dismiss: DismissAction
- @FocusState private var isCrownFocused: Bool
- var body: some View {
- VStack(spacing: 10) {
- if state.carbsAmount > 0 {
- Text(String(format: "%.1f g", state.carbsAmount))
- .bold()
- .foregroundStyle(.orange)
- }
- Text(String(format: "%.1f U", bolusAmount))
- .bold()
- .foregroundStyle(.blue)
- ProgressView(value: progress, total: 1.0)
- .tint(progress >= 1.0 ? .green : .blue)
- .padding(.horizontal)
- Text("\(Int(progress * 100))%")
- .font(.caption2)
- .foregroundStyle(.secondary)
- }
- .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)
- }
- }
- }
- }
- #Preview {
- BolusInputView(state: WatchState())
- }
|