Remove onAppOpened and related LiveActivity state

Remove the _lastActivityRecreation field and the onAppOpened(...) method from LiveActivityService, which previously handled ending/creating activities, refreshing push tokens, fetching the week's timetable and scheduling background fetch. Update home_screen to call LiveActivityService.checkAndUpdateTimetable(...) instead of onAppOpened, and tweak related log messages to clarify the behavior. This simplifies LiveActivity lifecycle on app resume and stops forced activity recreation/token refresh performed by the removed method.
This commit is contained in:
Horváth Gergely
2026-02-27 22:50:32 +01:00
parent c646ea2d51
commit dd3884de16
2 changed files with 3 additions and 66 deletions

View File

@@ -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<void> 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<void> onUserLogout() async {
_logger.info('onUserLogout: Function called');

View File

@@ -897,7 +897,7 @@ class _HomeScreenState extends FirkaState<HomeScreen>
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<HomeScreen>
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');
}
}