diff --git a/firka/lib/helpers/live_activity_service.dart b/firka/lib/helpers/live_activity_service.dart index 783c109d..ba7501d8 100644 --- a/firka/lib/helpers/live_activity_service.dart +++ b/firka/lib/helpers/live_activity_service.dart @@ -30,7 +30,6 @@ class LiveActivityService { static Timer? _updateTimer; static String? _cachedDeviceToken; static bool _isInitialized = false; - static DateTime? _lastActivityRecreation; static Timer? _bellDelayDebounceTimer; static double? _pendingBellDelay; @@ -951,7 +950,6 @@ class LiveActivityService { if (liveActivityEnabled) { await _startPlaceholderActivity(allLessons, studentName); - _lastActivityRecreation = DateTime.now(); } await _startTimetableMonitoring( @@ -971,67 +969,6 @@ class LiveActivityService { } } - /// Called when app is opened - sends timetable to backend, backend handles updates - static Future onAppOpened({ - required KretaClient client, - required String studentName, - SettingsStore? settingsStore, - }) async { - if (!Platform.isIOS || !_isInitialized) return; - - try { - final enabled = await isEnabled(settingsStore, client); - if (!enabled) { - _logger.info('LiveActivity is disabled, ending any running activities'); - await LiveActivityManager.endAllActivities(); - return; - } - - final now = DateTime.now(); - if (_lastActivityRecreation != null) { - final timeSinceLastRecreation = now.difference(_lastActivityRecreation!); - if (timeSinceLastRecreation < const Duration(minutes: 5)) { - _logger.info('onAppOpened: Skipping activity recreation, last was ${timeSinceLastRecreation.inSeconds}s ago'); - await checkAndUpdateTimetable( - client: client, - studentName: studentName, - settingsStore: settingsStore - ); - return; - } - } - - final activeActivities = await LiveActivityManager.getActiveActivities(); - if (activeActivities.isNotEmpty) { - _logger.info('Ending existing activity to refresh push token (8-hour expiration)'); - await LiveActivityManager.endAllActivities(); - await Future.delayed(const Duration(milliseconds: 500)); - } - - final todayStart = DateTime(now.year, now.month, now.day); - final startOfWeek = todayStart.subtract(Duration(days: now.weekday - 1)); - final endOfWeek = startOfWeek.add(const Duration(days: 6)); - final timetableResponse = await client.getTimeTable(startOfWeek, endOfWeek); - final allLessons = timetableResponse.response ?? []; - - await _startPlaceholderActivity(allLessons, studentName); - _lastActivityRecreation = now; - - _logger.info('New activity created with fresh push token'); - - await checkAndUpdateTimetable( - client: client, - studentName: studentName, - settingsStore: settingsStore - ); - - await scheduleBackgroundFetch(); - - } catch (e) { - _logger.severe('Error handling onAppOpened for LiveActivity: $e'); - } - } - /// Called when user logs out static Future onUserLogout() async { _logger.info('onUserLogout: Function called'); diff --git a/firka/lib/ui/phone/screens/home/home_screen.dart b/firka/lib/ui/phone/screens/home/home_screen.dart index 6be0855f..27b3ddd8 100644 --- a/firka/lib/ui/phone/screens/home/home_screen.dart +++ b/firka/lib/ui/phone/screens/home/home_screen.dart @@ -897,7 +897,7 @@ class _HomeScreenState extends FirkaState void _refreshLiveActivityOnResume() async { if (!_hasCompletedFirstPrefetch) { - logger.info('[Home] Skipping onAppOpened: first prefetch not yet complete'); + logger.info('[Home] Skipping LiveActivity update: first prefetch not yet complete'); return; } try { @@ -906,13 +906,13 @@ class _HomeScreenState extends FirkaState settings: widget.data.settings, ); final studentName = token?.studentId ?? "Student"; - await LiveActivityService.onAppOpened( + await LiveActivityService.checkAndUpdateTimetable( client: widget.data.client, studentName: studentName, settingsStore: widget.data.settings, ); } catch (e) { - logger.warning('[Home] LiveActivity refresh on resume failed: $e'); + logger.warning('[Home] LiveActivity timetable update on resume failed: $e'); } }