From c2f05da96e772eb9cfe1c36d5b64dd0e7b61498b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elias=20M=C3=BCller?= <elias@elias-mueller.com>
Date: Sat, 17 Feb 2024 13:26:40 +0100
Subject: [PATCH] Added refreshindicator for timetable

---
 lib/view/pages/timetable/timetable.dart | 106 +++++++++++++-----------
 1 file changed, 56 insertions(+), 50 deletions(-)

diff --git a/lib/view/pages/timetable/timetable.dart b/lib/view/pages/timetable/timetable.dart
index 7f44aaf..a5d0d90 100644
--- a/lib/view/pages/timetable/timetable.dart
+++ b/lib/view/pages/timetable/timetable.dart
@@ -99,63 +99,69 @@ class _TimetableState extends State<Timetable> {
 
           GetHolidaysResponse holidays = value.getHolidaysResponse;
 
-          return GestureDetector(
-            onScaleStart: (details) => baseElementScale = elementScale,
-            onScaleUpdate: (details) {
-              setState(() {
-                elementScale = (baseElementScale * details.scale).clamp(40, 80);
-              });
-            },
-            onScaleEnd: (details) {
-              settings.val(write: true).timetableSettings.zoom = elementScale;
-            },
+          return RefreshIndicator(
+              child: GestureDetector(
+                onScaleStart: (details) => baseElementScale = elementScale,
+                onScaleUpdate: (details) {
+                  setState(() {
+                    elementScale = (baseElementScale * details.scale).clamp(40, 80);
+                  });
+                },
+                onScaleEnd: (details) {
+                  settings.val(write: true).timetableSettings.zoom = elementScale;
+                },
 
-            child: SfCalendar(
-              view: CalendarView.workWeek,
-              dataSource: _buildTableEvents(value),
+                child: SfCalendar(
+                  view: CalendarView.workWeek,
+                  dataSource: _buildTableEvents(value),
 
-              maxDate: DateTime.now().add(const Duration(days: 7)).nextWeekday(DateTime.saturday),
-              minDate: DateTime.now().subtract(const Duration (days: 14)).nextWeekday(DateTime.sunday),
+                  maxDate: DateTime.now().add(const Duration(days: 7)).nextWeekday(DateTime.saturday),
+                  minDate: DateTime.now().subtract(const Duration (days: 14)).nextWeekday(DateTime.sunday),
 
-              controller: controller,
+                  controller: controller,
 
-              onViewChanged: (ViewChangedDetails details) {
-                WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
-                  Provider.of<TimetableProps>(context, listen: false).updateWeek(details.visibleDates.first, details.visibleDates.last);
-                });
-              },
+                  onViewChanged: (ViewChangedDetails details) {
+                    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
+                      Provider.of<TimetableProps>(context, listen: false).updateWeek(details.visibleDates.first, details.visibleDates.last);
+                    });
+                  },
 
-              onTap: (calendarTapDetails) {
-                if(calendarTapDetails.appointments == null) return;
-                Appointment tapped = calendarTapDetails.appointments!.first;
-                AppointmentDetails.show(context, value, tapped);
-              },
+                  onTap: (calendarTapDetails) {
+                    if(calendarTapDetails.appointments == null) return;
+                    Appointment tapped = calendarTapDetails.appointments!.first;
+                    AppointmentDetails.show(context, value, tapped);
+                  },
 
-              firstDayOfWeek: DateTime.monday,
-              specialRegions: _buildSpecialTimeRegions(holidays),
-              timeSlotViewSettings: TimeSlotViewSettings(
-                startHour: 07.5,
-                endHour: 16.5,
-                timeInterval: const Duration(minutes: 30),
-                timeFormat: "HH:mm",
-                dayFormat: "EE",
-                timeIntervalHeight: elementScale,
+                  firstDayOfWeek: DateTime.monday,
+                  specialRegions: _buildSpecialTimeRegions(holidays),
+                  timeSlotViewSettings: TimeSlotViewSettings(
+                    startHour: 07.5,
+                    endHour: 16.5,
+                    timeInterval: const Duration(minutes: 30),
+                    timeFormat: "HH:mm",
+                    dayFormat: "EE",
+                    timeIntervalHeight: elementScale,
+                  ),
+
+                  timeRegionBuilder: (BuildContext context, TimeRegionDetails timeRegionDetails) => TimeRegionComponent(details: timeRegionDetails),
+                  appointmentBuilder: (BuildContext context, CalendarAppointmentDetails details) => AppointmentComponent(
+                      details: details,
+                      crossedOut: _isCrossedOut(details)
+                  ),
+
+                  headerHeight: 0,
+                  selectionDecoration: const BoxDecoration(),
+
+                  allowAppointmentResize: false,
+                  allowDragAndDrop: false,
+                  allowViewNavigation: false,
+                ),
               ),
-
-              timeRegionBuilder: (BuildContext context, TimeRegionDetails timeRegionDetails) => TimeRegionComponent(details: timeRegionDetails),
-              appointmentBuilder: (BuildContext context, CalendarAppointmentDetails details) => AppointmentComponent(
-                  details: details,
-                  crossedOut: _isCrossedOut(details)
-              ),
-
-              headerHeight: 0,
-              selectionDecoration: const BoxDecoration(),
-
-              allowAppointmentResize: false,
-              allowDragAndDrop: false,
-              allowViewNavigation: false,
-            ),
-          );
+              onRefresh: () async {
+                Provider.of<TimetableProps>(context, listen: false).run(renew: true);
+                return Future.delayed(const Duration(seconds: 3));
+              }
+            );
         },
       ),
     );