190 lines
5.4 KiB
Dart
190 lines
5.4 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'group_event.dart';
|
|
import 'group_state.dart';
|
|
import '../../repositories/group_repository.dart';
|
|
import '../../data/models/group.dart';
|
|
|
|
class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
|
final GroupRepository _repository;
|
|
StreamSubscription? _groupsSubscription;
|
|
|
|
GroupBloc(this._repository) : super(GroupInitial()) {
|
|
on<LoadGroupsByUserId>(_onLoadGroupsByUserId);
|
|
on<_GroupsUpdated>(_onGroupsUpdated); // NOUVEAU événement interne
|
|
on<LoadGroupsByTrip>(_onLoadGroupsByTrip);
|
|
on<CreateGroup>(_onCreateGroup);
|
|
on<CreateGroupWithMembers>(_onCreateGroupWithMembers);
|
|
on<AddMemberToGroup>(_onAddMemberToGroup);
|
|
on<RemoveMemberFromGroup>(_onRemoveMemberFromGroup);
|
|
on<UpdateGroup>(_onUpdateGroup);
|
|
on<DeleteGroup>(_onDeleteGroup);
|
|
}
|
|
|
|
Future<void> _onLoadGroupsByUserId(
|
|
LoadGroupsByUserId event,
|
|
Emitter<GroupState> emit,
|
|
) async {
|
|
print('===== GroupBloc: _onLoadGroupsByUserId START =====');
|
|
|
|
try {
|
|
emit(GroupLoading());
|
|
print('>>> GroupBloc: État GroupLoading émis');
|
|
|
|
await _groupsSubscription?.cancel();
|
|
print('>>> GroupBloc: Ancien subscription annulé');
|
|
|
|
_groupsSubscription = _repository.getGroupsByUserId(event.userId).listen(
|
|
(groups) {
|
|
print('===== GroupBloc: Stream reçu ${groups.length} groupes =====');
|
|
// Utiliser un événement interne au lieu d'émettre directement
|
|
add(_GroupsUpdated(groups));
|
|
},
|
|
onError: (error) {
|
|
print('===== GroupBloc: Erreur stream: $error =====');
|
|
add(_GroupsUpdated([], error: error.toString()));
|
|
},
|
|
);
|
|
|
|
print('>>> GroupBloc: Subscription créé avec succès');
|
|
} catch (e, stackTrace) {
|
|
print('===== GroupBloc: Exception _onLoadGroupsByUserId =====');
|
|
print('Exception: $e');
|
|
print('StackTrace: $stackTrace');
|
|
emit(GroupError(e.toString()));
|
|
}
|
|
}
|
|
|
|
// NOUVEAU: Handler pour les mises à jour du stream
|
|
Future<void> _onGroupsUpdated(
|
|
_GroupsUpdated event,
|
|
Emitter<GroupState> emit,
|
|
) async {
|
|
print('===== GroupBloc: _onGroupsUpdated =====');
|
|
print('Groupes reçus: ${event.groups.length}');
|
|
|
|
if (event.error != null) {
|
|
print('>>> Émission GroupError: ${event.error}');
|
|
emit(GroupError(event.error!));
|
|
} else {
|
|
print('>>> Émission GroupsLoaded avec ${event.groups.length} groupes');
|
|
emit(GroupsLoaded(event.groups));
|
|
print('>>> GroupsLoaded émis avec succès !');
|
|
}
|
|
}
|
|
|
|
Future<void> _onLoadGroupsByTrip(
|
|
LoadGroupsByTrip event,
|
|
Emitter<GroupState> emit,
|
|
) async {
|
|
try {
|
|
emit(GroupLoading());
|
|
final group = await _repository.getGroupByTripId(event.tripId);
|
|
if (group != null) {
|
|
emit(GroupsLoaded([group]));
|
|
} else {
|
|
emit(const GroupsLoaded([]));
|
|
}
|
|
} catch (e) {
|
|
emit(GroupError(e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> _onCreateGroup(
|
|
CreateGroup event,
|
|
Emitter<GroupState> emit,
|
|
) async {
|
|
try {
|
|
emit(GroupLoading());
|
|
await _repository.createGroupWithMembers(
|
|
group: event.group,
|
|
members: [],
|
|
);
|
|
emit(const GroupOperationSuccess('Groupe créé avec succès'));
|
|
} catch (e) {
|
|
emit(GroupError('Erreur lors de la création: $e'));
|
|
}
|
|
}
|
|
|
|
Future<void> _onCreateGroupWithMembers(
|
|
CreateGroupWithMembers event,
|
|
Emitter<GroupState> emit,
|
|
) async {
|
|
try {
|
|
emit(GroupLoading());
|
|
await _repository.createGroupWithMembers(
|
|
group: event.group,
|
|
members: event.members,
|
|
);
|
|
emit(const GroupOperationSuccess('Groupe créé avec succès'));
|
|
} catch (e) {
|
|
emit(GroupError('Erreur lors de la création: $e'));
|
|
}
|
|
}
|
|
|
|
Future<void> _onAddMemberToGroup(
|
|
AddMemberToGroup event,
|
|
Emitter<GroupState> emit,
|
|
) async {
|
|
try {
|
|
await _repository.addMember(event.groupId, event.member);
|
|
emit(const GroupOperationSuccess('Membre ajouté'));
|
|
} catch (e) {
|
|
emit(GroupError('Erreur lors de l\'ajout: $e'));
|
|
}
|
|
}
|
|
|
|
Future<void> _onRemoveMemberFromGroup(
|
|
RemoveMemberFromGroup event,
|
|
Emitter<GroupState> emit,
|
|
) async {
|
|
try {
|
|
await _repository.removeMember(event.groupId, event.userId);
|
|
emit(const GroupOperationSuccess('Membre supprimé'));
|
|
} catch (e) {
|
|
emit(GroupError('Erreur lors de la suppression: $e'));
|
|
}
|
|
}
|
|
|
|
Future<void> _onUpdateGroup(
|
|
UpdateGroup event,
|
|
Emitter<GroupState> emit,
|
|
) async {
|
|
try {
|
|
await _repository.updateGroup(event.groupId, event.group);
|
|
emit(const GroupOperationSuccess('Groupe mis à jour'));
|
|
} catch (e) {
|
|
emit(GroupError('Erreur lors de la mise à jour: $e'));
|
|
}
|
|
}
|
|
|
|
Future<void> _onDeleteGroup(
|
|
DeleteGroup event,
|
|
Emitter<GroupState> emit,
|
|
) async {
|
|
try {
|
|
await _repository.deleteGroup(event.groupId);
|
|
emit(const GroupOperationSuccess('Groupe supprimé'));
|
|
} catch (e) {
|
|
emit(GroupError('Erreur lors de la suppression: $e'));
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<void> close() {
|
|
print('===== GroupBloc: close() =====');
|
|
_groupsSubscription?.cancel();
|
|
return super.close();
|
|
}
|
|
}
|
|
|
|
// NOUVEAU: Événement interne pour les mises à jour du stream
|
|
class _GroupsUpdated extends GroupEvent {
|
|
final List<Group> groups;
|
|
final String? error;
|
|
|
|
const _GroupsUpdated(this.groups, {this.error});
|
|
|
|
@override
|
|
List<Object?> get props => [groups, error];
|
|
} |