/* SPDX-License-Identifier: GPL-3.0-or-later */ /* Copyright (C) 2024 Marco Groß */ import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/perdate/perdate_pageview_controller.dart'; import 'package:calorimeter/storage/storage.dart'; import 'package:calorimeter/utils/date_time_helper.dart'; import 'package:calorimeter/utils/settings_bloc.dart'; import 'package:calorimeter/utils/theme_bloc.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); var foodStorage = await FoodStorage.create(); await foodStorage.buildFoodLookupDatabase(); runApp( MainApp(storage: foodStorage), ); } class MainApp extends StatefulWidget { final FoodStorage storage; const MainApp({super.key, required this.storage}); @override State createState() => _MainAppState(); } class _MainAppState extends State { late DateTime timeNow; late List entriesForToday; late double kcalLimit; late String brightness; late Future? initFuture; Future asyncInit() async { timeNow = DateTimeHelper.now(); entriesForToday = await widget.storage.getEntriesForDate(timeNow); kcalLimit = await widget.storage.readLimit(); brightness = await widget.storage.readBrightness(); return true; } @override void initState() { super.initState(); initFuture = asyncInit(); } @override Widget build(BuildContext context) { return SafeArea( child: FutureBuilder( future: initFuture, builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { return Center(child: CircularProgressIndicator()); } return MultiBlocProvider( providers: [ BlocProvider( create: (context) => FoodEntryBloc( storage: widget.storage, initialState: GlobalEntryState( foodEntries: {timeNow: entriesForToday}), ), ), BlocProvider( create: (context) => SettingsDataBloc( SettingsState(kcalLimit: kcalLimit), storage: widget.storage), ), BlocProvider( create: (context) => ThemeDataBloc( ThemeState(brightness: brightness), storage: widget.storage), ), ], child: BlocBuilder( builder: (context, state) { var newBrightness = Brightness.light; if (state.brightness == 'dark') { newBrightness = Brightness.dark; } return MaterialApp( home: PerDatePageViewController( initialDate: DateTimeHelper.now()), localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: [ Locale('en'), ...AppLocalizations.supportedLocales, ], theme: ThemeData( dividerTheme: const DividerThemeData(space: 2), colorScheme: ColorScheme.fromSeed( seedColor: Colors.lightBlue, brightness: newBrightness, ), ), ); }, ), ); }), ); } }