From 34ae2ead40123f8c1dc53eb0034e9fec66c85b38 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 24 Sep 2024 02:16:13 +0200 Subject: [PATCH] Make snackbar appear when state changes in BLoC --- lib/food_entry/food_entry_bloc.dart | 23 ++++++++++++++++++++++- lib/perdate/perdate_widget.dart | 13 +++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/food_entry/food_entry_bloc.dart b/lib/food_entry/food_entry_bloc.dart index 207db42..35baaf8 100644 --- a/lib/food_entry/food_entry_bloc.dart +++ b/lib/food_entry/food_entry_bloc.dart @@ -47,6 +47,12 @@ class FoodEntryBloc extends Bloc { if (scanResult.type == ResultType.Cancelled) { return; } + if (scanResult.type == ResultType.Error) { + emit(PageState( + foodEntries: state.foodEntries, + errorString: "Fehler beim Scannen des Barcodes")); + return; + } var responseFuture = client.retrieveFoodInfo(scanResult.rawContent); List newList = List.from(state.foodEntries); @@ -58,6 +64,20 @@ class FoodEntryBloc extends Bloc { await responseFuture.then((response) { newList.removeWhere((entryState) => entryState.id == newEntryWaiting.id); + if (response.status == FoodFactResponseStatus.barcodeNotFound) { + emit(PageState( + foodEntries: state.foodEntries, + errorString: "Barcode konnte nicht gefunden werden.")); + return; + } + if (response.status == + FoodFactResponseStatus.foodFactServerNotReachable) { + emit(PageState( + foodEntries: state.foodEntries, + errorString: "OpenFoodFacts-Server konnte nicht erreicht werden.")); + return; + } + var newEntryFinishedWaiting = FoodEntryState( name: response.food?.name ?? "", mass: response.food?.mass ?? 0, @@ -93,8 +113,9 @@ class BarcodeScanned extends FoodEvent { /// This is the state for one date/page class PageState { final List foodEntries; + final String? errorString; - PageState({required this.foodEntries}); + PageState({required this.foodEntries, this.errorString}); factory PageState.init() { return PageState(foodEntries: []); diff --git a/lib/perdate/perdate_widget.dart b/lib/perdate/perdate_widget.dart index 7bdc396..b6f41f8 100644 --- a/lib/perdate/perdate_widget.dart +++ b/lib/perdate/perdate_widget.dart @@ -56,19 +56,24 @@ class _PerDateWidgetState extends State { ), ) ], - child: BlocBuilder( - builder: (context, state) { + child: BlocConsumer( + listener: (context, pageState) { + if (pageState.errorString != null) { + showNewSnackbarWith(context, pageState.errorString!); + } + }, builder: (context, pageState) { return Scaffold( appBar: AppBar( title: Text(DateFormat.yMMMMd('de').format(widget.date)), actions: const [ThemeSwitcherButton()], ), - body: FoodEntryList(entries: state.foodEntries), + body: FoodEntryList(entries: pageState.foodEntries), bottomNavigationBar: BottomAppBar( shape: const RectangularNotchShape(), color: Theme.of(context).colorScheme.secondary, - child: SumWidget(foodEntries: state.foodEntries)), + child: + SumWidget(foodEntries: pageState.foodEntries)), drawer: const AppDrawer(), floatingActionButton: OverflowBar(children: [ ScanFoodFloatingButton(