diff --git a/firka/lib/api/client/kreta_client.dart b/firka/lib/api/client/kreta_client.dart index 9f3ffc5f..deb4762d 100644 --- a/firka/lib/api/client/kreta_client.dart +++ b/firka/lib/api/client/kreta_client.dart @@ -83,21 +83,50 @@ class KretaClient { return lessons; } - Future renewTimetable() async { - DateTime from = - /*(await isarInit.lessonCacheModels - .filter() - .endLessThan( - timeNow().getMidnight().getMonday().subtract( - Duration(days: 7), - ), - include: true, - ) - .sortByEndDesc() - .findFirst()) - ?.end ??*/ - timeNow().getFirstSchoolDay(); - DateTime to = timeNow().getMonday().add(Duration(days: 14)); + Future renewTimetable({bool wholeYear = false}) async { + DateTime now = timeNow(); + DateTime firstDayOfSchool = now.getFirstSchoolDay(); + DateTime firstDayOfNextYear = firstDayOfSchool.copyWith( + year: firstDayOfSchool.year + 1, + ); + DateTime? lastDayOfSchool = await cache + .getEvents() + .between( + firstDayOfNextYear.copyWith(month: 6, day: 1), + firstDayOfNextYear.copyWith(month: 8, day: 1), + ) + .startProperty() + .max(); + + if (lastDayOfSchool == null || + lastDayOfSchool.isBefore(firstDayOfNextYear.copyWith(month: 6))) { + wholeYear = true; + lastDayOfSchool = firstDayOfNextYear; + } + + if (now.isAfter(lastDayOfSchool)) { + return; + } + + DateTime from = firstDayOfSchool; + if (!wholeYear) { + from = + (await cache + .getClassLessons() + .between( + firstDayOfSchool, + timeNow().getMidnight().getMonday().subtract(Duration(days: 7)), + ) + .startProperty() + .max()) ?? + firstDayOfSchool; + } + + DateTime to = lastDayOfSchool; + if (!wholeYear) { + to = to.min(timeNow().getMonday().add(Duration(days: 14))); + } + DateTime date = from; List requests = []; int i = 0;