claude refactor
This commit is contained in:
@@ -1,26 +1,25 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../extensions/dateTime.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncfusion_flutter_calendar/calendar.dart';
|
||||
|
||||
import '../../../api/webuntis/queries/getHolidays/getHolidaysResponse.dart';
|
||||
import '../../../api/webuntis/queries/getTimetable/getTimetableResponse.dart';
|
||||
import '../../../model/timetable/timetableProps.dart';
|
||||
import '../../../storage/base/settingsProvider.dart';
|
||||
import '../../../widget/loadingSpinner.dart';
|
||||
import '../../../widget/placeholderView.dart';
|
||||
import 'appointmenetComponent.dart';
|
||||
import 'appointmentDetails.dart';
|
||||
import 'arbitraryAppointment.dart';
|
||||
import 'customTimetableColors.dart';
|
||||
import 'customTimetableEventEditDialog.dart';
|
||||
import 'timeRegionComponent.dart';
|
||||
import 'timetableEvents.dart';
|
||||
import 'timetableNameMode.dart';
|
||||
import 'viewCustomTimetableEvents.dart';
|
||||
import '../../../extensions/dateTime.dart';
|
||||
import '../../../state/app/infrastructure/loadableState/view/loadable_state_consumer.dart';
|
||||
import '../../../state/app/modules/timetable/bloc/timetable_bloc.dart';
|
||||
import '../../../state/app/modules/timetable/bloc/timetable_state.dart';
|
||||
import '../../../state/app/modules/settings/bloc/settings_cubit.dart';
|
||||
import 'custom_events/custom_event_edit_dialog.dart';
|
||||
import 'custom_events/custom_events_view.dart';
|
||||
import 'data/arbitrary_appointment.dart';
|
||||
import 'data/timetable_appointment_factory.dart';
|
||||
import 'details/appointment_details_dispatcher.dart';
|
||||
import 'widgets/appointment_tile.dart';
|
||||
import 'widgets/lesson_appointment_source.dart';
|
||||
import 'widgets/special_regions_builder.dart';
|
||||
import 'widgets/time_region_tile.dart';
|
||||
|
||||
enum _CalendarAction { addEvent, viewEvents }
|
||||
|
||||
class Timetable extends StatefulWidget {
|
||||
const Timetable({super.key});
|
||||
@@ -29,362 +28,152 @@ class Timetable extends StatefulWidget {
|
||||
State<Timetable> createState() => _TimetableState();
|
||||
}
|
||||
|
||||
enum CalendarActions { addEvent, viewEvents }
|
||||
|
||||
class _TimetableState extends State<Timetable> {
|
||||
CalendarController controller = CalendarController();
|
||||
late Timer updateTimings;
|
||||
late final SettingsProvider settings;
|
||||
final CalendarController _controller = CalendarController();
|
||||
late Timer _highlightTicker;
|
||||
|
||||
LessonAppointmentSource? _cachedSource;
|
||||
int? _lastDataVersion;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
settings = Provider.of<SettingsProvider>(context, listen: false);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
||||
Provider.of<TimetableProps>(context, listen: false).run();
|
||||
});
|
||||
|
||||
controller.displayDate = DateTime.now().add(const Duration(days: 2));
|
||||
|
||||
updateTimings = Timer.periodic(const Duration(seconds: 30), (Timer t) => setState((){}));
|
||||
|
||||
super.initState();
|
||||
_controller.displayDate = _initialDisplayDate();
|
||||
|
||||
_highlightTicker = Timer.periodic(const Duration(seconds: 30), (_) {
|
||||
if (mounted) setState(() => _cachedSource = null);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Stunden & Vertretungsplan'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.home_outlined),
|
||||
onPressed: () {
|
||||
controller.displayDate = DateTime.now().add(const Duration(days: 2));
|
||||
}
|
||||
),
|
||||
PopupMenuButton<CalendarActions>(
|
||||
icon: const Icon(Icons.edit_calendar_outlined),
|
||||
itemBuilder: (context) => CalendarActions.values.map(
|
||||
(e) {
|
||||
String title;
|
||||
Icon icon;
|
||||
switch(e) {
|
||||
case CalendarActions.addEvent:
|
||||
title = 'Kalendereintrag hinzufügen';
|
||||
icon = const Icon(Icons.add);
|
||||
case CalendarActions.viewEvents:
|
||||
title = 'Kalendereinträge anzeigen';
|
||||
icon = const Icon(Icons.perm_contact_calendar_outlined);
|
||||
}
|
||||
return PopupMenuItem<CalendarActions>(
|
||||
value: e,
|
||||
child: ListTile(
|
||||
title: Text(title),
|
||||
leading: icon,
|
||||
)
|
||||
);
|
||||
}
|
||||
).toList(),
|
||||
onSelected: (value) {
|
||||
switch(value) {
|
||||
case CalendarActions.addEvent:
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => const CustomTimetableEventEditDialog(),
|
||||
barrierDismissible: false,
|
||||
);
|
||||
case CalendarActions.viewEvents:
|
||||
Navigator.of(context).push(MaterialPageRoute(builder: (context) => const ViewCustomTimetableEvents()));
|
||||
}
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
body: Consumer<TimetableProps>(
|
||||
builder: (context, value, child) {
|
||||
|
||||
if(value.hasError) {
|
||||
return PlaceholderView(
|
||||
icon: Icons.calendar_month,
|
||||
text: 'Webuntis error: ${value.error.toString()}',
|
||||
button: TextButton(
|
||||
child: const Text('Neu laden'),
|
||||
onPressed: () {
|
||||
controller.displayDate = DateTime.now().add(const Duration(days: 2));
|
||||
Provider.of<TimetableProps>(context, listen: false).resetWeek();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if(value.primaryLoading()) return const LoadingSpinner();
|
||||
|
||||
var holidays = value.getHolidaysResponse;
|
||||
|
||||
return RefreshIndicator(
|
||||
child: SfCalendar(
|
||||
timeZone: 'W. Europe Standard Time',
|
||||
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),
|
||||
|
||||
controller: controller,
|
||||
|
||||
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);
|
||||
},
|
||||
|
||||
firstDayOfWeek: DateTime.monday,
|
||||
specialRegions: _buildSpecialTimeRegions(holidays),
|
||||
timeSlotViewSettings: const TimeSlotViewSettings(
|
||||
startHour: 07.5,
|
||||
endHour: 16.5,
|
||||
timeInterval: Duration(minutes: 30),
|
||||
timeFormat: 'HH:mm',
|
||||
dayFormat: 'EE',
|
||||
timeIntervalHeight: 40,
|
||||
),
|
||||
|
||||
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));
|
||||
}
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
updateTimings.cancel();
|
||||
_highlightTicker.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
List<TimeRegion> _buildSpecialTimeRegions(GetHolidaysResponse holidays) {
|
||||
var lastMonday = DateTime.now().subtract(const Duration(days: 14)).nextWeekday(DateTime.monday);
|
||||
var firstBreak = lastMonday.copyWith(hour: 10, minute: 15);
|
||||
var secondBreak = lastMonday.copyWith(hour: 13, minute: 50);
|
||||
DateTime _initialDisplayDate() => DateTime.now().add(const Duration(days: 2));
|
||||
|
||||
var holidayList = holidays.result.map((holiday) {
|
||||
var startDay = _parseWebuntisTimestamp(holiday.startDate, 0);
|
||||
var dayCount = _parseWebuntisTimestamp(holiday.endDate, 0)
|
||||
.difference(startDay)
|
||||
.inDays;
|
||||
var days = List<DateTime>.generate(dayCount, (index) => startDay.add(Duration(days: index)));
|
||||
|
||||
return days.map((holidayDay) => 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) => 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',
|
||||
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',
|
||||
text: 'centerIcon',
|
||||
color: Theme.of(context).primaryColor.withAlpha(50),
|
||||
iconData: Icons.restaurant
|
||||
),
|
||||
];
|
||||
void _jumpToToday() {
|
||||
_controller.displayDate = _initialDisplayDate();
|
||||
}
|
||||
|
||||
List<GetTimetableResponseObject> _removeDuplicates(TimetableProps data, Duration maxTimeBetweenDouble) {
|
||||
|
||||
var timetableList = data.getTimetableResponse.result.toList();
|
||||
|
||||
if(timetableList.isEmpty) return timetableList;
|
||||
|
||||
timetableList.sort((a, b) => _parseWebuntisTimestamp(a.date, a.startTime).compareTo(_parseWebuntisTimestamp(b.date, b.startTime)));
|
||||
|
||||
var previousElement = timetableList.first;
|
||||
for(var i = 1; i < timetableList.length; i++) {
|
||||
var currentElement = timetableList.elementAt(i);
|
||||
|
||||
bool isSameLesson() {
|
||||
var currentSubjectId = currentElement.su.firstOrNull?.id;
|
||||
var previousSubjectId = previousElement.su.firstOrNull?.id;
|
||||
|
||||
if(currentSubjectId == null || previousSubjectId == null || currentSubjectId != previousSubjectId) return false;
|
||||
|
||||
var currentRoomId = currentElement.ro.firstOrNull?.id;
|
||||
var previousRoomId = previousElement.ro.firstOrNull?.id;
|
||||
|
||||
if(currentRoomId != previousRoomId) return false;
|
||||
|
||||
var currentTeacherId = currentElement.te.firstOrNull?.id;
|
||||
var previousTeacherId = previousElement.te.firstOrNull?.id;
|
||||
|
||||
if(currentTeacherId != previousTeacherId) return false;
|
||||
|
||||
var currentStatusCode = currentElement.code;
|
||||
var previousStatusCode = previousElement.code;
|
||||
|
||||
if(currentStatusCode != previousStatusCode) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isNotSeparated() => _parseWebuntisTimestamp(previousElement.date, previousElement.endTime).add(maxTimeBetweenDouble)
|
||||
.isSameOrAfter(_parseWebuntisTimestamp(currentElement.date, currentElement.startTime));
|
||||
|
||||
if(isSameLesson() && isNotSeparated()) {
|
||||
previousElement.endTime = currentElement.endTime;
|
||||
timetableList.remove(currentElement);
|
||||
i--;
|
||||
} else {
|
||||
previousElement = currentElement;
|
||||
}
|
||||
}
|
||||
|
||||
return timetableList;
|
||||
}
|
||||
|
||||
TimetableEvents _buildTableEvents(TimetableProps data) {
|
||||
|
||||
var timetableList = data.getTimetableResponse.result.toList();
|
||||
|
||||
if(settings.val().timetableSettings.connectDoubleLessons) {
|
||||
timetableList = _removeDuplicates(data, const Duration(minutes: 5));
|
||||
}
|
||||
|
||||
var appointments = timetableList.map((element) {
|
||||
|
||||
var rooms = data.getRoomsResponse;
|
||||
var subjects = data.getSubjectsResponse;
|
||||
|
||||
try {
|
||||
var startTime = _parseWebuntisTimestamp(element.date, element.startTime);
|
||||
var endTime = _parseWebuntisTimestamp(element.date, element.endTime);
|
||||
|
||||
var subject = subjects.result.firstWhereOrNull((subject) => subject.id == element.su.firstOrNull?.id);
|
||||
var subjectName = 'Unbekannt';
|
||||
if(subject != null) {
|
||||
subjectName = {
|
||||
TimetableNameMode.name: subject.name,
|
||||
TimetableNameMode.longName: subject.longName,
|
||||
TimetableNameMode.alternateName: subject.alternateName,
|
||||
}[settings.val().timetableSettings.timetableNameMode]!;
|
||||
}
|
||||
|
||||
return Appointment(
|
||||
id: ArbitraryAppointment(webuntis: element),
|
||||
startTime: startTime,
|
||||
endTime: endTime,
|
||||
subject: subjectName,
|
||||
location: ''
|
||||
'${rooms.result.firstWhereOrNull((room) => room.id == element.ro.firstOrNull?.id)?.name ?? 'Unbekannt'}'
|
||||
'\n'
|
||||
'${element.te.firstOrNull?.longname ?? 'Unbekannt'}',
|
||||
notes: element.activityType,
|
||||
color: _getEventColor(element, startTime, endTime),
|
||||
void _onAction(_CalendarAction action) {
|
||||
switch (action) {
|
||||
case _CalendarAction.addEvent:
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (_) => const CustomEventEditDialog(),
|
||||
barrierDismissible: false,
|
||||
);
|
||||
} catch(e) {
|
||||
var endTime = _parseWebuntisTimestamp(element.date, element.endTime);
|
||||
return Appointment(
|
||||
id: ArbitraryAppointment(webuntis: element),
|
||||
startTime: _parseWebuntisTimestamp(element.date, element.startTime),
|
||||
endTime: endTime,
|
||||
subject: 'Änderung',
|
||||
notes: element.info,
|
||||
location: 'Unbekannt',
|
||||
color: const Color(0xff404040),
|
||||
startTimeZone: '',
|
||||
endTimeZone: '',
|
||||
);
|
||||
}
|
||||
}).toList();
|
||||
|
||||
appointments.addAll(data.getCustomTimetableEventResponse.events.map((customEvent) => Appointment(
|
||||
id: ArbitraryAppointment(custom: customEvent),
|
||||
startTime: customEvent.startDate,
|
||||
endTime: customEvent.endDate,
|
||||
location: customEvent.description,
|
||||
subject: customEvent.title,
|
||||
recurrenceRule: customEvent.rrule,
|
||||
color: TimetableColors.getColorFromString(customEvent.color ?? TimetableColors.defaultColor.name),
|
||||
startTimeZone: '',
|
||||
endTimeZone: '',
|
||||
)));
|
||||
|
||||
return TimetableEvents(appointments);
|
||||
}
|
||||
|
||||
DateTime _parseWebuntisTimestamp(int date, int time) {
|
||||
var timeString = time.toString().padLeft(4, '0');
|
||||
return DateTime.parse('$date ${timeString.substring(0, 2)}:${timeString.substring(2, 4)}');
|
||||
}
|
||||
|
||||
Color _getEventColor(GetTimetableResponseObject webuntisElement, DateTime startTime, DateTime endTime) {
|
||||
// Cancelled
|
||||
if(webuntisElement.code == 'cancelled') return const Color(0xff000000);
|
||||
|
||||
// Any changes or no teacher at this element
|
||||
if(webuntisElement.code == 'irregular' || webuntisElement.te.first.id == 0) return const Color(0xff8F19B3);
|
||||
|
||||
// Teacher has changed
|
||||
if(webuntisElement.te.any((element) => element.orgname != null)) return const Color(0xFF29639B);
|
||||
|
||||
// Event was in the past
|
||||
if(endTime.isBefore(DateTime.now())) return Theme.of(context).primaryColor;
|
||||
|
||||
// Event takes currently place
|
||||
if(endTime.isAfter(DateTime.now()) && startTime.isBefore(DateTime.now())) return Theme.of(context).primaryColor.withRed(200);
|
||||
|
||||
// Fallback
|
||||
return Theme.of(context).primaryColor;
|
||||
}
|
||||
|
||||
bool _isCrossedOut(CalendarAppointmentDetails calendarEntry) {
|
||||
var appointment = calendarEntry.appointments.first.id as ArbitraryAppointment;
|
||||
if(appointment.hasWebuntis()) {
|
||||
return appointment.webuntis!.code == 'cancelled';
|
||||
case _CalendarAction.viewEvents:
|
||||
Navigator.of(context).push(MaterialPageRoute(builder: (_) => const CustomEventsView()));
|
||||
}
|
||||
}
|
||||
|
||||
LessonAppointmentSource _appointmentSource(TimetableState state) {
|
||||
if (_cachedSource != null && _lastDataVersion == state.dataVersion) {
|
||||
return _cachedSource!;
|
||||
}
|
||||
_lastDataVersion = state.dataVersion;
|
||||
|
||||
final settings = context.read<SettingsCubit>();
|
||||
final appointments = TimetableAppointmentFactory(
|
||||
lessons: state.getAllKnownLessons().toList(),
|
||||
customEvents: state.customEvents?.events ?? const [],
|
||||
rooms: state.rooms!,
|
||||
subjects: state.subjects!,
|
||||
settings: settings.val().timetableSettings,
|
||||
colorScheme: Theme.of(context).colorScheme,
|
||||
now: DateTime.now(),
|
||||
).build();
|
||||
|
||||
return _cachedSource = LessonAppointmentSource(appointments);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final bloc = context.read<TimetableBloc>();
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Stunden & Vertretungsplan'),
|
||||
actions: [
|
||||
IconButton(icon: const Icon(Icons.home_outlined), onPressed: _jumpToToday),
|
||||
PopupMenuButton<_CalendarAction>(
|
||||
icon: const Icon(Icons.edit_calendar_outlined),
|
||||
onSelected: _onAction,
|
||||
itemBuilder: (_) => const [
|
||||
PopupMenuItem(
|
||||
value: _CalendarAction.addEvent,
|
||||
child: ListTile(title: Text('Kalendereintrag hinzufügen'), leading: Icon(Icons.add)),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: _CalendarAction.viewEvents,
|
||||
child: ListTile(
|
||||
title: Text('Kalendereinträge anzeigen'),
|
||||
leading: Icon(Icons.perm_contact_calendar_outlined),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
body: LoadableStateConsumer<TimetableBloc, TimetableState>(
|
||||
child: (state, _) => _calendar(state, bloc),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _calendar(TimetableState state, TimetableBloc bloc) {
|
||||
if (!state.hasReferenceData) return const SizedBox.shrink();
|
||||
|
||||
return SfCalendar(
|
||||
timeZone: 'W. Europe Standard Time',
|
||||
view: CalendarView.workWeek,
|
||||
dataSource: _appointmentSource(state),
|
||||
maxDate: DateTime.now().add(const Duration(days: 7)).nextWeekday(DateTime.saturday),
|
||||
minDate: DateTime.now().subtract(const Duration(days: 14)).nextWeekday(DateTime.sunday),
|
||||
controller: _controller,
|
||||
onViewChanged: (details) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted) return;
|
||||
bloc.changeWeek(details.visibleDates.first, details.visibleDates.last);
|
||||
});
|
||||
},
|
||||
onTap: (tap) {
|
||||
if (tap.appointments == null || tap.appointments!.isEmpty) return;
|
||||
AppointmentDetailsDispatcher.show(context, bloc, tap.appointments!.first);
|
||||
},
|
||||
firstDayOfWeek: DateTime.monday,
|
||||
specialRegions: SpecialRegionsBuilder(
|
||||
holidays: state.schoolHolidays!,
|
||||
colorScheme: Theme.of(context).colorScheme,
|
||||
disabledColor: Theme.of(context).disabledColor,
|
||||
).build(),
|
||||
timeSlotViewSettings: const TimeSlotViewSettings(
|
||||
startHour: 7.5,
|
||||
endHour: 16.5,
|
||||
timeInterval: Duration(minutes: 30),
|
||||
timeFormat: 'HH:mm',
|
||||
dayFormat: 'EE',
|
||||
timeIntervalHeight: 40,
|
||||
),
|
||||
timeRegionBuilder: (_, details) => TimeRegionTile(details: details),
|
||||
appointmentBuilder: (_, details) => AppointmentTile(
|
||||
details: details,
|
||||
crossedOut: _isCrossedOut(details),
|
||||
),
|
||||
headerHeight: 0,
|
||||
selectionDecoration: const BoxDecoration(),
|
||||
allowAppointmentResize: false,
|
||||
allowDragAndDrop: false,
|
||||
allowViewNavigation: false,
|
||||
);
|
||||
}
|
||||
|
||||
bool _isCrossedOut(CalendarAppointmentDetails details) {
|
||||
final appointment = details.appointments.first;
|
||||
final id = appointment.id;
|
||||
if (id is WebuntisAppointment) return id.lesson.code == 'cancelled';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user