From 53ff5688e9b5cad4a86ce10a8493e88a1a0905c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20K=C3=B6rber?= <56073945+jakobkoerber@users.noreply.github.com> Date: Fri, 29 Mar 2024 01:07:12 +0100 Subject: [PATCH] User Interface Enhancements (#233) --- lib/base/util/places_util.dart | 28 ++++++ lib/base/util/string_parser.dart | 10 +++ .../model/cafeterias/cafeteria.dart | 17 +--- .../viewModels/cafeterias_viewmodel.dart | 18 ++-- .../views/cafeterias/cafeteria_view.dart | 85 ++++++++----------- .../homeWidget/cafeteria_widget_view.dart | 26 ++---- 6 files changed, 91 insertions(+), 93 deletions(-) create mode 100644 lib/base/util/places_util.dart diff --git a/lib/base/util/places_util.dart b/lib/base/util/places_util.dart new file mode 100644 index 00000000..bbfcb14b --- /dev/null +++ b/lib/base/util/places_util.dart @@ -0,0 +1,28 @@ +import 'package:campus_flutter/base/extensions/context.dart'; +import 'package:campus_flutter/base/util/string_parser.dart'; +import 'package:campus_flutter/placesComponent/model/cafeterias/opening_hours.dart'; +import 'package:flutter/material.dart'; + +class PlacesUtil { + static Widget? openingHours( + (bool, OpeningHour?)? openingHours, + DateTime? dateTime, + BuildContext context, + ) { + if (openingHours != null && dateTime != null) { + final dayString = StringParser.getDayString(dateTime, context); + return Padding( + padding: EdgeInsets.only(left: context.padding), + child: Text( + context.localizations.open( + dayString, + openingHours.$2!.start, + openingHours.$2!.end, + ), + ), + ); + } else { + return null; + } + } +} diff --git a/lib/base/util/string_parser.dart b/lib/base/util/string_parser.dart index 7a07eada..30884b61 100644 --- a/lib/base/util/string_parser.dart +++ b/lib/base/util/string_parser.dart @@ -1,3 +1,4 @@ +import 'package:campus_flutter/base/extensions/date_time.dart'; import 'package:flutter/material.dart'; import 'package:campus_flutter/base/extensions/context.dart'; import 'package:intl/intl.dart'; @@ -57,4 +58,13 @@ class StringParser { static int? optStringToOptInt(String? number) { return number != null ? int.tryParse(number) : null; } + + static String getDayString(DateTime dateTime, BuildContext context) { + final today = DateTime.now(); + if (dateTime.isAtSameDay(today)) { + return context.localizations.today; + } else { + return DateFormat.EEEE(context.localizations.localeName).format(dateTime); + } + } } diff --git a/lib/placesComponent/model/cafeterias/cafeteria.dart b/lib/placesComponent/model/cafeterias/cafeteria.dart index bcfc4d32..29f95ce8 100644 --- a/lib/placesComponent/model/cafeterias/cafeteria.dart +++ b/lib/placesComponent/model/cafeterias/cafeteria.dart @@ -1,10 +1,6 @@ -import 'package:campus_flutter/base/extensions/context.dart'; -import 'package:campus_flutter/base/extensions/date_time.dart'; import 'package:campus_flutter/placesComponent/model/cafeterias/opening_hours.dart'; import 'package:campus_flutter/searchComponent/model/comparison_token.dart'; import 'package:campus_flutter/searchComponent/protocols/searchable.dart'; -import 'package:flutter/material.dart'; -import 'package:intl/intl.dart'; import 'package:json_annotation/json_annotation.dart'; part 'cafeteria.g.dart'; @@ -55,8 +51,8 @@ class Cafeteria extends Searchable { return name; } - (bool, OpeningHour?) openingHoursForDate(DateTime dateTime) { - switch (dateTime.weekday) { + (bool, OpeningHour?) openingHoursForDate(DateTime? dateTime) { + switch (dateTime?.weekday) { case 1: return (true, openingHours?.mon); case 2: @@ -72,15 +68,6 @@ class Cafeteria extends Searchable { } } - String getDayString(DateTime dateTime, BuildContext context) { - final today = DateTime.now(); - if (dateTime.isAtSameDay(today)) { - return context.localizations.today; - } else { - return DateFormat.EEEE(context.localizations.localeName).format(dateTime); - } - } - @override @JsonKey(includeFromJson: false, includeToJson: false) List get comparisonTokens => [ diff --git a/lib/placesComponent/viewModels/cafeterias_viewmodel.dart b/lib/placesComponent/viewModels/cafeterias_viewmodel.dart index e7b1f178..8ec652ea 100644 --- a/lib/placesComponent/viewModels/cafeterias_viewmodel.dart +++ b/lib/placesComponent/viewModels/cafeterias_viewmodel.dart @@ -93,15 +93,15 @@ class CafeteriasViewModel { } else { LocationService.getLastKnown().then( (position) => _getClosestCafeteria(position, value.$2), - onError: (error) { - widgetCafeteria.addError(error); - fetchCafeteriaMenu(forcedRefresh, value.$2.first).then( - (menu) => widgetCafeteria.add( - (value.$2.first, menu.firstOrNull), - ), - onError: (error) => widgetCafeteria.addError(error), - ); - }, + onError: (error) => fetchCafeteriaMenu( + forcedRefresh, + value.$2.first, + ).then( + (menu) => widgetCafeteria.add( + (value.$2.first, menu.firstOrNull), + ), + onError: (error) => widgetCafeteria.addError(error), + ), ); } }, diff --git a/lib/placesComponent/views/cafeterias/cafeteria_view.dart b/lib/placesComponent/views/cafeterias/cafeteria_view.dart index 6c6fca08..0b606b3a 100644 --- a/lib/placesComponent/views/cafeterias/cafeteria_view.dart +++ b/lib/placesComponent/views/cafeterias/cafeteria_view.dart @@ -5,6 +5,7 @@ import 'package:campus_flutter/base/util/delayed_loading_indicator.dart'; import 'package:campus_flutter/base/util/directions_launcher.dart'; import 'package:campus_flutter/base/util/info_row.dart'; import 'package:campus_flutter/base/errorHandling/error_handling_router.dart'; +import 'package:campus_flutter/base/util/places_util.dart'; import 'package:campus_flutter/placesComponent/model/cafeterias/cafeteria.dart'; import 'package:campus_flutter/placesComponent/model/cafeterias/opening_hours.dart'; import 'package:campus_flutter/placesComponent/viewModels/cafeterias_viewmodel.dart'; @@ -17,6 +18,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; +import 'package:syncfusion_flutter_core/theme.dart'; import 'package:syncfusion_flutter_datepicker/datepicker.dart'; class CafeteriaScaffold extends ConsumerWidget { @@ -137,13 +139,11 @@ class CafeteriaView extends ConsumerStatefulWidget { class _CafeteriaViewState extends ConsumerState { late DateTime selectedDate; - late (bool, OpeningHour?) openingHours; @override void initState() { final today = DateTime.now(); selectedDate = DateTime(today.year, today.month, today.day); - openingHours = widget.cafeteria.openingHoursForDate(today); super.initState(); } @@ -163,35 +163,12 @@ class _CafeteriaViewState extends ConsumerState { ], ); } else { - return Column( - children: [ - if (openingHours.$2 != null && openingHours.$1) - _openingTimes(openingHours, context), - _pickerAndSlider(false), - ], - ); + return _pickerAndSlider(false); } }, ); } - Widget _openingTimes( - (bool, OpeningHour?) openingHours, - BuildContext context, - ) { - if (!openingHours.$1) { - return Text(context.localizations.closedToday); - } else { - return Text( - context.localizations.open( - widget.cafeteria.getDayString(DateTime.now(), context), - openingHours.$2!.start, - openingHours.$2!.end, - ), - ); - } - } - List _mapAndDirections() { return [ MapWidget.fullPadding( @@ -244,14 +221,24 @@ class _CafeteriaViewState extends ConsumerState { element.date.isAfter(selectedDate), ), ); - return Expanded( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - Padding( - padding: EdgeInsets.all(context.padding), - child: SizedBox( - height: 80, + final Widget? openingHoursWidget = PlacesUtil.openingHours( + widget.cafeteria.openingHoursForDate(selectedDate), + selectedDate, + context, + ); + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + if (openingHoursWidget != null) openingHoursWidget, + Padding( + padding: EdgeInsets.all(context.padding), + child: SizedBox( + height: 80, + child: SfDateRangePickerTheme( + data: const SfDateRangePickerThemeData( + headerBackgroundColor: Colors.transparent, + backgroundColor: Colors.transparent, + ), child: SfDateRangePicker( headerHeight: 0, toggleDaySelection: false, @@ -275,24 +262,24 @@ class _CafeteriaViewState extends ConsumerState { ), ), ), - if (todayMeals.isNotEmpty) - Expanded( - child: DishGridView( - dishes: todayMeals, - isLandscape: isLandscape, - //inverted: true, - ), + ), + if (todayMeals.isNotEmpty) + Expanded( + child: DishGridView( + dishes: todayMeals, + isLandscape: isLandscape, + //inverted: true, ), - if (todayMeals.isEmpty) - Center( - child: Text( - context.localizations.noEntriesFound( - context.localizations.mealPlans, - ), + ), + if (todayMeals.isEmpty) + Center( + child: Text( + context.localizations.noEntriesFound( + context.localizations.mealPlans, ), ), - ], - ), + ), + ], ); } } else if (snapshot.hasError) { diff --git a/lib/placesComponent/views/homeWidget/cafeteria_widget_view.dart b/lib/placesComponent/views/homeWidget/cafeteria_widget_view.dart index 5e2abb27..4beea856 100644 --- a/lib/placesComponent/views/homeWidget/cafeteria_widget_view.dart +++ b/lib/placesComponent/views/homeWidget/cafeteria_widget_view.dart @@ -2,6 +2,7 @@ import 'package:campus_flutter/base/enums/error_handling_view_type.dart'; import 'package:campus_flutter/base/util/delayed_loading_indicator.dart'; import 'package:campus_flutter/base/errorHandling/error_handling_router.dart'; import 'package:campus_flutter/base/routing/routes.dart'; +import 'package:campus_flutter/base/util/places_util.dart'; import 'package:campus_flutter/homeComponent/widgetComponent/views/preference_selection_view.dart'; import 'package:campus_flutter/homeComponent/widgetComponent/views/widget_frame_view.dart'; import 'package:campus_flutter/placesComponent/model/cafeterias/cafeteria.dart'; @@ -70,32 +71,17 @@ class _CafeteriaWidgetViewState extends ConsumerState { ), ], ), - subtitle: _openingHours(snapshot.data), + subtitle: PlacesUtil.openingHours( + snapshot.data?.$1.openingHoursForDate(snapshot.data?.$2?.date), + snapshot.data?.$2!.date, + context, + ), child: _dynamicContent(snapshot), ); }, ); } - Widget? _openingHours((Cafeteria, CafeteriaMenu?)? value) { - if (value != null && value.$2 != null) { - final openingHours = value.$1.openingHoursForDate(value.$2!.date); - final dayString = value.$1.getDayString(value.$2!.date, context); - return Padding( - padding: EdgeInsets.only(left: context.padding), - child: Text( - context.localizations.open( - dayString, - openingHours.$2!.start, - openingHours.$2!.end, - ), - ), - ); - } else { - return null; - } - } - Widget _dynamicContent(AsyncSnapshot<(Cafeteria, CafeteriaMenu?)?> snapshot) { if (snapshot.hasData) { final dishes =