Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor lecture card and show all upcoming classes from today #979

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 49 additions & 43 deletions uni/lib/view/home/widgets/schedule_card.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:collection';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/generated/l10n.dart';
Expand Down Expand Up @@ -37,7 +38,10 @@ class ScheduleCard extends GenericCard {
return LazyConsumer<LectureProvider>(
builder: (context, lectureProvider) => RequestDependentWidgetBuilder(
status: lectureProvider.status,
builder: () => generateSchedule(lectureProvider.lectures, context),
builder: () => Column(
mainAxisSize: MainAxisSize.min,
children: getScheduleRows(context, lectureProvider.lectures),
),
hasContentPredicate: lectureProvider.lectures.isNotEmpty,
onNullContent: Center(
child: Text(
Expand All @@ -51,53 +55,55 @@ class ScheduleCard extends GenericCard {
);
}

Widget generateSchedule(
UnmodifiableListView<Lecture> lectures,
BuildContext context,
) {
final lectureList = List<Lecture>.of(lectures);
return Column(
mainAxisSize: MainAxisSize.min,
children: getScheduleRows(context, lectureList),
);
}

List<Widget> getScheduleRows(BuildContext context, List<Lecture> lectures) {
final rows = <Widget>[];

final now = DateTime.now();
var added = 0; // Lectures added to widget
var lastAddedLectureDate = DateTime.now(); // Day of last added lecture

for (var i = 0; added < 2 && i < lectures.length; i++) {
if (now.compareTo(lectures[i].endTime) < 0) {
if (lastAddedLectureDate.weekday != lectures[i].startTime.weekday &&
lastAddedLectureDate.compareTo(lectures[i].startTime) <= 0) {
rows.add(
DateRectangle(
date:
Provider.of<LocaleNotifier>(context).getWeekdaysWithLocale()[
(lectures[i].startTime.weekday - 1) % 7],
),
);
}

rows.add(createRowFromLecture(context, lectures[i]));
lastAddedLectureDate = lectures[i].startTime;
added++;
final lecturesByDay = lectures
.groupListsBy(
(lecture) => lecture.startTime.weekday,
)
.entries
.toList()
.sortedBy<num>((element) {
// Sort by day of the week, but next days come first
final dayDiff = element.key - DateTime.now().weekday;
return dayDiff >= 0 ? dayDiff - 7 : dayDiff;
}).toList();

for (final dayLectures
in lecturesByDay.sublist(0, min(2, lecturesByDay.length))) {
final day = dayLectures.key;
final lectures = dayLectures.value
.where(
(element) =>
// Hide finished lectures from today
element.startTime.weekday != DateTime.now().weekday ||
LuisDuarte1 marked this conversation as resolved.
Show resolved Hide resolved
element.endTime.isAfter(DateTime.now()),
)
.toList();

if (lectures.isEmpty) {
continue;
}
}

if (rows.isEmpty) {
rows
..add(
DateRectangle(
date: Provider.of<LocaleNotifier>(context)
.getWeekdaysWithLocale()[lectures[0].startTime.weekday % 7],
),
)
..add(createRowFromLecture(context, lectures[0]));
rows.add(
DateRectangle(
date: Provider.of<LocaleNotifier>(context)
.getWeekdaysWithLocale()[(day - 1) % 7],
),
);

for (final lecture in lectures) {
rows.add(createRowFromLecture(context, lecture));
}

rows.add(const Padding(padding: EdgeInsets.only(bottom: 10)));

if (lectures.length >= 2) {
break;
}
}

return rows;
}

Expand Down