chore: do not remote sync twice in backup page (#22484)

do not remote sync twice in backup page

Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
This commit is contained in:
shenlong 2025-09-29 23:44:01 +05:30 committed by GitHub
parent 75b9bd163e
commit 44284b4351
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 42 additions and 10 deletions

View File

@ -599,7 +599,7 @@
"backup_controller_page_turn_on": "Turn on foreground backup",
"backup_controller_page_uploading_file_info": "Uploading file info",
"backup_err_only_album": "Cannot remove the only album",
"backup_error_sync_failed": "Sync failed. Cannot start backup.",
"backup_error_sync_failed": "Sync failed. Cannot process backup.",
"backup_info_card_assets": "assets",
"backup_manual_cancelled": "Cancelled",
"backup_manual_in_progress": "Upload already in progress. Try after sometime",

View File

@ -31,6 +31,8 @@ class DriftBackupPage extends ConsumerStatefulWidget {
}
class _DriftBackupPageState extends ConsumerState<DriftBackupPage> {
bool? syncSuccess;
@override
void initState() {
super.initState();
@ -44,7 +46,13 @@ class _DriftBackupPageState extends ConsumerState<DriftBackupPage> {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await ref.read(driftBackupProvider.notifier).getBackupStatus(currentUser.id);
await ref.read(backgroundSyncProvider).syncRemote();
ref.read(driftBackupProvider.notifier).updateSyncing(true);
syncSuccess = await ref.read(backgroundSyncProvider).syncRemote();
ref
.read(driftBackupProvider.notifier)
.updateError(syncSuccess == true ? BackupError.none : BackupError.syncFailed);
ref.read(driftBackupProvider.notifier).updateSyncing(false);
if (mounted) {
await ref.read(driftBackupProvider.notifier).getBackupStatus(currentUser.id);
@ -76,12 +84,17 @@ class _DriftBackupPageState extends ConsumerState<DriftBackupPage> {
return;
}
final syncSuccess = await backupSyncManager.syncRemote();
if (syncSuccess == null) {
ref.read(driftBackupProvider.notifier).updateSyncing(true);
syncSuccess = await backupSyncManager.syncRemote();
ref.read(driftBackupProvider.notifier).updateSyncing(false);
}
await backupNotifier.getBackupStatus(currentUser.id);
if (!syncSuccess) {
if (syncSuccess == false) {
Logger("DriftBackupPage").warning("Remote sync did not complete successfully, skipping backup");
await backupNotifier.updateError(BackupError.syncFailed);
backupNotifier.updateError(BackupError.syncFailed);
return;
}
await backupNotifier.startBackup(currentUser.id);
@ -125,7 +138,13 @@ class _DriftBackupPageState extends ConsumerState<DriftBackupPage> {
const _BackupCard(),
const _RemainderCard(),
const Divider(),
BackupToggleButton(onStart: () async => await startBackup(), onStop: () async => await stopBackup()),
BackupToggleButton(
onStart: () async => await startBackup(),
onStop: () async {
syncSuccess = null;
await stopBackup();
},
),
switch (error) {
BackupError.none => const SizedBox.shrink(),
BackupError.syncFailed => Padding(

View File

@ -65,7 +65,9 @@ class BackupToggleButtonState extends ConsumerState<BackupToggleButton> with Sin
final uploadTasks = ref.watch(driftBackupProvider.select((state) => state.uploadItems));
final isUploading = uploadTasks.isNotEmpty;
final isSyncing = ref.watch(driftBackupProvider.select((state) => state.isSyncing));
final isProcessing = uploadTasks.isNotEmpty || isSyncing;
return AnimatedBuilder(
animation: _animationController,
@ -129,7 +131,7 @@ class BackupToggleButtonState extends ConsumerState<BackupToggleButton> with Sin
],
),
),
child: isUploading
child: isProcessing
? const SizedBox(width: 24, height: 24, child: CircularProgressIndicator(strokeWidth: 2))
: Icon(Icons.cloud_upload_outlined, color: context.primaryColor, size: 24),
),

View File

@ -109,6 +109,7 @@ class DriftBackupState {
final int enqueueCount;
final int enqueueTotalCount;
final bool isSyncing;
final bool isCanceling;
final BackupError error;
@ -122,6 +123,7 @@ class DriftBackupState {
required this.enqueueCount,
required this.enqueueTotalCount,
required this.isCanceling,
required this.isSyncing,
required this.uploadItems,
this.error = BackupError.none,
});
@ -134,6 +136,7 @@ class DriftBackupState {
int? enqueueCount,
int? enqueueTotalCount,
bool? isCanceling,
bool? isSyncing,
Map<String, DriftUploadStatus>? uploadItems,
BackupError? error,
}) {
@ -145,6 +148,7 @@ class DriftBackupState {
enqueueCount: enqueueCount ?? this.enqueueCount,
enqueueTotalCount: enqueueTotalCount ?? this.enqueueTotalCount,
isCanceling: isCanceling ?? this.isCanceling,
isSyncing: isSyncing ?? this.isSyncing,
uploadItems: uploadItems ?? this.uploadItems,
error: error ?? this.error,
);
@ -152,7 +156,7 @@ class DriftBackupState {
@override
String toString() {
return 'DriftBackupState(totalCount: $totalCount, backupCount: $backupCount, remainderCount: $remainderCount, processingCount: $processingCount, enqueueCount: $enqueueCount, enqueueTotalCount: $enqueueTotalCount, isCanceling: $isCanceling, uploadItems: $uploadItems, error: $error)';
return 'DriftBackupState(totalCount: $totalCount, backupCount: $backupCount, remainderCount: $remainderCount, processingCount: $processingCount, enqueueCount: $enqueueCount, enqueueTotalCount: $enqueueTotalCount, isCanceling: $isCanceling, isSyncing: $isSyncing, uploadItems: $uploadItems, error: $error)';
}
@override
@ -167,6 +171,7 @@ class DriftBackupState {
other.enqueueCount == enqueueCount &&
other.enqueueTotalCount == enqueueTotalCount &&
other.isCanceling == isCanceling &&
other.isSyncing == isSyncing &&
mapEquals(other.uploadItems, uploadItems) &&
other.error == error;
}
@ -180,6 +185,7 @@ class DriftBackupState {
enqueueCount.hashCode ^
enqueueTotalCount.hashCode ^
isCanceling.hashCode ^
isSyncing.hashCode ^
uploadItems.hashCode ^
error.hashCode;
}
@ -200,6 +206,7 @@ class DriftBackupNotifier extends StateNotifier<DriftBackupState> {
enqueueCount: 0,
enqueueTotalCount: 0,
isCanceling: false,
isSyncing: false,
uploadItems: {},
error: BackupError.none,
),
@ -335,10 +342,14 @@ class DriftBackupNotifier extends StateNotifier<DriftBackupState> {
);
}
Future<void> updateError(BackupError error) async {
void updateError(BackupError error) async {
state = state.copyWith(error: error);
}
void updateSyncing(bool isSyncing) async {
state = state.copyWith(isSyncing: isSyncing);
}
Future<void> startBackup(String userId) {
state = state.copyWith(error: BackupError.none);
return _uploadService.startBackup(userId, _updateEnqueueCount);