From b99769e19212bdb39ddc35d9dd17284746938c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Sun, 7 Jan 2024 21:19:39 +0100 Subject: [PATCH] Enabled Webuntis Holiday query Implemented Holiday view in Timetable Hide other special time regions like breaks when shown in holiday --- lib/extensions/dateTime.dart | 5 ++ lib/model/timetable/timetableProps.dart | 22 ++--- lib/view/pages/talk/chatView.dart | 7 +- .../pages/timetable/timeRegionComponent.dart | 4 +- lib/view/pages/timetable/timetable.dart | 89 ++++++++++++------- 5 files changed, 76 insertions(+), 51 deletions(-) create mode 100644 lib/extensions/dateTime.dart diff --git a/lib/extensions/dateTime.dart b/lib/extensions/dateTime.dart new file mode 100644 index 0000000..e1e0c1e --- /dev/null +++ b/lib/extensions/dateTime.dart @@ -0,0 +1,5 @@ +extension IsSameDay on DateTime { + bool isSameDay(DateTime other) { + return year == other.year && month == other.month && day == other.day; + } +} \ No newline at end of file diff --git a/lib/model/timetable/timetableProps.dart b/lib/model/timetable/timetableProps.dart index 40545eb..f903fa3 100644 --- a/lib/model/timetable/timetableProps.dart +++ b/lib/model/timetable/timetableProps.dart @@ -1,8 +1,7 @@ -import 'dart:convert'; - import 'package:intl/intl.dart'; import '../../api/apiResponse.dart'; +import '../../api/webuntis/queries/getHolidays/getHolidaysCache.dart'; import '../../api/webuntis/queries/getHolidays/getHolidaysResponse.dart'; import '../../api/webuntis/queries/getRooms/getRoomsCache.dart'; import '../../api/webuntis/queries/getRooms/getRoomsResponse.dart'; @@ -79,15 +78,16 @@ class TimetableProps extends DataHolder { } ); - // GetHolidaysCache( // TODO is this fixed by webuntis? miese kriese - // onUpdate: (GetHolidaysResponse data) => { - // _getHolidaysResponse = data, - // notifyListeners(), - // } - // ); - _getHolidaysResponse = GetHolidaysResponse.fromJson(jsonDecode(""" - {"jsonrpc":"2.0","id":"ID","result":[]} - """)); + GetHolidaysCache( // This broke in the past. Below here is backup simulation for an empty holiday block + onUpdate: (GetHolidaysResponse data) => { + _getHolidaysResponse = data, + notifyListeners(), + } + ); + // _getHolidaysResponse = GetHolidaysResponse.fromJson(jsonDecode(""" + // {"jsonrpc":"2.0","id":"ID","result":[]} + // """)); + notifyListeners(); } diff --git a/lib/view/pages/talk/chatView.dart b/lib/view/pages/talk/chatView.dart index a2c0c82..b26a1df 100644 --- a/lib/view/pages/talk/chatView.dart +++ b/lib/view/pages/talk/chatView.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:loader_overlay/loader_overlay.dart'; +import 'package:marianum_mobile/extensions/dateTime.dart'; import 'package:provider/provider.dart'; import '../../../api/marianumcloud/talk/chat/getChatResponse.dart'; @@ -39,10 +40,6 @@ class _ChatViewState extends State { Provider.of(context, listen: false).setQueryToken(widget.room.token); } - bool isSameDay(DateTime date1, DateTime date2) { - return date1.year == date2.year && date1.month == date2.month && date1.day == date2.day; - } - @override Widget build(BuildContext context) { return Consumer( @@ -57,7 +54,7 @@ class _ChatViewState extends State { if(element.systemMessage.contains("reaction")) return; - if(!isSameDay(elementDate, lastDate)) { + if(!elementDate.isSameDay(lastDate)) { lastDate = elementDate; messages.add(ChatBubble( context: context, diff --git a/lib/view/pages/timetable/timeRegionComponent.dart b/lib/view/pages/timetable/timeRegionComponent.dart index bbb2d41..b0e1e3d 100644 --- a/lib/view/pages/timetable/timeRegionComponent.dart +++ b/lib/view/pages/timetable/timeRegionComponent.dart @@ -31,10 +31,10 @@ class _TimeRegionComponentState extends State { alignment: Alignment.center, child: Column( children: [ - const SizedBox(height: 5), + const SizedBox(height: 15), const Icon(Icons.cake), const Text("FREI"), - const SizedBox(height: 5), + const SizedBox(height: 10), RotatedBox( quarterTurns: 1, child: Text( diff --git a/lib/view/pages/timetable/timetable.dart b/lib/view/pages/timetable/timetable.dart index 92d3716..16cd2ea 100644 --- a/lib/view/pages/timetable/timetable.dart +++ b/lib/view/pages/timetable/timetable.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:marianum_mobile/extensions/dateTime.dart'; import 'package:provider/provider.dart'; import 'package:syncfusion_flutter_calendar/calendar.dart'; @@ -154,46 +155,68 @@ class _TimetableState extends State { super.dispose(); } - List _buildSpecialTimeRegions(GetHolidaysResponse holidays, ) { + List _buildSpecialTimeRegions(GetHolidaysResponse holidays) { DateTime lastMonday = DateTime.now().subtract(Duration(days: DateTime.now().weekday - 1)); DateTime firstBreak = lastMonday.copyWith(hour: 10, minute: 15); DateTime secondBreak = lastMonday.copyWith(hour: 13, minute: 50); DateTime beforeSchool = lastMonday.copyWith(hour: 7, minute: 30); - return [ - ...holidays.result.map((e) { - return TimeRegion( - startTime: _parseWebuntisTimestamp(e.startDate, 755), - endTime: _parseWebuntisTimestamp(e.startDate, 1630), - text: 'holiday:${e.name}', - color: Theme.of(context).disabledColor.withAlpha(50), - iconData: Icons.holiday_village_outlined - ); - }), + Iterable holidayList = holidays.result.map((holiday) { + DateTime startDay = _parseWebuntisTimestamp(holiday.startDate, 0); + int dayCount = _parseWebuntisTimestamp(holiday.endDate, 0) + .difference(startDay) + .inDays; + List days = List.generate(dayCount, (index) => startDay.add(Duration(days: index))); - TimeRegion( - startTime: firstBreak, - endTime: firstBreak.add(const Duration(minutes: 20)), + return days.map((holidayDay) { + return TimeRegion( + startTime: holidayDay.copyWith(hour: 07, minute: 55), + endTime: holidayDay.copyWith(hour: 16, minute: 30), + text: 'holiday:${holiday.name}', + color: Theme + .of(context) + .disabledColor + .withAlpha(50), + iconData: Icons.holiday_village_outlined + ); + }); + }).expand((e) => e); + + bool isInHoliday(DateTime time) { + return holidayList.any((element) => element.startTime.isSameDay(time)); + } + + return [ + ...holidayList, + + if(!isInHoliday(firstBreak)) + TimeRegion( + startTime: firstBreak, + endTime: firstBreak.add(const Duration(minutes: 20)), + recurrenceRule: 'FREQ=DAILY;INTERVAL=1;COUNT=5', + text: 'centerIcon', + color: Theme.of(context).primaryColor.withAlpha(50), + iconData: Icons.restaurant + ), + + if(!isInHoliday(secondBreak)) + TimeRegion( + startTime: secondBreak, + endTime: secondBreak.add(const Duration(minutes: 15)), + recurrenceRule: 'FREQ=DAILY;INTERVAL=1;COUNT=5', + text: 'centerIcon', + color: Theme.of(context).primaryColor.withAlpha(50), + iconData: Icons.restaurant + ), + + if(!isInHoliday(beforeSchool)) + TimeRegion( + startTime: beforeSchool, + endTime: beforeSchool.add(const Duration(minutes: 25)), recurrenceRule: 'FREQ=DAILY;INTERVAL=1;COUNT=5', - text: 'centerIcon', - color: Theme.of(context).primaryColor.withAlpha(50), - iconData: Icons.restaurant - ), - TimeRegion( - startTime: secondBreak, - endTime: secondBreak.add(const Duration(minutes: 15)), - recurrenceRule: 'FREQ=DAILY;INTERVAL=1;COUNT=5', - text: 'centerIcon', - color: Theme.of(context).primaryColor.withAlpha(50), - iconData: Icons.restaurant - ), - TimeRegion( - startTime: beforeSchool, - endTime: beforeSchool.add(const Duration(minutes: 25)), - recurrenceRule: 'FREQ=DAILY;INTERVAL=1;COUNT=5', - color: Theme.of(context).disabledColor.withAlpha(50), - text: "centerIcon", - ), + color: Theme.of(context).disabledColor.withAlpha(50), + text: "centerIcon", + ), ]; }