2024-12-07 12:29:34 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
2024-12-07 12:39:11 +00:00
|
|
|
/* Copyright (C) 2024 Marco Groß <mgross@sw-gross.de> */
|
2024-10-07 22:58:56 +00:00
|
|
|
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
2024-10-06 23:44:19 +00:00
|
|
|
import 'package:calorimeter/perdate/perdate_pageview_controller.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/storage/storage.dart';
|
2024-10-06 23:44:19 +00:00
|
|
|
import 'package:calorimeter/utils/date_time_helper.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/utils/settings_bloc.dart';
|
|
|
|
import 'package:calorimeter/utils/theme_bloc.dart';
|
2024-05-29 22:58:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-10-03 21:21:13 +00:00
|
|
|
import 'package:go_router/go_router.dart';
|
2024-12-10 21:37:07 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-10-07 22:58:56 +00:00
|
|
|
List<FoodEntryState> entriesForToday = [];
|
|
|
|
DateTime timeNow = DateTime.now();
|
|
|
|
|
2024-06-09 17:06:10 +00:00
|
|
|
void main() async {
|
2024-06-09 18:11:54 +00:00
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2024-10-03 21:21:13 +00:00
|
|
|
|
2024-09-04 20:47:32 +00:00
|
|
|
var storage = await FoodStorage.create();
|
|
|
|
await storage.buildFoodLookupDatabase();
|
2024-10-07 22:58:56 +00:00
|
|
|
|
2024-10-18 00:44:43 +00:00
|
|
|
timeNow = DateTimeHelper.now();
|
2024-10-07 22:58:56 +00:00
|
|
|
entriesForToday = await storage.getEntriesForDate(timeNow);
|
|
|
|
|
2024-06-11 17:05:42 +00:00
|
|
|
var kcalLimit = await storage.readLimit();
|
2024-06-12 12:42:29 +00:00
|
|
|
var brightness = await storage.readBrightness();
|
2024-05-29 22:58:26 +00:00
|
|
|
|
2024-06-12 12:42:29 +00:00
|
|
|
runApp(
|
|
|
|
MainApp(
|
|
|
|
storage: storage,
|
|
|
|
kcalLimit: kcalLimit,
|
|
|
|
brightness: brightness,
|
|
|
|
),
|
|
|
|
);
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class MainApp extends StatelessWidget {
|
2024-09-04 20:47:32 +00:00
|
|
|
final FoodStorage storage;
|
2024-06-11 17:05:42 +00:00
|
|
|
final double kcalLimit;
|
2024-06-12 12:42:29 +00:00
|
|
|
final String brightness;
|
2024-06-11 17:05:42 +00:00
|
|
|
|
2024-06-12 12:42:29 +00:00
|
|
|
const MainApp(
|
|
|
|
{required this.storage,
|
|
|
|
required this.kcalLimit,
|
|
|
|
required this.brightness,
|
|
|
|
super.key});
|
2024-05-29 22:58:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-09-04 20:47:32 +00:00
|
|
|
return SafeArea(
|
|
|
|
child: MultiBlocProvider(
|
|
|
|
providers: [
|
2024-10-07 22:58:56 +00:00
|
|
|
BlocProvider(
|
|
|
|
create: (context) => FoodEntryBloc(
|
|
|
|
storage: storage,
|
|
|
|
initialState:
|
|
|
|
GlobalEntryState(foodEntries: {timeNow: entriesForToday}),
|
|
|
|
),
|
|
|
|
),
|
2024-09-04 20:47:32 +00:00
|
|
|
BlocProvider(
|
|
|
|
create: (context) => SettingsDataBloc(
|
|
|
|
SettingsState(kcalLimit: kcalLimit),
|
|
|
|
storage: storage),
|
|
|
|
),
|
|
|
|
BlocProvider(
|
|
|
|
create: (context) => ThemeDataBloc(
|
|
|
|
ThemeState(brightness: brightness),
|
|
|
|
storage: storage),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
child: BlocBuilder<ThemeDataBloc, ThemeState>(
|
|
|
|
builder: (context, state) {
|
2024-09-05 14:36:35 +00:00
|
|
|
var newBrightness = Brightness.light;
|
2024-09-04 20:47:32 +00:00
|
|
|
if (state.brightness == 'dark') {
|
2024-09-05 14:36:35 +00:00
|
|
|
newBrightness = Brightness.dark;
|
2024-09-04 20:47:32 +00:00
|
|
|
}
|
2024-06-12 12:42:29 +00:00
|
|
|
|
2024-10-03 21:21:13 +00:00
|
|
|
return MaterialApp.router(
|
2024-12-10 21:37:07 +00:00
|
|
|
//locale: Locale('de'),
|
2024-10-03 21:21:13 +00:00
|
|
|
routerConfig: GoRouter(
|
|
|
|
routes: [
|
|
|
|
GoRoute(
|
|
|
|
path: '/',
|
|
|
|
builder: (context, state) {
|
2024-10-06 23:44:19 +00:00
|
|
|
return PerDatePageViewController(
|
|
|
|
initialDate: DateTimeHelper.now(),
|
2024-10-03 21:21:13 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2024-12-10 21:37:07 +00:00
|
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
2024-09-05 14:36:35 +00:00
|
|
|
theme: ThemeData(
|
2024-09-06 00:23:47 +00:00
|
|
|
dividerTheme: const DividerThemeData(space: 2),
|
2024-09-05 14:36:35 +00:00
|
|
|
colorScheme: ColorScheme.fromSeed(
|
|
|
|
seedColor: Colors.lightBlue,
|
|
|
|
brightness: newBrightness,
|
|
|
|
),
|
|
|
|
),
|
2024-09-04 20:47:32 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-05-29 22:58:26 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|