From 368ad86f716340a731ef68922c9646d8904f993b Mon Sep 17 00:00:00 2001 From: checkedear <271323618+checkedear@users.noreply.github.com> Date: Sun, 2 Aug 2026 21:06:52 +0200 Subject: [PATCH] fix: timetable cache optimalizations --- firka/lib/api/client/kreta_client.dart | 59 +++++++++++++++++++------- 1 file changed, 44 insertions(+), 15 deletions(-) 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;