1
0
forked from firka/firka

commonize getLessonNo and get{Current/Prev/Next}Lesson

This commit is contained in:
2025-07-15 10:18:33 +02:00
parent 49f9e1a80e
commit b5a59c510c
3 changed files with 34 additions and 42 deletions

View File

@@ -163,11 +163,10 @@ extension DateGrouper<T> on Iterable<T> {
}
}
extension LessonExtension on Lesson {
int getNo(List<Lesson> lessons) {
var first = lessons.first;
extension LessonExtension on List<Lesson> {
int getLessonNo(Lesson lesson) {
var midnight = first.start.getMidnight();
int i = lessons.indexOf(this);
int i = indexOf(lesson);
if (!first.start.isBefore(midnight.add(Duration(hours: 8)))) {
i++;
@@ -175,4 +174,19 @@ extension LessonExtension on Lesson {
return i;
}
Lesson? getCurrentLesson(DateTime now) {
return firstWhereOrNull(
(lesson) => now.isAfter(lesson.start) && now.isBefore(lesson.end));
}
Lesson? getPrevLesson(DateTime now) {
return firstWhereOrNull(
(lesson) => lesson.end.isBefore(now.add(Duration(milliseconds: 1))));
}
Lesson? getNextLesson(DateTime now) {
return firstWhereOrNull(
(lesson) => lesson.start.isAfter(now.add(Duration(milliseconds: 1))));
}
}

View File

@@ -82,21 +82,19 @@ class _HomeMainScreen extends State<HomeMainScreen> {
// "fun" fact if your clock was exactly when the class ends then isBefore
// and isAfter would fail, so to work around that we just add 1ms to the
// current time
var prevLesson = lessons!.firstWhereOrNull((lesson) =>
lesson.end.isBefore(now.add(Duration(milliseconds: 1))));
var nextLesson = lessons!.firstWhereOrNull((lesson) =>
lesson.start.isAfter(now.add(Duration(milliseconds: 1))));
var prevLesson = lessons!.getPrevLesson(now);
var nextLesson = lessons!.getNextLesson(now);
int? lessonIndex;
if (currentLesson != null) lessonIndex = currentLesson.getNo(lessons!);
if (currentLesson != null)
lessonIndex = lessons!.getLessonNo(currentLesson);
welcomeWidget = LessonBigWidget(
now, lessonIndex, currentLesson, prevLesson, nextLesson);
}
}
if (lessons != null && lessons!.isNotEmpty) {
var nextLesson =
lessons!.firstWhereOrNull((lesson) => lesson.start.isAfter(now));
var nextLesson = lessons!.getNextLesson(now);
if (nextLesson != null) nextClass = LessonSmallWidget(nextLesson);
}
@@ -119,9 +117,6 @@ class _HomeMainScreen extends State<HomeMainScreen> {
),
),
);
/*return Column(
children: [HomeMainWelcome(now, student!, lessons!), SizedBox()],
);*/
} else {
return DelayedSpinnerWidget();
}

View File

@@ -129,37 +129,20 @@ class _WearHomeScreenState extends State<WearHomeScreen> {
}
currentLessonNo = null;
if (now.isAfter(today.first.start) && now.isBefore(today.last.end)) {
Lesson? currentLesson;
Lesson? lastLesson; // last as in the last lesson that you've been to
Lesson? next;
Lesson? nextLesson;
Duration? currentBreak;
Duration? currentBreakProgress;
for (int i = 0; i < today.length; i++) {
var lesson = today[i];
if (now.isAfter(lesson.start) && now.isBefore(lesson.end)) {
currentLesson = lesson;
currentLessonNo = i + 1;
Lesson? currentLesson = today.getCurrentLesson(now);
Lesson? lastLesson = today.getPrevLesson(now);
Lesson? nextLesson = today.getNextLesson(now);
if (i + 2 < today.length) nextLesson = today[i + 1];
break;
}
if (now.isAfter(lesson.end)) {
if (lastLesson == null) {
lastLesson = lesson;
if (i < today.length) next = today[i + 1];
} else {
if (lesson.end.isAfter(lastLesson.end)) {
lastLesson = lesson;
if (i < today.length) next = today[i + 1];
}
}
}
if (currentLesson != null) {
currentLessonNo = today.getLessonNo(currentLesson);
}
if (lastLesson != null && next != null) {
currentBreak = next.start.difference(lastLesson.end);
currentBreakProgress = next.start.difference(now);
Duration? currentBreak;
Duration? currentBreakProgress;
if (lastLesson != null && nextLesson != null) {
currentBreak = nextLesson.start.difference(lastLesson.end);
currentBreakProgress = nextLesson.start.difference(now);
}
if (currentLesson == null) {