Skip to content

Commit 0446b33

Browse files
authored
Fix alert presentation on watch (#65)
* [LOOP 993] Unify carb + bolus flow on watch * Reset bolus amount when returning to carb entry * Updates from review * Fix alert state
1 parent 5399d46 commit 0446b33

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

WatchApp Extension/Views/Carb Entry & Bolus/CarbAndBolusFlow.swift

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ struct CarbAndBolusFlow: View {
2323
case bolusConfirmation
2424
}
2525

26+
fileprivate enum AlertState {
27+
case bolusRecommendationChanged
28+
case communicationError(CarbAndBolusFlowViewModel.Error)
29+
}
30+
2631
// MARK: - State
2732
@State private var flowState: FlowState
2833
@ObservedObject private var viewModel: CarbAndBolusFlowViewModel
@@ -37,7 +42,7 @@ struct CarbAndBolusFlow: View {
3742
// MARK: - State: Bolus Entry
3843
@State private var bolusAmount: Double = 0
3944
@State private var receivedInitialBolusRecommendation = false
40-
@State private var showingRecommendationChangedAlert = false
45+
@State private var activeAlert: AlertState?
4146

4247
// MARK: - State: Bolus Confirmation
4348
@State private var bolusConfirmationProgress: Double = 0
@@ -71,10 +76,10 @@ struct CarbAndBolusFlow: View {
7176

7277
// Handle incoming bolus recommendations.
7378
.onReceive(viewModel.$recommendedBolusAmount, perform: handleNewBolusRecommendation)
74-
.alert(isPresented: $showingRecommendationChangedAlert, content: recommendedBolusUpdatedAlert)
7579

7680
// Handle error states.
77-
.alert(item: $viewModel.error, content: communicationErrorAlert(for:))
81+
.onReceive(viewModel.$error) { self.activeAlert = $0.map(AlertState.communicationError) }
82+
.alert(item: $activeAlert, content: alert(for:))
7883
}
7984
}
8085

@@ -308,14 +313,23 @@ extension CarbAndBolusFlow {
308313
}
309314

310315
bolusAmount = recommendedBolus ?? 0
311-
showingRecommendationChangedAlert = true
316+
activeAlert = .bolusRecommendationChanged
312317
}
313318
}
314319

315-
private func recommendedBolusUpdatedAlert() -> Alert {
320+
private func alert(for activeAlert: AlertState) -> Alert {
321+
switch activeAlert {
322+
case .bolusRecommendationChanged:
323+
return recommendedBolusUpdatedAlert
324+
case .communicationError(let error):
325+
return communicationErrorAlert(for: error)
326+
}
327+
}
328+
329+
private var recommendedBolusUpdatedAlert: Alert {
316330
Alert(
317331
title: Text("Bolus Recommendation Updated", comment: "Alert title for updated bolus recommendation on Apple Watch"),
318-
message: Text("The bolus recommendation has updated. Please reconfirm the bolus amount.", comment: "Alert message for updated bolus recommendation on Apple Watch"),
332+
message: Text("Please reconfirm the bolus amount.", comment: "Alert message for updated bolus recommendation on Apple Watch"),
319333
dismissButton: .default(Text("OK"))
320334
)
321335
}
@@ -336,3 +350,7 @@ extension CarbAndBolusFlow {
336350
)
337351
}
338352
}
353+
354+
extension CarbAndBolusFlow.AlertState: Hashable, Identifiable {
355+
var id: Self { self }
356+
}

0 commit comments

Comments
 (0)