diff --git a/lib/food_entry/enter_food_widget.dart b/lib/food_entry/enter_food_widget.dart index 031707c..bbfe0d3 100644 --- a/lib/food_entry/enter_food_widget.dart +++ b/lib/food_entry/enter_food_widget.dart @@ -1,4 +1,4 @@ -import 'package:calorimeter/perdate/perdate_widget.dart'; +import 'package:calorimeter/utils/enter_food_controller.dart'; import 'package:calorimeter/storage/storage.dart'; import 'package:flutter/material.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart'; diff --git a/lib/food_scan/food_fact_lookup.dart b/lib/food_scan/food_fact_lookup.dart index 1a0e00b..2d1667f 100644 --- a/lib/food_scan/food_fact_lookup.dart +++ b/lib/food_scan/food_fact_lookup.dart @@ -22,7 +22,7 @@ class FoodFactLookupClient { if (response.statusCode != HttpStatus.ok) { return FoodFactResponse( - food: null, status: FoodFactResponseStatus.foodNotFound); + food: null, status: FoodFactResponseStatus.barcodeNotFound); } asString = await response.transform(utf8.decoder).join(); @@ -57,7 +57,7 @@ class FoodFactModel { enum FoodFactResponseStatus { foodFactServerNotReachable, - foodNotFound, + barcodeNotFound, ok, } diff --git a/lib/perdate/perdate_widget.dart b/lib/perdate/perdate_widget.dart index c3cbb12..2dfd4e8 100644 --- a/lib/perdate/perdate_widget.dart +++ b/lib/perdate/perdate_widget.dart @@ -1,7 +1,6 @@ -import 'dart:developer'; - import 'package:barcode_scan2/barcode_scan2.dart'; import 'package:calorimeter/food_scan/food_fact_lookup.dart'; +import 'package:calorimeter/utils/enter_food_controller.dart'; import 'package:calorimeter/utils/scan_food_floating_button.dart'; import 'package:calorimeter/utils/app_drawer.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart'; @@ -31,7 +30,6 @@ class _PerDateWidgetState extends State { @override void initState() { - log("PerDateWidgetState's initState()"); storage = FoodStorage.getInstance(); entriesFuture = storage.getEntriesForDate(widget.date); entriesFuture.then((val) { @@ -42,7 +40,6 @@ class _PerDateWidgetState extends State { @override Widget build(BuildContext context) { - log("PerDateWidgetState's build()"); return FutureBuilder( future: entriesFuture, builder: (context, snapshot) { @@ -80,15 +77,16 @@ class _PerDateWidgetState extends State { var client = FoodFactLookupClient(); var scanResult = await BarcodeScanner.scan(); - if (!context.mounted) return; + if (scanResult.type == ResultType.Cancelled) { return; } + + if (!context.mounted) return; + if (scanResult.type == ResultType.Error) { - var snackbar = const SnackBar( - content: Text("Error scanning barcode")); - ScaffoldMessenger.of(context) - .showSnackBar(snackbar); + showNewSnackbarWith(context, + "Fehler beim Scannen des Barcodes."); } var response = await client .retrieveFoodInfo(scanResult.rawContent); @@ -96,21 +94,18 @@ class _PerDateWidgetState extends State { if (!context.mounted) return; if (response.status == - FoodFactResponseStatus.foodNotFound) { - var snackbar = const SnackBar( - content: Text("Food not found")); - ScaffoldMessenger.of(context) - .showSnackBar(snackbar); + FoodFactResponseStatus.barcodeNotFound) { + showNewSnackbarWith(context, + "Barcode konnte nicht gefunden werden."); + return; } if (response.status == FoodFactResponseStatus .foodFactServerNotReachable) { - var snackbar = const SnackBar( - content: - Text("FoodFact server not reachable")); - ScaffoldMessenger.of(context) - .showSnackBar(snackbar); + showNewSnackbarWith(context, + "OpenFoodFacts-Server konnte nicht erreicht werden."); + return; } context.read().set( @@ -139,15 +134,24 @@ class _PerDateWidgetState extends State { ); }); } -} -class EnterFoodController extends ChangeNotifier { - String name = ""; - String kcalPer100g = ""; + void showNewSnackbarWith(BuildContext context, String text) { + var snackbar = + ErrorSnackbar(colorScheme: Theme.of(context).colorScheme, text: text); - void set(String newName, String newKcal) { - name = newName; - kcalPer100g = newKcal; - notifyListeners(); + ScaffoldMessenger.of(context).clearSnackBars(); + ScaffoldMessenger.of(context).showSnackBar(snackbar); } } + +class ErrorSnackbar extends SnackBar { + final String text; + final ColorScheme colorScheme; + ErrorSnackbar({ + required this.text, + required this.colorScheme, + super.key, + }) : super( + content: Text(text, style: TextStyle(color: colorScheme.onError)), + backgroundColor: colorScheme.error); +} diff --git a/lib/utils/enter_food_controller.dart b/lib/utils/enter_food_controller.dart new file mode 100644 index 0000000..44a998f --- /dev/null +++ b/lib/utils/enter_food_controller.dart @@ -0,0 +1,12 @@ +import 'package:flutter/material.dart'; + +class EnterFoodController extends ChangeNotifier { + String name = ""; + String kcalPer100g = ""; + + void set(String newName, String newKcal) { + name = newName; + kcalPer100g = newKcal; + notifyListeners(); + } +}