feat: Refactor group deletion logic to use tripId and add reset trips functionality
This commit is contained in:
@@ -148,7 +148,7 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
|||||||
Emitter<GroupState> emit,
|
Emitter<GroupState> emit,
|
||||||
) async {
|
) async {
|
||||||
try {
|
try {
|
||||||
await _repository.deleteGroup(event.groupId);
|
await _repository.deleteGroup(event.tripId);
|
||||||
emit(const GroupOperationSuccess('Groupe supprimé'));
|
emit(const GroupOperationSuccess('Groupe supprimé'));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(GroupError('Erreur lors de la suppression: $e'));
|
emit(GroupError('Erreur lors de la suppression: $e'));
|
||||||
|
|||||||
@@ -88,10 +88,10 @@ class UpdateGroup extends GroupEvent {
|
|||||||
|
|
||||||
// Supprimer un groupe
|
// Supprimer un groupe
|
||||||
class DeleteGroup extends GroupEvent {
|
class DeleteGroup extends GroupEvent {
|
||||||
final String groupId;
|
final String tripId;
|
||||||
|
|
||||||
const DeleteGroup(this.groupId);
|
const DeleteGroup(this.tripId);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object?> get props => [groupId];
|
List<Object?> get props => [tripId];
|
||||||
}
|
}
|
||||||
@@ -17,15 +17,13 @@ class TripBloc extends Bloc<TripEvent, TripState> {
|
|||||||
on<TripUpdateRequested>(_onTripUpdateRequested);
|
on<TripUpdateRequested>(_onTripUpdateRequested);
|
||||||
on<TripDeleteRequested>(_onTripDeleteRequested);
|
on<TripDeleteRequested>(_onTripDeleteRequested);
|
||||||
on<_TripsUpdated>(_onTripsUpdated);
|
on<_TripsUpdated>(_onTripsUpdated);
|
||||||
|
on<ResetTrips>(_onResetTrips);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onLoadTripsByUserId(
|
Future<void> _onLoadTripsByUserId(
|
||||||
LoadTripsByUserId event,
|
LoadTripsByUserId event,
|
||||||
Emitter<TripState> emit,
|
Emitter<TripState> emit,
|
||||||
) async {
|
) async {
|
||||||
print('🔍 Chargement des trips pour userId: ${event.userId}');
|
|
||||||
|
|
||||||
// MODIFIÉ : Toujours émettre Loading pour forcer le rechargement
|
|
||||||
emit(TripLoading());
|
emit(TripLoading());
|
||||||
|
|
||||||
_currentUserId = event.userId;
|
_currentUserId = event.userId;
|
||||||
@@ -33,11 +31,9 @@ class TripBloc extends Bloc<TripEvent, TripState> {
|
|||||||
|
|
||||||
_tripsSubscription = _repository.getTripsByUserId(event.userId).listen(
|
_tripsSubscription = _repository.getTripsByUserId(event.userId).listen(
|
||||||
(trips) {
|
(trips) {
|
||||||
print('📦 Stream reçu: ${trips.length} trips');
|
|
||||||
add(_TripsUpdated(trips));
|
add(_TripsUpdated(trips));
|
||||||
},
|
},
|
||||||
onError: (error) {
|
onError: (error) {
|
||||||
print('❌ Erreur stream: $error');
|
|
||||||
emit(TripError(error.toString()));
|
emit(TripError(error.toString()));
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -47,7 +43,6 @@ class TripBloc extends Bloc<TripEvent, TripState> {
|
|||||||
_TripsUpdated event,
|
_TripsUpdated event,
|
||||||
Emitter<TripState> emit,
|
Emitter<TripState> emit,
|
||||||
) {
|
) {
|
||||||
print('✅ Émission de TripLoaded avec ${event.trips.length} trips');
|
|
||||||
emit(TripLoaded(event.trips));
|
emit(TripLoaded(event.trips));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,24 +51,18 @@ class TripBloc extends Bloc<TripEvent, TripState> {
|
|||||||
Emitter<TripState> emit,
|
Emitter<TripState> emit,
|
||||||
) async {
|
) async {
|
||||||
try {
|
try {
|
||||||
print('📝 Création du voyage: ${event.trip.title}');
|
|
||||||
emit(TripLoading());
|
emit(TripLoading());
|
||||||
|
|
||||||
final tripId = await _repository.createTrip(event.trip);
|
final tripId = await _repository.createTrip(event.trip);
|
||||||
print('✅ Voyage créé avec ID: $tripId');
|
|
||||||
|
|
||||||
// Émettre TripCreated pour que create_trip_content puisse créer le groupe
|
|
||||||
emit(TripCreated(tripId: tripId));
|
emit(TripCreated(tripId: tripId));
|
||||||
|
|
||||||
// AJOUTÉ : Attendre un peu puis recharger manuellement
|
|
||||||
await Future.delayed(const Duration(milliseconds: 800));
|
await Future.delayed(const Duration(milliseconds: 800));
|
||||||
if (_currentUserId != null) {
|
if (_currentUserId != null) {
|
||||||
print('🔄 Rechargement forcé après création');
|
|
||||||
add(LoadTripsByUserId(userId: _currentUserId!));
|
add(LoadTripsByUserId(userId: _currentUserId!));
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('❌ Erreur création: $e');
|
|
||||||
emit(TripError('Erreur lors de la création: $e'));
|
emit(TripError('Erreur lors de la création: $e'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,22 +72,14 @@ class TripBloc extends Bloc<TripEvent, TripState> {
|
|||||||
Emitter<TripState> emit,
|
Emitter<TripState> emit,
|
||||||
) async {
|
) async {
|
||||||
try {
|
try {
|
||||||
print('📝 Mise à jour du voyage: ${event.trip.title}');
|
|
||||||
|
|
||||||
await _repository.updateTrip(event.trip.id!, event.trip);
|
await _repository.updateTrip(event.trip.id!, event.trip);
|
||||||
print('✅ Voyage mis à jour');
|
|
||||||
|
|
||||||
emit(const TripOperationSuccess('Voyage mis à jour avec succès'));
|
emit(const TripOperationSuccess('Voyage mis à jour avec succès'));
|
||||||
|
|
||||||
// AJOUTÉ : Recharger après mise à jour
|
|
||||||
await Future.delayed(const Duration(milliseconds: 500));
|
await Future.delayed(const Duration(milliseconds: 500));
|
||||||
if (_currentUserId != null) {
|
if (_currentUserId != null) {
|
||||||
print('🔄 Rechargement forcé après mise à jour');
|
|
||||||
add(LoadTripsByUserId(userId: _currentUserId!));
|
add(LoadTripsByUserId(userId: _currentUserId!));
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('❌ Erreur mise à jour: $e');
|
|
||||||
emit(TripError('Erreur lors de la mise à jour: $e'));
|
emit(TripError('Erreur lors de la mise à jour: $e'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,26 +89,29 @@ class TripBloc extends Bloc<TripEvent, TripState> {
|
|||||||
Emitter<TripState> emit,
|
Emitter<TripState> emit,
|
||||||
) async {
|
) async {
|
||||||
try {
|
try {
|
||||||
print('🗑️ Suppression du voyage: ${event.tripId}');
|
|
||||||
|
|
||||||
await _repository.deleteTrip(event.tripId);
|
await _repository.deleteTrip(event.tripId);
|
||||||
print('✅ Voyage supprimé');
|
|
||||||
|
|
||||||
emit(const TripOperationSuccess('Voyage supprimé avec succès'));
|
emit(const TripOperationSuccess('Voyage supprimé avec succès'));
|
||||||
|
|
||||||
// AJOUTÉ : Recharger après suppression
|
|
||||||
await Future.delayed(const Duration(milliseconds: 500));
|
await Future.delayed(const Duration(milliseconds: 500));
|
||||||
if (_currentUserId != null) {
|
if (_currentUserId != null) {
|
||||||
print('🔄 Rechargement forcé après suppression');
|
|
||||||
add(LoadTripsByUserId(userId: _currentUserId!));
|
add(LoadTripsByUserId(userId: _currentUserId!));
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('❌ Erreur suppression: $e');
|
|
||||||
emit(TripError('Erreur lors de la suppression: $e'));
|
emit(TripError('Erreur lors de la suppression: $e'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _onResetTrips(
|
||||||
|
ResetTrips event,
|
||||||
|
Emitter<TripState> emit,
|
||||||
|
) async {
|
||||||
|
await _tripsSubscription?.cancel();
|
||||||
|
_currentUserId = null;
|
||||||
|
emit(TripInitial());
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> close() {
|
Future<void> close() {
|
||||||
_tripsSubscription?.cancel();
|
_tripsSubscription?.cancel();
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ class TripUpdateRequested extends TripEvent {
|
|||||||
List<Object?> get props => [trip];
|
List<Object?> get props => [trip];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ResetTrips extends TripEvent {
|
||||||
|
const ResetTrips();
|
||||||
|
}
|
||||||
|
|
||||||
class TripDeleteRequested extends TripEvent {
|
class TripDeleteRequested extends TripEvent {
|
||||||
final String tripId;
|
final String tripId;
|
||||||
|
|
||||||
|
|||||||
@@ -20,22 +20,26 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
|||||||
@override
|
@override
|
||||||
bool get wantKeepAlive => true;
|
bool get wantKeepAlive => true;
|
||||||
|
|
||||||
|
bool _hasLoadedTrips = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
// MODIFIÉ : Attendre un frame avant de charger
|
// MODIFIÉ : Utiliser addPostFrameCallback pour attendre que le widget tree soit prêt
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
_loadTripsIfUserLoaded();
|
_loadTripsIfUserLoaded();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _loadTripsIfUserLoaded() {
|
void _loadTripsIfUserLoaded() {
|
||||||
|
if (!_hasLoadedTrips && mounted) {
|
||||||
final userState = context.read<UserBloc>().state;
|
final userState = context.read<UserBloc>().state;
|
||||||
if (userState is UserLoaded) {
|
if (userState is UserLoaded) {
|
||||||
print('🚀 Chargement initial des trips pour ${userState.user.id}');
|
_hasLoadedTrips = true;
|
||||||
context.read<TripBloc>().add(LoadTripsByUserId(userId: userState.user.id));
|
context.read<TripBloc>().add(LoadTripsByUserId(userId: userState.user.id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -93,7 +97,6 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
} else if (tripState is TripCreated) {
|
} else if (tripState is TripCreated) {
|
||||||
// Afficher un message de succès temporaire
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
content: Text('Voyage en cours de création...'),
|
content: Text('Voyage en cours de création...'),
|
||||||
@@ -104,12 +107,17 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
builder: (context, tripState) {
|
builder: (context, tripState) {
|
||||||
|
// AJOUTÉ : Si l'état est initial et qu'on n'a pas encore chargé, charger maintenant
|
||||||
|
if (tripState is TripInitial && !_hasLoadedTrips) {
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
_loadTripsIfUserLoaded();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: RefreshIndicator(
|
body: RefreshIndicator(
|
||||||
onRefresh: () async {
|
onRefresh: () async {
|
||||||
print('🔄 Pull to refresh');
|
|
||||||
context.read<TripBloc>().add(LoadTripsByUserId(userId: user.id));
|
context.read<TripBloc>().add(LoadTripsByUserId(userId: user.id));
|
||||||
// Attendre que le chargement soit terminé
|
|
||||||
await Future.delayed(Duration(milliseconds: 500));
|
await Future.delayed(Duration(milliseconds: 500));
|
||||||
},
|
},
|
||||||
child: SingleChildScrollView(
|
child: SingleChildScrollView(
|
||||||
@@ -137,6 +145,8 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
|||||||
tripState.trips.isEmpty
|
tripState.trips.isEmpty
|
||||||
? _buildEmptyState()
|
? _buildEmptyState()
|
||||||
: _buildTripsList(tripState.trips)
|
: _buildTripsList(tripState.trips)
|
||||||
|
else if (tripState is TripInitial)
|
||||||
|
_buildLoadingState() // Afficher le loader pendant le premier chargement
|
||||||
else
|
else
|
||||||
_buildEmptyState(),
|
_buildEmptyState(),
|
||||||
|
|
||||||
@@ -152,9 +162,7 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
|||||||
MaterialPageRoute(builder: (context) => const CreateTripContent()),
|
MaterialPageRoute(builder: (context) => const CreateTripContent()),
|
||||||
);
|
);
|
||||||
|
|
||||||
// AJOUTÉ : Recharger manuellement après retour
|
|
||||||
if (result == true && mounted) {
|
if (result == true && mounted) {
|
||||||
print('🔄 Retour de création, rechargement...');
|
|
||||||
context.read<TripBloc>().add(LoadTripsByUserId(userId: user.id));
|
context.read<TripBloc>().add(LoadTripsByUserId(userId: user.id));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -251,7 +259,6 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
|||||||
),
|
),
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
// AJOUTÉ : Recharger après retour des détails
|
|
||||||
final result = await Navigator.push(
|
final result = await Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
@@ -262,7 +269,6 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
|||||||
if (result == true && mounted) {
|
if (result == true && mounted) {
|
||||||
final userState = context.read<UserBloc>().state;
|
final userState = context.read<UserBloc>().state;
|
||||||
if (userState is UserLoaded) {
|
if (userState is UserLoaded) {
|
||||||
print('🔄 Retour des détails, rechargement...');
|
|
||||||
context.read<TripBloc>().add(LoadTripsByUserId(userId: userState.user.id));
|
context.read<TripBloc>().add(LoadTripsByUserId(userId: userState.user.id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -304,7 +310,7 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
|||||||
Container(
|
Container(
|
||||||
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
color: _getStatusColor(trip).withOpacity(0.2),
|
color: _getStatusColor(trip).withValues(alpha: 0.2),
|
||||||
borderRadius: BorderRadius.circular(12),
|
borderRadius: BorderRadius.circular(12),
|
||||||
),
|
),
|
||||||
child: Text(
|
child: Text(
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:travel_mate/blocs/group/group_bloc.dart';
|
||||||
|
import 'package:travel_mate/blocs/group/group_event.dart';
|
||||||
|
import 'package:travel_mate/blocs/trip/trip_bloc.dart';
|
||||||
|
import 'package:travel_mate/blocs/trip/trip_event.dart';
|
||||||
import 'package:travel_mate/components/home/create_trip_content.dart';
|
import 'package:travel_mate/components/home/create_trip_content.dart';
|
||||||
import 'package:travel_mate/data/models/trip.dart';
|
import 'package:travel_mate/data/models/trip.dart';
|
||||||
|
|
||||||
@@ -117,6 +122,54 @@ class _ShowTripDetailsContentState extends State<ShowTripDetailsContent> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
SizedBox(
|
||||||
|
width: double.infinity,
|
||||||
|
height: 50,
|
||||||
|
child: ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => AlertDialog(
|
||||||
|
title: Text('Confirmer la suppression'),
|
||||||
|
content: Text(
|
||||||
|
'Êtes-vous sûr de vouloir supprimer ce voyage ? Cette action est irréversible.'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context),
|
||||||
|
child: Text('Annuler'),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () {
|
||||||
|
context.read<TripBloc>().add(TripDeleteRequested(tripId: widget.trip.id!));
|
||||||
|
context.read<GroupBloc>().add(DeleteGroup(widget.trip.id!));
|
||||||
|
Navigator.pop(context); // Fermer le dialogue
|
||||||
|
},
|
||||||
|
child: Text(
|
||||||
|
'Supprimer',
|
||||||
|
style: TextStyle(color: Colors.red),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: Colors.red,
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: Text(
|
||||||
|
'Supprimer le voyage',
|
||||||
|
style: TextStyle(
|
||||||
|
color: Colors.white,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
fontSize: 16,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:travel_mate/blocs/trip/trip_bloc.dart';
|
||||||
|
import 'package:travel_mate/blocs/trip/trip_event.dart';
|
||||||
import '../components/home/home_content.dart';
|
import '../components/home/home_content.dart';
|
||||||
import '../components/settings/settings_content.dart';
|
import '../components/settings/settings_content.dart';
|
||||||
import '../components/map/map_content.dart';
|
import '../components/map/map_content.dart';
|
||||||
@@ -9,8 +11,6 @@ import '../blocs/user/user_bloc.dart';
|
|||||||
import '../blocs/user/user_event.dart';
|
import '../blocs/user/user_event.dart';
|
||||||
import '../blocs/auth/auth_bloc.dart';
|
import '../blocs/auth/auth_bloc.dart';
|
||||||
import '../blocs/auth/auth_event.dart';
|
import '../blocs/auth/auth_event.dart';
|
||||||
import '../blocs/trip/trip_bloc.dart';
|
|
||||||
import '../blocs/trip/trip_event.dart';
|
|
||||||
|
|
||||||
class HomePage extends StatefulWidget {
|
class HomePage extends StatefulWidget {
|
||||||
const HomePage({super.key});
|
const HomePage({super.key});
|
||||||
@@ -106,8 +106,8 @@ class _HomePageState extends State<HomePage> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (shouldLogout != true || !mounted) return;
|
if (shouldLogout != true || !mounted) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
context.read<TripBloc>().add(ResetTrips());
|
||||||
context.read<UserBloc>().add(UserLoggedOut());
|
context.read<UserBloc>().add(UserLoggedOut());
|
||||||
_pageCache.clear();
|
_pageCache.clear();
|
||||||
context.read<AuthBloc>().add(AuthSignOutRequested());
|
context.read<AuthBloc>().add(AuthSignOutRequested());
|
||||||
|
|||||||
@@ -173,8 +173,20 @@ class GroupRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteGroup(String groupId) async {
|
Future<void> deleteGroup(String tripId) async {
|
||||||
try {
|
try {
|
||||||
|
final querySnapshot = await _groupsCollection
|
||||||
|
.where('tripId', isEqualTo: tripId)
|
||||||
|
.limit(1)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
if (querySnapshot.docs.isEmpty) {
|
||||||
|
throw Exception('Aucun groupe trouvé pour ce voyage');
|
||||||
|
}
|
||||||
|
|
||||||
|
final groupDoc = querySnapshot.docs.first;
|
||||||
|
final groupId = groupDoc.id;
|
||||||
|
|
||||||
final membersSnapshot = await _membersCollection(groupId).get();
|
final membersSnapshot = await _membersCollection(groupId).get();
|
||||||
for (var doc in membersSnapshot.docs) {
|
for (var doc in membersSnapshot.docs) {
|
||||||
await doc.reference.delete();
|
await doc.reference.delete();
|
||||||
|
|||||||
Reference in New Issue
Block a user