@@ -49,13 +49,11 @@ class MensaPageState extends State<MensaPage> with WidgetsBindingObserver, Autom
4949 late List <DishEntity > unikidsDishes = [];
5050 late List <Failure > failures = [];
5151
52- late int selectedDay;
52+ // Weekday to show as selected
53+ int selectedDay = - 1 ;
5354
54- DateTime selectedDate = DateTime .now ().weekday == 6
55- ? DateTime .now ().subtract (const Duration (days: 1 ))
56- : DateTime .now ().weekday == 7
57- ? DateTime .now ().subtract (const Duration (days: 2 ))
58- : DateTime .now ();
55+ // Weekday that is selected
56+ DateTime selectedDate = DateTime .now ();
5957
6058 StreamController <DateTime > streamController = StreamController <DateTime >.broadcast ();
6159
@@ -258,28 +256,17 @@ class MensaPageState extends State<MensaPage> with WidgetsBindingObserver, Autom
258256 @override
259257 void initState () {
260258 super .initState ();
259+ final DateTime today = DateTime .now ();
260+ final DateTime startOfWeek = today.subtract (Duration (days: today.weekday - 1 ));
261261
262- // Add observer in order to listen to `didChangeAppLifecycleState`
263- WidgetsBinding .instance. addObserver ( this ) ;
262+ // Choose selected day: Mo -> 0 ... Fr -> 4, Sa & So -> 5 (next monday)
263+ selectedDay = today.weekday > 5 ? today.weekday - 1 : 5 ;
264264
265- switch (DateTime .now ().weekday) {
266- case 1 : // Monday
267- selectedDay = 0 ;
268- break ;
269- case 2 : // Tuesday
270- selectedDay = 1 ;
271- break ;
272- case 3 : // Wednesday
273- selectedDay = 2 ;
274- break ;
275- case 4 : // Thursday
276- selectedDay = 3 ;
277- break ;
278- default : // Friday, Saturday or Sunday
279- selectedDay = 4 ;
280- break ;
281- }
265+ // Choose selected date to load data: today or next monday on weekend
266+ selectedDate = today.weekday > 5 ? today : startOfWeek.add (const Duration (days: 7 ));
282267
268+ // Add observer in order to listen to `didChangeAppLifecycleState`
269+ WidgetsBinding .instance.addObserver (this );
283270 loadData ();
284271 }
285272
0 commit comments