From 45f44099a115e3f171b8f17014be0ddc6b76d4a6 Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 6 Sep 2024 13:48:56 +0200 Subject: [PATCH] Refactor code --- lib/enter_food_widget.dart | 2 +- lib/{ => food_entry}/food_entry_bloc.dart | 0 lib/{ => food_entry}/food_entry_widget.dart | 6 +- lib/main.dart | 34 +---- lib/perdate/entry_list.dart | 53 ++++++++ lib/perdate/perdate_widget.dart | 72 ++++++++++ lib/perdate_widget.dart | 126 ------------------ lib/storage/storage.dart | 2 +- lib/{ => utils}/calendar_floating_button.dart | 0 lib/utils/router.dart | 28 ++++ lib/{ => utils}/settings.dart | 4 +- lib/{ => utils}/settings_bloc.dart | 0 lib/{ => utils}/sum_widget.dart | 4 +- lib/{ => utils}/theme_bloc.dart | 0 lib/utils/theme_switcher_button.dart | 25 ++++ 15 files changed, 193 insertions(+), 163 deletions(-) rename lib/{ => food_entry}/food_entry_bloc.dart (100%) rename lib/{ => food_entry}/food_entry_widget.dart (94%) create mode 100644 lib/perdate/entry_list.dart create mode 100644 lib/perdate/perdate_widget.dart delete mode 100644 lib/perdate_widget.dart rename lib/{ => utils}/calendar_floating_button.dart (100%) create mode 100644 lib/utils/router.dart rename lib/{ => utils}/settings.dart (95%) rename lib/{ => utils}/settings_bloc.dart (100%) rename lib/{ => utils}/sum_widget.dart (90%) rename lib/{ => utils}/theme_bloc.dart (100%) create mode 100644 lib/utils/theme_switcher_button.dart diff --git a/lib/enter_food_widget.dart b/lib/enter_food_widget.dart index 485b0db..8af7c7c 100644 --- a/lib/enter_food_widget.dart +++ b/lib/enter_food_widget.dart @@ -1,6 +1,6 @@ import 'package:calodiary/storage/storage.dart'; import 'package:flutter/material.dart'; -import 'package:calodiary/food_entry_bloc.dart'; +import 'package:calodiary/food_entry/food_entry_bloc.dart'; import 'package:calodiary/row_with_spacers_widget.dart'; class EnterFoodWidget extends StatefulWidget { diff --git a/lib/food_entry_bloc.dart b/lib/food_entry/food_entry_bloc.dart similarity index 100% rename from lib/food_entry_bloc.dart rename to lib/food_entry/food_entry_bloc.dart diff --git a/lib/food_entry_widget.dart b/lib/food_entry/food_entry_widget.dart similarity index 94% rename from lib/food_entry_widget.dart rename to lib/food_entry/food_entry_widget.dart index a31c695..9206f4f 100644 --- a/lib/food_entry_widget.dart +++ b/lib/food_entry/food_entry_widget.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:calodiary/food_entry_bloc.dart'; +import 'package:calodiary/food_entry/food_entry_bloc.dart'; import 'package:calodiary/row_with_spacers_widget.dart'; class FoodEntryWidget extends StatefulWidget { @@ -66,7 +66,7 @@ class _FoodEntryWidgetState extends State { children: [ SizedBox( child: IconButton( - padding: EdgeInsets.all(0.0), + padding: const EdgeInsets.all(0.0), icon: const Icon(Icons.cancel), onPressed: showCancelAndDelete ? () => setState(() { @@ -77,7 +77,7 @@ class _FoodEntryWidgetState extends State { ), SizedBox( child: IconButton( - padding: EdgeInsets.all(0.0), + padding: const EdgeInsets.all(0.0), iconSize: 24, icon: const Icon(Icons.delete), color: Colors.redAccent, diff --git a/lib/main.dart b/lib/main.dart index 07e4574..a45b66e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,9 @@ -import 'package:calodiary/perdate_widget.dart'; -import 'package:calodiary/settings.dart'; -import 'package:calodiary/settings_bloc.dart'; import 'package:calodiary/storage/storage.dart'; -import 'package:calodiary/theme_bloc.dart'; +import 'package:calodiary/perdate/perdate_widget.dart'; +import 'package:calodiary/utils/router.dart'; +import 'package:calodiary/utils/settings.dart'; +import 'package:calodiary/utils/settings_bloc.dart'; +import 'package:calodiary/utils/theme_bloc.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; @@ -74,7 +75,7 @@ class MainApp extends StatelessWidget { brightness: newBrightness, ), ), - routerConfig: router, + routerConfig: getRouterConfig(), ); }, ), @@ -82,26 +83,3 @@ class MainApp extends StatelessWidget { ); } } - -final router = GoRouter(initialLocation: '/day', routes: [ - GoRoute( - path: '/day', - name: 'perDay', - builder: (context, state) { - DateTime date; - if (state.extra == null || state.extra is! DateTime) { - date = DateTime.now(); - } else { - date = state.extra as DateTime; - } - - return PerDateWidget(date: date); - }), - GoRoute( - path: '/settings', - name: 'settings', - builder: (context, state) { - return const SettingsWidget(); - }, - ) -]); diff --git a/lib/perdate/entry_list.dart b/lib/perdate/entry_list.dart new file mode 100644 index 0000000..94e79f6 --- /dev/null +++ b/lib/perdate/entry_list.dart @@ -0,0 +1,53 @@ +import 'package:calodiary/enter_food_widget.dart'; +import 'package:calodiary/food_entry/food_entry_bloc.dart'; +import 'package:calodiary/food_entry/food_entry_widget.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +class FoodEntryList extends StatelessWidget { + final List entries; + + const FoodEntryList({ + required this.entries, + super.key, + }); + + @override + Widget build(BuildContext context) { + return ListView.builder( + itemCount: entries.length + 1, + itemBuilder: (BuildContext itemBuilderContext, int listIndex) { + if (listIndex == entries.length) { + return Column( + children: [ + EnterFoodWidget( + onAdd: (context, entry) { + context + .read() + .add(FoodEntryEvent(entry: entry)); + }, + ), + const SizedBox(height: 75), + ], + ); + } + + var entryIndex = listIndex; + return Column( + children: [ + FoodEntryWidget( + key: ValueKey(entries[entryIndex].id), + entry: entries[entryIndex], + onDelete: (callbackContext, id) { + callbackContext.read().add(FoodDeletionEvent( + entryID: id, + )); + }, + ), + const Divider(), + ], + ); + }, + ); + } +} diff --git a/lib/perdate/perdate_widget.dart b/lib/perdate/perdate_widget.dart new file mode 100644 index 0000000..eac1813 --- /dev/null +++ b/lib/perdate/perdate_widget.dart @@ -0,0 +1,72 @@ +import 'package:calodiary/app_drawer.dart'; +import 'package:calodiary/utils/calendar_floating_button.dart'; +import 'package:calodiary/perdate/entry_list.dart'; +import 'package:calodiary/food_entry/food_entry_bloc.dart'; +import 'package:calodiary/storage/storage.dart'; +import 'package:calodiary/utils/sum_widget.dart'; +import 'package:calodiary/utils/theme_switcher_button.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:intl/intl.dart'; + +class PerDateWidget extends StatefulWidget { + final DateTime date; + const PerDateWidget({super.key, required this.date}); + + @override + State createState() => _PerDateWidgetState(); +} + +class _PerDateWidgetState extends State { + late FoodStorage storage; + late Future> entriesFuture; + List entries = []; + String formattedDate = ''; + + @override + void initState() { + formattedDate = DateFormat.yMMMMd('de').format(widget.date); + 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) { + if (snapshot.connectionState != ConnectionState.done) { + return const Center(child: CircularProgressIndicator()); + } else { + return BlocProvider( + create: (context) => FoodEntryBloc( + initialState: FoodEntryState(foodEntries: entries), + storage: storage, + forDate: widget.date), + child: BlocBuilder( + builder: (context, state) { + return Scaffold( + appBar: AppBar( + title: Text(formattedDate), + actions: const [ThemeSwitcherButton()], + ), + body: FoodEntryList(entries: state.foodEntries), + bottomNavigationBar: BottomAppBar( + shape: const CircularNotchedRectangle(), + color: Theme.of(context).colorScheme.secondary, + child: SumWidget(foodEntries: state.foodEntries)), + drawer: const AppDrawer(), + floatingActionButton: + CalendarFloatingButton(date: widget.date), + floatingActionButtonLocation: + FloatingActionButtonLocation.endDocked); + }), + ); + } + }); + } +} diff --git a/lib/perdate_widget.dart b/lib/perdate_widget.dart deleted file mode 100644 index 16e81f0..0000000 --- a/lib/perdate_widget.dart +++ /dev/null @@ -1,126 +0,0 @@ -import 'package:calodiary/theme_bloc.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:intl/intl.dart'; -import 'package:calodiary/app_drawer.dart'; -import 'package:calodiary/calendar_floating_button.dart'; -import 'package:calodiary/enter_food_widget.dart'; -import 'package:calodiary/food_entry_bloc.dart'; -import 'package:calodiary/food_entry_widget.dart'; -import 'package:calodiary/storage/storage.dart'; -import 'package:calodiary/sum_widget.dart'; - -class PerDateWidget extends StatefulWidget { - final DateTime date; - const PerDateWidget({super.key, required this.date}); - - @override - State createState() => _PerDateWidgetState(); -} - -class _PerDateWidgetState extends State { - late FoodStorage storage; - late Future> entriesFuture; - late List entries = []; - - @override - void initState() { - super.initState(); - - storage = FoodStorage.getInstance(); - entriesFuture = storage.getEntriesForDate(widget.date); - entriesFuture.then((val) { - entries = val; - }); - } - - @override - Widget build(BuildContext context) { - var formattedDate = DateFormat.yMMMMd('de').format(widget.date); - return FutureBuilder( - future: entriesFuture, - builder: (context, snapshot) { - if (snapshot.connectionState != ConnectionState.done) { - return const Center(child: CircularProgressIndicator()); - } else { - return BlocProvider( - create: (context) => FoodEntryBloc( - initialState: FoodEntryState(foodEntries: entries), - storage: storage, - forDate: widget.date), - child: BlocBuilder( - builder: (context, state) { - return Scaffold( - appBar: AppBar( - title: Text(formattedDate), - actions: [ - BlocBuilder( - builder: (context, state) { - var icon = const Icon(Icons.light_mode); - if (state.brightness == 'light') { - icon = const Icon(Icons.dark_mode); - } - return IconButton( - icon: icon, - onPressed: () { - context - .read() - .add(ThemeToggleEvent()); - }, - ); - }), - ], - ), - body: ListView.builder( - itemCount: state.foodEntries.length + 1, - itemBuilder: - (BuildContext itemBuilderContext, int listIndex) { - if (listIndex == state.foodEntries.length) { - return Column( - children: [ - EnterFoodWidget( - onAdd: (context, entry) { - context - .read() - .add(FoodEntryEvent(entry: entry)); - }, - ), - const SizedBox(height: 75), - ], - ); - } - - var entryIndex = listIndex; - return Column( - children: [ - FoodEntryWidget( - key: ValueKey(state.foodEntries[entryIndex].id), - entry: state.foodEntries[entryIndex], - onDelete: (callbackContext, id) { - callbackContext - .read() - .add(FoodDeletionEvent( - entryID: id, - )); - }, - ), - const Divider(), - ], - ); - }, - ), - bottomNavigationBar: BottomAppBar( - shape: const CircularNotchedRectangle(), - color: Theme.of(context).colorScheme.secondary, - child: SumWidget(foodEntries: state.foodEntries)), - drawer: const AppDrawer(), - floatingActionButton: - CalendarFloatingButton(date: widget.date), - floatingActionButtonLocation: - FloatingActionButtonLocation.endDocked); - }), - ); - } - }); - } -} diff --git a/lib/storage/storage.dart b/lib/storage/storage.dart index b6fc749..4cf41be 100644 --- a/lib/storage/storage.dart +++ b/lib/storage/storage.dart @@ -1,7 +1,7 @@ import 'dart:developer'; import 'dart:io'; -import 'package:calodiary/food_entry_bloc.dart'; +import 'package:calodiary/food_entry/food_entry_bloc.dart'; import 'package:path_provider/path_provider.dart'; import 'package:universal_platform/universal_platform.dart'; diff --git a/lib/calendar_floating_button.dart b/lib/utils/calendar_floating_button.dart similarity index 100% rename from lib/calendar_floating_button.dart rename to lib/utils/calendar_floating_button.dart diff --git a/lib/utils/router.dart b/lib/utils/router.dart new file mode 100644 index 0000000..e06a13c --- /dev/null +++ b/lib/utils/router.dart @@ -0,0 +1,28 @@ +import 'package:calodiary/perdate/perdate_widget.dart'; +import 'package:calodiary/utils/settings.dart'; +import 'package:go_router/go_router.dart'; + +GoRouter getRouterConfig() { + return GoRouter(initialLocation: '/day', routes: [ + GoRoute( + path: '/day', + name: 'perDay', + builder: (context, state) { + DateTime date; + if (state.extra == null || state.extra is! DateTime) { + date = DateTime.now(); + } else { + date = state.extra as DateTime; + } + + return PerDateWidget(date: date); + }), + GoRoute( + path: '/settings', + name: 'settings', + builder: (context, state) { + return const SettingsWidget(); + }, + ) + ]); +} diff --git a/lib/settings.dart b/lib/utils/settings.dart similarity index 95% rename from lib/settings.dart rename to lib/utils/settings.dart index 4bf1c40..f836b9b 100644 --- a/lib/settings.dart +++ b/lib/utils/settings.dart @@ -2,8 +2,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; import 'package:calodiary/app_drawer.dart'; -import 'package:calodiary/calendar_floating_button.dart'; -import 'package:calodiary/settings_bloc.dart'; +import 'package:calodiary/utils/calendar_floating_button.dart'; +import 'package:calodiary/utils/settings_bloc.dart'; import 'package:settings_ui/settings_ui.dart'; class SettingsWidget extends StatefulWidget { diff --git a/lib/settings_bloc.dart b/lib/utils/settings_bloc.dart similarity index 100% rename from lib/settings_bloc.dart rename to lib/utils/settings_bloc.dart diff --git a/lib/sum_widget.dart b/lib/utils/sum_widget.dart similarity index 90% rename from lib/sum_widget.dart rename to lib/utils/sum_widget.dart index 6b4088a..94aa46e 100644 --- a/lib/sum_widget.dart +++ b/lib/utils/sum_widget.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:calodiary/food_entry_bloc.dart'; -import 'package:calodiary/settings_bloc.dart'; +import 'package:calodiary/food_entry/food_entry_bloc.dart'; +import 'package:calodiary/utils/settings_bloc.dart'; import 'package:calodiary/row_with_spacers_widget.dart'; class SumWidget extends StatefulWidget { diff --git a/lib/theme_bloc.dart b/lib/utils/theme_bloc.dart similarity index 100% rename from lib/theme_bloc.dart rename to lib/utils/theme_bloc.dart diff --git a/lib/utils/theme_switcher_button.dart b/lib/utils/theme_switcher_button.dart new file mode 100644 index 0000000..096bbf4 --- /dev/null +++ b/lib/utils/theme_switcher_button.dart @@ -0,0 +1,25 @@ +import 'package:calodiary/utils/theme_bloc.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +class ThemeSwitcherButton extends StatelessWidget { + const ThemeSwitcherButton({ + super.key, + }); + + @override + Widget build(BuildContext context) { + return BlocBuilder(builder: (context, state) { + var icon = const Icon(Icons.light_mode); + if (state.brightness == 'light') { + icon = const Icon(Icons.dark_mode); + } + return IconButton( + icon: icon, + onPressed: () { + context.read().add(ThemeToggleEvent()); + }, + ); + }); + } +}