calorimeter/lib/main.dart

111 lines
3.3 KiB
Dart
Raw Permalink Normal View History

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> */
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';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
2024-12-10 21:37:13 +00:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2024-06-09 17:06:10 +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();
var storage = await FoodStorage.create();
await storage.buildFoodLookupDatabase();
timeNow = DateTimeHelper.now();
entriesForToday = await storage.getEntriesForDate(timeNow);
var kcalLimit = await storage.readLimit();
2024-06-12 12:42:29 +00:00
var brightness = await storage.readBrightness();
2024-06-12 12:42:29 +00:00
runApp(
MainApp(
storage: storage,
kcalLimit: kcalLimit,
brightness: brightness,
),
);
}
class MainApp extends StatelessWidget {
final FoodStorage storage;
final double kcalLimit;
2024-06-12 12:42:29 +00:00
final String brightness;
2024-06-12 12:42:29 +00:00
const MainApp(
{required this.storage,
required this.kcalLimit,
required this.brightness,
super.key});
@override
Widget build(BuildContext context) {
return SafeArea(
child: MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => FoodEntryBloc(
storage: storage,
initialState:
GlobalEntryState(foodEntries: {timeNow: entriesForToday}),
),
),
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;
if (state.brightness == 'dark') {
2024-09-05 14:36:35 +00:00
newBrightness = Brightness.dark;
}
2024-06-12 12:42:29 +00:00
return MaterialApp.router(
routerConfig: GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) {
2024-10-06 23:44:19 +00:00
return PerDatePageViewController(
initialDate: DateTimeHelper.now(),
);
},
),
],
),
2024-12-10 21:37:13 +00:00
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: [
Locale('en'),
Locale('de'),
],
2024-09-05 14:36:35 +00:00
theme: ThemeData(
dividerTheme: const DividerThemeData(space: 2),
2024-09-05 14:36:35 +00:00
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.lightBlue,
brightness: newBrightness,
),
),
);
},
),
),
);
}
}