diff --git a/firka/lib/helpers/extensions.dart b/firka/lib/helpers/extensions.dart index 307132c3..656c225f 100644 --- a/firka/lib/helpers/extensions.dart +++ b/firka/lib/helpers/extensions.dart @@ -163,11 +163,10 @@ extension DateGrouper on Iterable { } } -extension LessonExtension on Lesson { - int getNo(List lessons) { - var first = lessons.first; +extension LessonExtension on List { + 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)))); + } } diff --git a/firka/lib/ui/phone/pages/home/home_main.dart b/firka/lib/ui/phone/pages/home/home_main.dart index dc109ae1..a6b7499d 100644 --- a/firka/lib/ui/phone/pages/home/home_main.dart +++ b/firka/lib/ui/phone/pages/home/home_main.dart @@ -82,21 +82,19 @@ class _HomeMainScreen extends State { // "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 { ), ), ); - /*return Column( - children: [HomeMainWelcome(now, student!, lessons!), SizedBox()], - );*/ } else { return DelayedSpinnerWidget(); } diff --git a/firka/lib/ui/wear/screens/home/home_screen.dart b/firka/lib/ui/wear/screens/home/home_screen.dart index a7d51b07..728a5a2c 100644 --- a/firka/lib/ui/wear/screens/home/home_screen.dart +++ b/firka/lib/ui/wear/screens/home/home_screen.dart @@ -129,37 +129,20 @@ class _WearHomeScreenState extends State { } 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) {