Client/lib/screen/pages/timetable/weekView.dart

192 lines
6.7 KiB
Dart

import 'dart:developer' as dev;
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:jiffy/jiffy.dart';
import 'package:marianum_mobile/api/webuntis/queries/getHolidays/getHolidaysResponse.dart';
import 'package:timetable_view/timetable_view.dart';
import '../../../api/webuntis/queries/getHolidays/getHolidays.dart';
import '../../../api/webuntis/queries/getRooms/getRoomsResponse.dart';
import '../../../api/webuntis/queries/getSubjects/getSubjectsResponse.dart';
import '../../../api/webuntis/queries/getTimetable/getTimetableResponse.dart';
import '../../../data/timetable/timetableProps.dart';
extension DateHelpers on DateTime {
bool isToday() {
final now = DateTime.now();
return now.day == this.day &&
now.month == this.month &&
now.year == this.year;
}
bool isYesterday() {
final yesterday = DateTime.now().subtract(Duration(days: 1));
return yesterday.day == this.day &&
yesterday.month == this.month &&
yesterday.year == this.year;
}
}
class WeekView extends StatefulWidget {
final TimetableProps value;
const WeekView(this.value, {Key? key}) : super(key: key);
@override
State<WeekView> createState() => _WeekViewState();
}
class _WeekViewState extends State<WeekView> {
@override
Widget build(BuildContext context) {
return TimetableView(
laneEventsList: _buildLaneEvents(widget.value),
onEventTap: (TableEvent event) {},
timetableStyle: CustomTableStyle(context),
onEmptySlotTap: (int laneIndex, TableEventTime start, TableEventTime end) => {},
);
}
List<LaneEvents> _buildLaneEvents(TimetableProps data) {
List<LaneEvents> laneEvents = List<LaneEvents>.empty(growable: true);
Jiffy.locale("de"); // todo move outwards
GetTimetableResponse timetable = data.getTimetableResponse;
GetRoomsResponse rooms = data.getRoomsResponse;
GetSubjectsResponse subjects = data.getSubjectsResponse;
GetHolidaysResponse holidays = data.getHolidaysResponse;
List<int> dayList = timetable.result.map((e) => e.date).toSet().toList();
dayList.sort((a, b) => a-b);
for(int i = 0; i <= data.endDate.difference(data.startDate).inDays; i++) {
DateTime currentDay = data.startDate.copyWith().add(Duration(days: i));
// Check Holiday Information
GetHolidaysResponseObject? holidayInfo = GetHolidays.find(holidays, time: currentDay);
if(holidayInfo != null) {
laneEvents.add(
LaneEvents(
lane: getLane(currentDay),
events: List<TableEvent>.of([
TableEvent(
title: holidayInfo.name,
eventId: holidayInfo.id,
laneIndex: data.startDate.millisecondsSinceEpoch,
startTime: parseTime(0800),
endTime: parseTime(1500),
padding: const EdgeInsets.all(5),
backgroundColor: Theme.of(context).disabledColor,
location: "\n${holidayInfo.longName}",
)
]),
)
);
}
}
for (var day in dayList) {
DateTime currentDay = DateTime.parse("$day");
Lane currentLane = getLane(currentDay);
//Every Day
List<TableEvent> events = List<TableEvent>.generate(
timetable.result.where((element) => element.date == day).length,
(index) {
GetTimetableResponseObject tableEvent = timetable.result.where((element) => element.date == day).elementAt(index);
GetSubjectsResponseObject subject = subjects.result.firstWhere((subject) => subject.id == tableEvent.su[0]['id']);
return TableEvent(
title: "${subject.alternateName} (${subject.longName})",
eventId: tableEvent.id,
laneIndex: day,
startTime: parseTime(tableEvent.startTime),
endTime: parseTime(tableEvent.endTime),
padding: const EdgeInsets.all(5),
backgroundColor: getEventColor(
currentDay.add(Duration(hours: parseTime(tableEvent.startTime).hour, minutes: parseTime(tableEvent.startTime).minute)),
currentDay.add(Duration(hours: parseTime(tableEvent.endTime).hour, minutes: parseTime(tableEvent.endTime).minute)),
),
location: "\n${rooms.result.firstWhere((room) => room.id == tableEvent.ro[0]['id']).name} - ${tableEvent.te[0]['longname']} (${tableEvent.te[0]['name']})",
);
}
);
//Timepointer
if(currentDay.isToday()) {
events.add(TableEvent(
title: "",
eventId: 0,
laneIndex: day,
startTime: formatTime(DateTime.now()),
endTime: formatTime(DateTime.now().add(const Duration(minutes: 3))),
backgroundColor: Theme.of(context).disabledColor,
));
}
laneEvents.add(
LaneEvents(
lane: currentLane,
events: events
)
);
}
return laneEvents;
}
Lane getLane(DateTime currentDay) {
return Lane(
backgroundColor: currentDay.isToday() ? Theme.of(context).dividerColor : Colors.white,
laneIndex: currentDay.millisecondsSinceEpoch,
name: "${Jiffy(currentDay.toString()).format("dd MMM")}\n${Jiffy(currentDay.toString()).format("EE")}",
textStyle: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
fontSize: 14
)
);
}
TableEventTime parseTime(int input) {
String time = input.toString().length < 4 ? "0$input" : input.toString();
return TableEventTime(hour: int.parse(time.substring(0, 2)), minute: int.parse(time.substring(2, 4)));
}
TableEventTime formatTime(DateTime input) {
return TableEventTime(hour: input.hour, minute: input.minute);
}
Color getEventColor(DateTime startTime, DateTime endTime) {
if(endTime.isBefore(DateTime.now())) return Colors.grey;
if(startTime.isAfter(DateTime.now())) return Theme.of(context).primaryColor;
return Colors.redAccent;
}
}
class CustomTableStyle extends TimetableStyle {
dynamic context;
CustomTableStyle(this.context);
@override
int get startHour => 07;
@override
int get endHour => 17;
@override
Color get cornerColor => Theme.of(context).primaryColor;
@override
Color get timeItemTextColor => Theme.of(context).primaryColor;
@override
double get timeItemHeight => MediaQuery.of(context).size.width > 1000 ? 60 : 100 + (-MediaQuery.of(context).size.width + 1000) * 0.01;
@override
double get timeItemWidth => 50;
@override
double get laneHeight => 40;
@override
double get laneWidth => (MediaQuery.of(context).size.width - timeItemWidth) / 5;
}