implemented current schoolyear API and dynamic timetable scroll boundaries, added handling for out-of-range errors to narrow accessible dates, optimized holiday region rendering by collapsing overlaps, and refined holiday tile UI
This commit is contained in:
@@ -86,6 +86,40 @@ class CustomWorkWeekCalendarState extends State<CustomWorkWeekCalendar> {
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant CustomWorkWeekCalendar oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.minDate == oldWidget.minDate &&
|
||||
widget.maxDate == oldWidget.maxDate) {
|
||||
return;
|
||||
}
|
||||
// Boundaries changed (e.g. school-year payload finished loading after
|
||||
// the initial mount with the conservative fallback). Recompute the
|
||||
// range and snap the controller to the page that still represents the
|
||||
// currently visible week so the user doesn't get yanked around.
|
||||
final newFirstMonday = _mondayOf(widget.minDate);
|
||||
final newLastMonday = _mondayOf(widget.maxDate);
|
||||
final newTotalWeeks =
|
||||
newLastMonday.difference(newFirstMonday).inDays ~/ 7 + 1;
|
||||
final visibleWeekStart = _firstMonday.add(
|
||||
Duration(days: _currentWeekIndex * 7),
|
||||
);
|
||||
final newIndex = visibleWeekStart
|
||||
.difference(newFirstMonday)
|
||||
.inDays
|
||||
~/
|
||||
7;
|
||||
final clampedIndex = newIndex.clamp(0, newTotalWeeks - 1);
|
||||
final oldController = _pageController;
|
||||
_pageController = PageController(initialPage: clampedIndex);
|
||||
oldController.dispose();
|
||||
setState(() {
|
||||
_firstMonday = newFirstMonday;
|
||||
_totalWeeks = newTotalWeeks;
|
||||
_currentWeekIndex = clampedIndex;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pageController.dispose();
|
||||
|
||||
@@ -62,33 +62,47 @@ class SpecialRegionsBuilder {
|
||||
|
||||
static String _dayKey(DateTime d) => '${d.year}-${d.month}-${d.day}';
|
||||
|
||||
Iterable<TimeRegion> _buildHolidayRegions() => holidays.result.expand((
|
||||
holiday,
|
||||
) {
|
||||
final startDay = WebuntisTime.parse(holiday.startDate, 0);
|
||||
final endDay = WebuntisTime.parse(holiday.endDate, 0);
|
||||
// Webuntis treats endDate inclusively (last day of the break) — the
|
||||
// `+ 1` covers single-day public holidays (where startDate == endDate)
|
||||
// and the final day of a multi-day vacation, both of which would
|
||||
// otherwise be skipped.
|
||||
final dayCount = endDay.difference(startDay).inDays + 1;
|
||||
final days = List<DateTime>.generate(
|
||||
dayCount,
|
||||
(i) => startDay.add(Duration(days: i)),
|
||||
);
|
||||
Iterable<TimeRegion> _buildHolidayRegions() {
|
||||
// Multiple Webuntis holiday entries can cover the same day (e.g. a
|
||||
// public holiday falling inside a vacation period). Collapse them
|
||||
// per-day so we emit exactly one TimeRegion per day and the
|
||||
// overlapping labels don't render on top of each other.
|
||||
final byDay = <String, _HolidayDay>{};
|
||||
for (final holiday in holidays.result) {
|
||||
final startDay = WebuntisTime.parse(holiday.startDate, 0);
|
||||
final endDay = WebuntisTime.parse(holiday.endDate, 0);
|
||||
// Webuntis treats endDate inclusively (last day of the break) — the
|
||||
// `+ 1` covers single-day public holidays (where startDate == endDate)
|
||||
// and the final day of a multi-day vacation, both of which would
|
||||
// otherwise be skipped.
|
||||
final dayCount = endDay.difference(startDay).inDays + 1;
|
||||
for (var i = 0; i < dayCount; i++) {
|
||||
final day = startDay.add(Duration(days: i));
|
||||
final key = _dayKey(day);
|
||||
byDay.putIfAbsent(key, () => _HolidayDay(day, [])).names.add(
|
||||
holiday.name,
|
||||
);
|
||||
}
|
||||
}
|
||||
final gridStartHour = kCalendarStartHour.floor();
|
||||
final gridStartMinute = ((kCalendarStartHour - gridStartHour) * 60).round();
|
||||
final gridEndHour = kCalendarEndHour.floor();
|
||||
final gridEndMinute = ((kCalendarEndHour - gridEndHour) * 60).round();
|
||||
return days.map(
|
||||
(day) => TimeRegion(
|
||||
startTime: day.copyWith(hour: gridStartHour, minute: gridStartMinute),
|
||||
endTime: day.copyWith(hour: gridEndHour, minute: gridEndMinute),
|
||||
text: '$kTimeRegionHolidayPrefix${holiday.name}',
|
||||
return byDay.values.map(
|
||||
(entry) => TimeRegion(
|
||||
startTime: entry.day.copyWith(
|
||||
hour: gridStartHour,
|
||||
minute: gridStartMinute,
|
||||
),
|
||||
endTime: entry.day.copyWith(
|
||||
hour: gridEndHour,
|
||||
minute: gridEndMinute,
|
||||
),
|
||||
text: '$kTimeRegionHolidayPrefix${entry.names.join(" / ")}',
|
||||
color: disabledColor.withAlpha(50),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
TimeRegion _breakRegion(DateTime start, Duration duration) => TimeRegion(
|
||||
startTime: start,
|
||||
@@ -98,3 +112,9 @@ class SpecialRegionsBuilder {
|
||||
iconData: Icons.restaurant,
|
||||
);
|
||||
}
|
||||
|
||||
class _HolidayDay {
|
||||
final DateTime day;
|
||||
final List<String> names;
|
||||
_HolidayDay(this.day, this.names);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,10 @@ class TimeRegionTile extends StatelessWidget {
|
||||
quarterTurns: 1,
|
||||
child: Text(
|
||||
text.substring(kTimeRegionHolidayPrefix.length),
|
||||
maxLines: 1,
|
||||
maxLines: 2,
|
||||
softWrap: true,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 15,
|
||||
|
||||
Reference in New Issue
Block a user