From 80599c13d85de3739b2a84cb7542ffbe3d421706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horv=C3=A1th=20Gergely?= Date: Mon, 15 Dec 2025 23:06:28 +0100 Subject: [PATCH] Refactor lesson filtering and add logic to handle break events in live activity manager --- firka/lib/helpers/live_activity_manager.dart | 19 ++++-- firka/lib/helpers/live_activity_service.dart | 68 ++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/firka/lib/helpers/live_activity_manager.dart b/firka/lib/helpers/live_activity_manager.dart index 7d385d1f..8f93eede 100644 --- a/firka/lib/helpers/live_activity_manager.dart +++ b/firka/lib/helpers/live_activity_manager.dart @@ -370,18 +370,25 @@ class LiveActivityManager { _logger.info("Checking for activity update at ${now.toIso8601String()}"); final lessons = todayLessons.where((lesson) { - final type = lesson.type.name?.toLowerCase() ?? ''; - return !(lesson.state.name?.toLowerCase().contains('törölt') ?? false) && - !type.contains('tanevrendje'); + return !(lesson.state.name?.toLowerCase().contains('törölt') ?? false); }).toList(); if (lessons.isEmpty) { - _logger.info("No relevant lessons today. Ending activity if running."); - await endAllActivities(); + _logger.info("No lessons today. Backend will control activity state."); return; } - final state = _findCurrentActivityState(lessons, now); + final schoolLessons = lessons.where((lesson) { + final type = lesson.type.name?.toLowerCase() ?? ''; + return !type.contains('tanevrendje'); + }).toList(); + + if (schoolLessons.isEmpty) { + _logger.info("No school lessons today (only break events). Backend will control activity state."); + return; + } + + final state = _findCurrentActivityState(schoolLessons, now); _logger.info("Current state: lesson=${state.currentLesson?.name}, break=${state.isBreak}, mode=${state.mode}"); diff --git a/firka/lib/helpers/live_activity_service.dart b/firka/lib/helpers/live_activity_service.dart index 3ca0e880..b8fe4ccc 100644 --- a/firka/lib/helpers/live_activity_service.dart +++ b/firka/lib/helpers/live_activity_service.dart @@ -1055,6 +1055,74 @@ class LiveActivityService { if (addedCount > 0) { _logger.info('[Firka] [INFO] Added $addedCount $breakType break day(s) to timetable for backend processing'); } + + if (allBreakEventsOfType.isNotEmpty) { + allBreakEventsOfType.sort((a, b) => a.start.compareTo(b.start)); + final lastBreakDay = allBreakEventsOfType.last.start; + + _logger.info('Searching for first school day after $breakType break (after ${lastBreakDay.toIso8601String().split('T')[0]})...'); + + bool foundFirstSchoolDayAfterBreak = false; + for (int dayOffset = 1; dayOffset <= 14; dayOffset++) { + final candidateDay = lastBreakDay.add(Duration(days: dayOffset)); + + if (candidateDay.weekday == DateTime.saturday || candidateDay.weekday == DateTime.sunday) { + continue; + } + + try { + final candidateDayEnd = candidateDay.add(const Duration(days: 1)); + final response = await client.getTimeTable(candidateDay, candidateDayEnd, forceCache: false); + + if (response.response != null && response.response!.isNotEmpty) { + final schoolLessons = response.response!.where((lesson) { + final uid = lesson.uid.toLowerCase(); + return uid.contains('orarendiora') || uid.contains('tanitasiora') || uid.contains('uresora'); + }).toList(); + + if (schoolLessons.isNotEmpty) { + schoolLessons.sort((a, b) => a.start.compareTo(b.start)); + final firstLesson = schoolLessons.first; + + final notificationLesson = Lesson( + uid: '${firstLesson.uid}__FOR_NOTIFICATION_ONLY', + date: firstLesson.date, + start: firstLesson.start, + end: firstLesson.end, + name: firstLesson.name, + lessonNumber: firstLesson.lessonNumber, + teacher: firstLesson.teacher, + theme: firstLesson.theme, + roomName: firstLesson.roomName, + substituteTeacher: firstLesson.substituteTeacher, + type: firstLesson.type, + state: firstLesson.state, + canStudentEditHomework: firstLesson.canStudentEditHomework, + isHomeworkComplete: firstLesson.isHomeworkComplete, + attachments: firstLesson.attachments, + isDigitalLesson: firstLesson.isDigitalLesson, + digitalSupportDeviceTypeList: firstLesson.digitalSupportDeviceTypeList, + createdAt: firstLesson.createdAt ?? firstLesson.lastModifiedAt ?? DateTime.now(), + lastModifiedAt: firstLesson.lastModifiedAt, + ); + + allLessons.add(notificationLesson); + + _logger.info('Added first lesson after $breakType break: ${firstLesson.date.split('T')[0]} - ${firstLesson.name} (marked for notification scheduling only)'); + + foundFirstSchoolDayAfterBreak = true; + break; + } + } + } catch (e) { + _logger.warning('Could not fetch lessons after break for day offset $dayOffset: $e'); + } + } + + if (!foundFirstSchoolDayAfterBreak) { + _logger.warning('No school day found within 14 days after $breakType break'); + } + } } } catch (e) { _logger.warning('Could not fetch extended break events: $e');