v1.0.66: fix scheduled background sync never registering on pair creation
Build & Release APK / build (push) Successful in 12m49s

Creating an interval/daily/weekly sync pair saved it enabled but never enqueued
the periodic WorkManager job — it only scheduled on the enable-toggle or a
reboot, so a freshly-created scheduled backup silently never ran in the
background. AddPairViewModel.save now registers the work (periodic / watcher)
on save, mirroring toggleEnabled + BootReceiver. Verified on-device: the
JobScheduler periodic job appears on save and a forced run performs the sync.
This commit is contained in:
2026-06-05 21:08:42 +00:00
parent 15b94a0407
commit e1abf80f11
2 changed files with 35 additions and 5 deletions
@@ -10,7 +10,10 @@ import com.syncflow.data.db.SyncPairDao
import com.syncflow.data.db.entities.CloudAccountEntity
import com.syncflow.data.db.entities.SyncPairEntity
import com.syncflow.domain.model.*
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.WorkManager
import com.syncflow.worker.FileWatchService
import com.syncflow.worker.SyncWorker
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.*
@@ -176,7 +179,7 @@ class AddPairViewModel @Inject constructor(
notifyOnComplete = s.notifyOnComplete, notifyOnError = s.notifyOnError,
isEnabled = true, lastSyncAt = null, lastSyncResult = SyncStatus.IDLE, pendingConflicts = 0,
)
if (editPairId == null) {
val pairId = if (editPairId == null) {
syncPairDao.insert(entity)
} else {
val existing = syncPairDao.getById(editPairId)
@@ -189,13 +192,40 @@ class AddPairViewModel @Inject constructor(
) {
fileStateDao.deleteForPair(editPairId)
}
editPairId
}
entity.copy(id = pairId)
}
.onSuccess {
if (s.scheduleType == ScheduleType.ON_CHANGE) FileWatchService.start(context)
.onSuccess { saved ->
applySchedule(saved)
_state.update { it.copy(done = true) }
}
.onFailure { e -> _state.update { it.copy(isSaving = false, error = e.message) } }
}
}
/**
* Register the pair's background work the moment it's saved. Previously this only happened on
* the enable-toggle or a reboot, so a freshly-created scheduled pair never actually ran in the
* background. Mirrors HomeViewModel.toggleEnabled / BootReceiver.
*/
private fun applySchedule(pair: SyncPairEntity) {
val wm = WorkManager.getInstance(context)
when (pair.scheduleType) {
ScheduleType.ON_CHANGE -> {
wm.cancelUniqueWork("periodic_${pair.id}")
FileWatchService.start(context)
}
ScheduleType.MANUAL -> wm.cancelUniqueWork("periodic_${pair.id}")
else -> {
val req = SyncWorker.buildPeriodicRequest(
pair.id,
pair.scheduleIntervalMinutes.toLong().coerceAtLeast(15),
pair.wifiOnly,
pair.chargingOnly,
)
wm.enqueueUniquePeriodicWork("periodic_${pair.id}", ExistingPeriodicWorkPolicy.UPDATE, req)
}
}
}
}
+2 -2
View File
@@ -1,2 +1,2 @@
VERSION_NAME=1.0.65
VERSION_CODE=66
VERSION_NAME=1.0.66
VERSION_CODE=67