fix(sync): run as foreground service with persistent notification

Call setForeground() at start of doWork() to:
- Keep sync running when screen is off
- Show persistent notification during sync
- Update notification with sync progress

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Davide Ferrari
2026-01-05 14:15:26 +01:00
co-authored by Claude Opus 4.5
parent 63090f833e
commit 1a0838dea3
@@ -60,6 +60,15 @@ class DataSyncWorker @AssistedInject constructor(
override suspend fun doWork(): Result {
log("Starting data sync worker (attempt ${runAttemptCount})")
// Run as foreground service to prevent being killed when screen is off
// This shows a persistent notification during sync
try {
setForeground(createForegroundInfo("Starting sync..."))
} catch (e: Exception) {
log("Could not start foreground service: ${e.message}")
// Continue anyway - expedited work may still work
}
try {
// Get list of cars
val carsResult = teslamateRepository.getCars()
@@ -85,8 +94,11 @@ class DataSyncWorker @AssistedInject constructor(
// Sync cars sequentially to better handle network errors
var hasNetworkError = false
for (car in cars) {
for ((index, car) in cars.withIndex()) {
try {
// Update notification with current car
setForeground(createForegroundInfo("Syncing car ${index + 1}/${cars.size}..."))
val success = syncRepository.syncCar(car.carId)
if (!success) {
log("Sync incomplete for car ${car.carId}, will retry")