diff --git a/app/lib/dashboard/bloc/current_lesson_index.dart b/app/lib/dashboard/bloc/current_lesson_index.dart index 12d10ca66..3bd86a2b2 100644 --- a/app/lib/dashboard/bloc/current_lesson_index.dart +++ b/app/lib/dashboard/bloc/current_lesson_index.dart @@ -8,39 +8,45 @@ part of 'dashboard_bloc.dart'; -/// Bestimmt, welcher Index in [views] jetzt oder als nächstes stattfindet. -/// Hat man jetzt z.B. Deutsch als zweites Fach am Tag, wird der Index 1 -/// zurückgegeben. Es wird also bei 0 angefangen zu zählen. -int getCurrentLessonIndex(List views) { - // Prüfun, ob bisher noch keine Schulstunde stattgefunden hat (also ob die - // Schule noch nicht angefangen hat) - if (views.where((view) => view.timeStatus == LessonTimeStatus.isYetToCome).length == - views.length) +/// Return the index of the lesson that is +/// a. currently taking place [LessonTimeStatus.isNow] +/// b. the next lesson if there is no lesson taking place right now +/// (no lesson has [LessonTimeStatus.isNow]) +/// +/// If no lesson has started yet it will return 0 (first lesson). +/// If every lesson has already passed it will throw [AllLessonsAreOverException]. +/// +/// Example: +/// We have the lessons [maths, english, german]. +/// If english is currently taking place ([LessonView.timeStatus] == +/// [LessonTimeStatus.isNow]) this function will return 1. +int getCurrentLessonIndex(List lessons) { + // School hasn't begun yet. + if (lessons + .every((lesson) => lesson.timeStatus == LessonTimeStatus.isYetToCome)) { return 0; - else { - // Prüfun, ob alle Schulstunden schon bisher stattgefunden haben (Schulschluss). - if (views - .where((view) => view.timeStatus == LessonTimeStatus.hasAlreadyTakenPlace) - .length == - views.length) throw AllLessonsAreOver(); + } - // Jede Stunde prüfen, ob diese gerade stattfindet. Falls ja, wird der - // Index der Schulstunde zurückgegeben, die als erstes die Bedingung erfüllt. - for (int i = 0; i < views.length; i++) { - if (views[i].timeStatus == LessonTimeStatus.isNow) return i; - } + // School is over. + if (lessons.every( + (lesson) => lesson.timeStatus == LessonTimeStatus.hasAlreadyTakenPlace)) + throw AllLessonsAreOverException(); - // Herausfinden, ob der Nutzer gerade in einer Freistunde / Pause ist. Falls ja, - // wird die nächste Stunde als aktueller Index zurückgegeben. - if (views.length >= 2) { - for (int i = 1; i < views.length; i++) { - if (views[i - 1].timeStatus == LessonTimeStatus.hasAlreadyTakenPlace && - views[i].timeStatus == LessonTimeStatus.isYetToCome) return i; - } - } + // If a lesson is currently taking place return its index. + for (int i = 0; i < lessons.length; i++) { + if (lessons[i].timeStatus == LessonTimeStatus.isNow) return i; + } - return 0; + // If no lesson is currently taking place (e.g. lunch break) we return the + // index of the next lesson to come. + if (lessons.length >= 2) { + for (int i = 1; i < lessons.length; i++) { + if (lessons[i - 1].timeStatus == LessonTimeStatus.hasAlreadyTakenPlace && + lessons[i].timeStatus == LessonTimeStatus.isYetToCome) return i; + } } + + return 0; } -class AllLessonsAreOver implements Exception {} +class AllLessonsAreOverException implements Exception {} diff --git a/app/lib/dashboard/timetable/lesson_row.dart b/app/lib/dashboard/timetable/lesson_row.dart index 9d297a752..a82d7bb8f 100644 --- a/app/lib/dashboard/timetable/lesson_row.dart +++ b/app/lib/dashboard/timetable/lesson_row.dart @@ -22,7 +22,7 @@ class _LessonRow extends StatelessWidget { int currentIndex; try { currentIndex = getCurrentLessonIndex(views); - } on AllLessonsAreOver { + } on AllLessonsAreOverException { return _SchoolIsOver(); } diff --git a/app/test/dashboard/current_lesson_index_test.dart b/app/test/dashboard/current_lesson_index_test.dart index a0b7a1947..c86a32f95 100644 --- a/app/test/dashboard/current_lesson_index_test.dart +++ b/app/test/dashboard/current_lesson_index_test.dart @@ -41,7 +41,7 @@ void main() { getLessonViewWithTimeStatus(LessonTimeStatus.hasAlreadyTakenPlace), ]; expect(() => getCurrentLessonIndex(schoolIsOver), - throwsA(predicate((e) => e is AllLessonsAreOver))); + throwsA(predicate((e) => e is AllLessonsAreOverException))); }); test('first lesson is now', () { @@ -75,5 +75,18 @@ void main() { ]; expect(getCurrentLessonIndex(firstLessonNow), 1); }); + + test('acceptance test', () { + final lessons = [ + getLessonViewWithTimeStatus(LessonTimeStatus.hasAlreadyTakenPlace), + getLessonViewWithTimeStatus(LessonTimeStatus.hasAlreadyTakenPlace), + getLessonViewWithTimeStatus(LessonTimeStatus.hasAlreadyTakenPlace), + getLessonViewWithTimeStatus(LessonTimeStatus.isNow), + getLessonViewWithTimeStatus(LessonTimeStatus.isYetToCome), + getLessonViewWithTimeStatus(LessonTimeStatus.isYetToCome), + getLessonViewWithTimeStatus(LessonTimeStatus.isYetToCome), + ]; + expect(getCurrentLessonIndex(lessons), 3); + }); }); }