fix(mobile): keep backup remainder from going negative (#29011)

* recount the backup counters when a run starts

* clear the error before the recount

* create the cancel token before the recount
This commit is contained in:
Santo Shakil 2026-08-05 20:47:05 +06:00 committed by GitHub
parent 1c7c28bb0d
commit b2eb62dfa5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 122 additions and 3 deletions

View file

@ -255,7 +255,7 @@ class DriftBackupNotifier extends StateNotifier<DriftBackupState> {
state = state.copyWith(isSyncing: isSyncing);
}
Future<void> startForegroundBackup(String userId) {
Future<void> startForegroundBackup(String userId) async {
// Cancel any existing backup before starting a new one
if (_cancelToken != null) {
stopForegroundBackup();
@ -263,11 +263,17 @@ class DriftBackupNotifier extends StateNotifier<DriftBackupState> {
state = state.copyWith(error: BackupError.none);
_cancelToken = Completer<void>();
// A pause during the recount below nulls _cancelToken, so the run keeps its own reference.
final cancelToken = Completer<void>();
_cancelToken = cancelToken;
// Re-baseline the counters against the same DB read that feeds this run's candidate list,
// otherwise a resume counts duplicate successes against the old baseline (#26215).
await getBackupStatus(userId);
return _foregroundUploadService.uploadCandidates(
userId,
_cancelToken!,
cancelToken,
callbacks: UploadCallbacks(
onProgress: _handleForegroundBackupProgress,
onSuccess: _handleForegroundBackupSuccess,
@ -333,6 +339,10 @@ class DriftBackupNotifier extends StateNotifier<DriftBackupState> {
}
void _handleForegroundBackupSuccess(String localAssetId, String remoteAssetId) {
if (!mounted) {
_logger.warning("Skip _handleForegroundBackupSuccess: notifier disposed");
return;
}
state = state.copyWith(backupCount: state.backupCount + 1, remainderCount: state.remainderCount - 1);
_uploadSpeedManager.removeTask(localAssetId);