2024-09-07 23:57:40 +00:00
|
|
|
import 'package:barcode_scan2/barcode_scan2.dart';
|
|
|
|
import 'package:calorimeter/food_scan/food_fact_lookup.dart';
|
2024-09-08 13:07:39 +00:00
|
|
|
import 'package:calorimeter/utils/enter_food_controller.dart';
|
2024-09-06 23:38:03 +00:00
|
|
|
import 'package:calorimeter/utils/scan_food_floating_button.dart';
|
2024-09-06 16:51:24 +00:00
|
|
|
import 'package:calorimeter/utils/app_drawer.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
2024-09-06 16:51:24 +00:00
|
|
|
import 'package:calorimeter/perdate/entry_list.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/storage/storage.dart';
|
2024-09-06 16:51:24 +00:00
|
|
|
import 'package:calorimeter/utils/calendar_floating_button.dart';
|
|
|
|
import 'package:calorimeter/utils/rectangular_notch_shape.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/utils/sum_widget.dart';
|
|
|
|
import 'package:calorimeter/utils/theme_switcher_button.dart';
|
2024-09-06 11:48:56 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
2024-09-06 22:02:01 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2024-09-06 11:48:56 +00:00
|
|
|
|
|
|
|
class PerDateWidget extends StatefulWidget {
|
|
|
|
final DateTime date;
|
|
|
|
const PerDateWidget({super.key, required this.date});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<PerDateWidget> createState() => _PerDateWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PerDateWidgetState extends State<PerDateWidget> {
|
|
|
|
late FoodStorage storage;
|
|
|
|
late Future<List<FoodEntry>> entriesFuture;
|
|
|
|
List<FoodEntry> entries = [];
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
storage = FoodStorage.getInstance();
|
|
|
|
entriesFuture = storage.getEntriesForDate(widget.date);
|
|
|
|
entriesFuture.then((val) {
|
|
|
|
entries = val;
|
|
|
|
});
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return FutureBuilder(
|
|
|
|
future: entriesFuture,
|
|
|
|
builder: (context, snapshot) {
|
2024-09-07 23:57:40 +00:00
|
|
|
return snapshot.connectionState != ConnectionState.done
|
|
|
|
? const Center(child: CircularProgressIndicator())
|
|
|
|
: MultiProvider(
|
|
|
|
providers: [
|
|
|
|
ChangeNotifierProvider(
|
|
|
|
create: (context) => EnterFoodController()),
|
|
|
|
BlocProvider(
|
|
|
|
create: (context) => FoodEntryBloc(
|
|
|
|
initialState: FoodEntryState(foodEntries: entries),
|
|
|
|
storage: storage,
|
|
|
|
forDate: widget.date,
|
2024-09-06 16:51:24 +00:00
|
|
|
),
|
2024-09-07 23:57:40 +00:00
|
|
|
)
|
|
|
|
],
|
|
|
|
child: BlocBuilder<FoodEntryBloc, FoodEntryState>(
|
|
|
|
builder: (context, state) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title:
|
|
|
|
Text(DateFormat.yMMMMd('de').format(widget.date)),
|
|
|
|
actions: const [ThemeSwitcherButton()],
|
2024-09-06 22:02:01 +00:00
|
|
|
),
|
2024-09-07 23:57:40 +00:00
|
|
|
body: FoodEntryList(entries: state.foodEntries),
|
|
|
|
bottomNavigationBar: BottomAppBar(
|
|
|
|
shape: const RectangularNotchShape(),
|
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
|
|
|
child: SumWidget(foodEntries: state.foodEntries)),
|
|
|
|
drawer: const AppDrawer(),
|
|
|
|
floatingActionButton: OverflowBar(children: [
|
|
|
|
ScanFoodFloatingButton(
|
|
|
|
onPressed: () async {
|
|
|
|
var client = FoodFactLookupClient();
|
|
|
|
|
|
|
|
var scanResult = await BarcodeScanner.scan();
|
2024-09-08 13:07:39 +00:00
|
|
|
|
2024-09-07 23:57:40 +00:00
|
|
|
if (scanResult.type == ResultType.Cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-08 13:07:39 +00:00
|
|
|
|
|
|
|
if (!context.mounted) return;
|
|
|
|
|
2024-09-07 23:57:40 +00:00
|
|
|
if (scanResult.type == ResultType.Error) {
|
2024-09-08 13:07:39 +00:00
|
|
|
showNewSnackbarWith(context,
|
|
|
|
"Fehler beim Scannen des Barcodes.");
|
2024-09-07 23:57:40 +00:00
|
|
|
}
|
|
|
|
var response = await client
|
|
|
|
.retrieveFoodInfo(scanResult.rawContent);
|
|
|
|
|
|
|
|
if (!context.mounted) return;
|
|
|
|
|
|
|
|
if (response.status ==
|
2024-09-08 13:07:39 +00:00
|
|
|
FoodFactResponseStatus.barcodeNotFound) {
|
|
|
|
showNewSnackbarWith(context,
|
|
|
|
"Barcode konnte nicht gefunden werden.");
|
|
|
|
return;
|
2024-09-07 23:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (response.status ==
|
|
|
|
FoodFactResponseStatus
|
|
|
|
.foodFactServerNotReachable) {
|
2024-09-08 13:07:39 +00:00
|
|
|
showNewSnackbarWith(context,
|
|
|
|
"OpenFoodFacts-Server konnte nicht erreicht werden.");
|
|
|
|
return;
|
2024-09-07 23:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
context.read<EnterFoodController>().set(
|
|
|
|
response.food!.name,
|
|
|
|
response.food!.kcalPer100g.toString(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
CalendarFloatingButton(
|
|
|
|
startFromDate: widget.date,
|
|
|
|
onDateSelected: (dateSelected) {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) {
|
|
|
|
return PerDateWidget(date: dateSelected);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
floatingActionButtonLocation:
|
|
|
|
FloatingActionButtonLocation.endDocked);
|
|
|
|
}),
|
|
|
|
);
|
2024-09-06 11:48:56 +00:00
|
|
|
});
|
|
|
|
}
|
2024-09-06 22:02:01 +00:00
|
|
|
|
2024-09-08 13:07:39 +00:00
|
|
|
void showNewSnackbarWith(BuildContext context, String text) {
|
|
|
|
var snackbar =
|
|
|
|
ErrorSnackbar(colorScheme: Theme.of(context).colorScheme, text: text);
|
2024-09-06 22:02:01 +00:00
|
|
|
|
2024-09-08 13:07:39 +00:00
|
|
|
ScaffoldMessenger.of(context).clearSnackBars();
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackbar);
|
2024-09-06 22:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-08 13:07:39 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|