refactor: Optimistic state update when forgetting a snapshot

pull/440/head
Inex Code 2024-01-31 18:06:22 +04:00
parent 1ba8f324fe
commit e330f71b63
1 changed files with 8 additions and 8 deletions

View File

@ -366,6 +366,14 @@ class BackupsBloc extends Bloc<BackupsEvent, BackupsState> {
) async { ) async {
final currentState = state; final currentState = state;
if (currentState is BackupsInitialized) { if (currentState is BackupsInitialized) {
// Optimistically remove the snapshot from the list
getIt<ApiConnectionRepository>().apiData.backups.data =
getIt<ApiConnectionRepository>()
.apiData
.backups
.data
?.where((final Backup backup) => backup.id != event.backupId)
.toList();
emit(BackupsBusy.fromState(currentState)); emit(BackupsBusy.fromState(currentState));
final GenericResult result = final GenericResult result =
await getIt<ApiConnectionRepository>().api.forgetSnapshot( await getIt<ApiConnectionRepository>().api.forgetSnapshot(
@ -377,16 +385,8 @@ class BackupsBloc extends Bloc<BackupsEvent, BackupsState> {
} else if (result.data == false) { } else if (result.data == false) {
getIt<NavigationService>() getIt<NavigationService>()
.showSnackBar('backup.forget_snapshot_error'.tr()); .showSnackBar('backup.forget_snapshot_error'.tr());
} else {
final backups = getIt<ApiConnectionRepository>().apiData.backups.data;
if (backups != null) {
getIt<ApiConnectionRepository>().apiData.backups.data = backups
.where((final Backup backup) => backup.id != event.backupId)
.toList();
}
} }
emit(currentState); emit(currentState);
getIt<ApiConnectionRepository>().emitData();
} }
} }