Skip to content

Commit

Permalink
Refactor getCurrentLessonIndex. (#123)
Browse files Browse the repository at this point in the history
* Change comments to english.
* Make code more readable.
* Add a test case.
* Rename `AllLessonsOver` to `AllLessonsOverException`.
  • Loading branch information
Jonas-Sander authored Feb 7, 2022
1 parent 4740e48 commit 1ce1e50
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
64 changes: 35 additions & 29 deletions app/lib/dashboard/bloc/current_lesson_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<LessonView> 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<LessonView> 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 {}
2 changes: 1 addition & 1 deletion app/lib/dashboard/timetable/lesson_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class _LessonRow extends StatelessWidget {
int currentIndex;
try {
currentIndex = getCurrentLessonIndex(views);
} on AllLessonsAreOver {
} on AllLessonsAreOverException {
return _SchoolIsOver();
}

Expand Down
15 changes: 14 additions & 1 deletion app/test/dashboard/current_lesson_index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down Expand Up @@ -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);
});
});
}

0 comments on commit 1ce1e50

Please sign in to comment.