@@ -31,12 +31,7 @@ import 'package:wger/providers/routines.dart';
3131import 'package:wger/theme/theme.dart' ;
3232
3333/// Types of events
34- enum EventType {
35- weight,
36- measurement,
37- session,
38- caloriesDiary,
39- }
34+ enum EventType { weight, measurement, session, caloriesDiary }
4035
4136/// An event in the dashboard calendar
4237class Event {
@@ -85,6 +80,22 @@ class _DashboardCalendarWidgetState extends State<DashboardCalendarWidget>
8580 });
8681 }
8782
83+ /// Loads and organizes all events from various providers into the calendar.
84+ ///
85+ /// This method asynchronously fetches and processes data from multiple sources:
86+ /// - **Weight entries**: Retrieves weight measurements from [BodyWeightProvider]
87+ /// - **Measurements**: Retrieves body measurements from [MeasurementProvider]
88+ /// - **Workout sessions**: Fetches workout session data from [RoutinesProvider]
89+ /// - **Nutritional plans**: Retrieves calorie diary entries from [NutritionPlansProvider]
90+ ///
91+ /// Each event is formatted according to the current locale and stored in the
92+ /// [_events] map, keyed by date. The date format is determined by [DateFormatLists.format] .
93+ ///
94+ /// After loading all events, the [_selectedEvents] value is updated with events
95+ /// for the currently selected day, if any.
96+ ///
97+ /// **Note**: This method checks if the widget is still mounted before updating
98+ /// the state after the async workout session fetch operation.
8899 void loadEvents () async {
89100 final numberFormat = NumberFormat .decimalPattern (Localizations .localeOf (context).toString ());
90101 final i18n = AppLocalizations .of (context);
@@ -112,10 +123,12 @@ class _DashboardCalendarWidgetState extends State<DashboardCalendarWidget>
112123 _events[date] = [];
113124 }
114125
115- _events[date]? .add (Event (
116- EventType .measurement,
117- '${category .name }: ${numberFormat .format (entry .value )} ${category .unit }' ,
118- ));
126+ _events[date]? .add (
127+ Event (
128+ EventType .measurement,
129+ '${category .name }: ${numberFormat .format (entry .value )} ${category .unit }' ,
130+ ),
131+ );
119132 }
120133 }
121134
@@ -131,16 +144,20 @@ class _DashboardCalendarWidgetState extends State<DashboardCalendarWidget>
131144 time = '(${timeToString (session .timeStart )} - ${timeToString (session .timeEnd )})' ;
132145
133146 // Add events to lists
134- _events[date]? .add (Event (
135- EventType .session,
136- '${i18n .impression }: ${session .impressionAsString } $time ' ,
137- ));
147+ _events[date]? .add (
148+ Event (EventType .session, '${i18n .impression }: ${session .impressionAsString } $time ' ),
149+ );
138150 }
139151 });
152+ if (! mounted) {
153+ return ;
154+ }
140155
141156 // Process nutritional plans
142- final NutritionPlansProvider nutritionProvider =
143- Provider .of <NutritionPlansProvider >(context, listen: false );
157+ final NutritionPlansProvider nutritionProvider = Provider .of <NutritionPlansProvider >(
158+ context,
159+ listen: false ,
160+ );
144161 for (final plan in nutritionProvider.items) {
145162 for (final entry in plan.logEntriesValues.entries) {
146163 final date = DateFormatLists .format (entry.key);
@@ -149,10 +166,9 @@ class _DashboardCalendarWidgetState extends State<DashboardCalendarWidget>
149166 }
150167
151168 // Add events to lists
152- _events[date]? .add (Event (
153- EventType .caloriesDiary,
154- i18n.kcalValue (entry.value.energy.toStringAsFixed (0 )),
155- ));
169+ _events[date]? .add (
170+ Event (EventType .caloriesDiary, i18n.kcalValue (entry.value.energy.toStringAsFixed (0 ))),
171+ );
156172 }
157173 }
158174
@@ -250,8 +266,10 @@ class _DashboardCalendarWidgetState extends State<DashboardCalendarWidget>
250266 valueListenable: _selectedEvents,
251267 builder: (context, value, _) => Column (
252268 children: [
253- ...value.map ((event) => ListTile (
254- title: Text ((() {
269+ ...value.map (
270+ (event) => ListTile (
271+ title: Text (
272+ (() {
255273 switch (event.type) {
256274 case EventType .caloriesDiary:
257275 return AppLocalizations .of (context).nutritionalDiary;
@@ -265,10 +283,12 @@ class _DashboardCalendarWidgetState extends State<DashboardCalendarWidget>
265283 case EventType .measurement:
266284 return AppLocalizations .of (context).measurement;
267285 }
268- })()),
269- subtitle: Text (event.description),
270- //onTap: () => print('$event tapped!'),
271- )),
286+ })(),
287+ ),
288+ subtitle: Text (event.description),
289+ //onTap: () => print('$event tapped!'),
290+ ),
291+ ),
272292 ],
273293 ),
274294 ),
0 commit comments