/// BLoC pour la gestion des sauvegardes library backup_bloc; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:injectable/injectable.dart'; import 'package:equatable/equatable.dart'; import '../../data/repositories/backup_repository.dart'; import '../../data/models/backup_model.dart'; import '../../data/models/backup_config_model.dart'; // Events abstract class BackupEvent extends Equatable { @override List get props => []; } class LoadBackups extends BackupEvent {} class CreateBackup extends BackupEvent { final String name; final String? description; CreateBackup(this.name, {this.description}); @override List get props => [name, description]; } class RestoreBackup extends BackupEvent { final String backupId; RestoreBackup(this.backupId); @override List get props => [backupId]; } class DeleteBackup extends BackupEvent { final String backupId; DeleteBackup(this.backupId); @override List get props => [backupId]; } class LoadBackupConfig extends BackupEvent {} class UpdateBackupConfig extends BackupEvent { final Map config; UpdateBackupConfig(this.config); @override List get props => [config]; } // States abstract class BackupState extends Equatable { @override List get props => []; } class BackupInitial extends BackupState {} class BackupLoading extends BackupState {} class BackupsLoaded extends BackupState { final List backups; BackupsLoaded(this.backups); @override List get props => [backups]; } class BackupConfigLoaded extends BackupState { final BackupConfigModel config; BackupConfigLoaded(this.config); @override List get props => [config]; } class BackupSuccess extends BackupState { final String message; BackupSuccess(this.message); @override List get props => [message]; } class BackupError extends BackupState { final String error; BackupError(this.error); @override List get props => [error]; } // Bloc @injectable class BackupBloc extends Bloc { final BackupRepository _repository; BackupBloc(this._repository) : super(BackupInitial()) { on(_onLoadBackups); on(_onCreateBackup); on(_onRestoreBackup); on(_onDeleteBackup); on(_onLoadBackupConfig); on(_onUpdateBackupConfig); } Future _onLoadBackups(LoadBackups event, Emitter emit) async { emit(BackupLoading()); try { final backups = await _repository.getAll(); emit(BackupsLoaded(backups)); } catch (e) { emit(BackupError('Erreur: ${e.toString()}')); } } Future _onCreateBackup(CreateBackup event, Emitter emit) async { emit(BackupLoading()); try { await _repository.create(event.name, description: event.description); final backups = await _repository.getAll(); emit(BackupsLoaded(backups)); emit(BackupSuccess('Sauvegarde créée')); } catch (e) { emit(BackupError('Erreur: ${e.toString()}')); } } Future _onRestoreBackup(RestoreBackup event, Emitter emit) async { emit(BackupLoading()); try { await _repository.restore(event.backupId); emit(BackupSuccess('Restauration en cours')); } catch (e) { emit(BackupError('Erreur: ${e.toString()}')); } } Future _onDeleteBackup(DeleteBackup event, Emitter emit) async { emit(BackupLoading()); try { await _repository.delete(event.backupId); final backups = await _repository.getAll(); emit(BackupsLoaded(backups)); emit(BackupSuccess('Sauvegarde supprimée')); } catch (e) { emit(BackupError('Erreur: ${e.toString()}')); } } Future _onLoadBackupConfig(LoadBackupConfig event, Emitter emit) async { emit(BackupLoading()); try { final config = await _repository.getConfig(); emit(BackupConfigLoaded(config)); } catch (e) { emit(BackupError('Erreur: ${e.toString()}')); } } Future _onUpdateBackupConfig(UpdateBackupConfig event, Emitter emit) async { emit(BackupLoading()); try { final config = await _repository.updateConfig(event.config); emit(BackupConfigLoaded(config)); emit(BackupSuccess('Configuration mise à jour')); } catch (e) { emit(BackupError('Erreur: ${e.toString()}')); } } }