feat: Refactor group deletion logic to use tripId and add reset trips functionality
This commit is contained in:
@@ -19,21 +19,25 @@ class HomeContent extends StatefulWidget {
|
||||
class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClientMixin {
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
|
||||
bool _hasLoadedTrips = false;
|
||||
|
||||
@override
|
||||
void 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((_) {
|
||||
_loadTripsIfUserLoaded();
|
||||
});
|
||||
}
|
||||
|
||||
void _loadTripsIfUserLoaded() {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
if (userState is UserLoaded) {
|
||||
print('🚀 Chargement initial des trips pour ${userState.user.id}');
|
||||
context.read<TripBloc>().add(LoadTripsByUserId(userId: userState.user.id));
|
||||
if (!_hasLoadedTrips && mounted) {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
if (userState is UserLoaded) {
|
||||
_hasLoadedTrips = true;
|
||||
context.read<TripBloc>().add(LoadTripsByUserId(userId: userState.user.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +97,6 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
||||
),
|
||||
);
|
||||
} else if (tripState is TripCreated) {
|
||||
// Afficher un message de succès temporaire
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Voyage en cours de création...'),
|
||||
@@ -104,12 +107,17 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
||||
}
|
||||
},
|
||||
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(
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
print('🔄 Pull to refresh');
|
||||
context.read<TripBloc>().add(LoadTripsByUserId(userId: user.id));
|
||||
// Attendre que le chargement soit terminé
|
||||
await Future.delayed(Duration(milliseconds: 500));
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
@@ -137,6 +145,8 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
||||
tripState.trips.isEmpty
|
||||
? _buildEmptyState()
|
||||
: _buildTripsList(tripState.trips)
|
||||
else if (tripState is TripInitial)
|
||||
_buildLoadingState() // Afficher le loader pendant le premier chargement
|
||||
else
|
||||
_buildEmptyState(),
|
||||
|
||||
@@ -152,9 +162,7 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
||||
MaterialPageRoute(builder: (context) => const CreateTripContent()),
|
||||
);
|
||||
|
||||
// AJOUTÉ : Recharger manuellement après retour
|
||||
if (result == true && mounted) {
|
||||
print('🔄 Retour de création, rechargement...');
|
||||
context.read<TripBloc>().add(LoadTripsByUserId(userId: user.id));
|
||||
}
|
||||
},
|
||||
@@ -251,7 +259,6 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
||||
),
|
||||
child: InkWell(
|
||||
onTap: () async {
|
||||
// AJOUTÉ : Recharger après retour des détails
|
||||
final result = await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
@@ -262,7 +269,6 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
||||
if (result == true && mounted) {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
if (userState is UserLoaded) {
|
||||
print('🔄 Retour des détails, rechargement...');
|
||||
context.read<TripBloc>().add(LoadTripsByUserId(userId: userState.user.id));
|
||||
}
|
||||
}
|
||||
@@ -304,7 +310,7 @@ class _HomeContentState extends State<HomeContent> with AutomaticKeepAliveClient
|
||||
Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: _getStatusColor(trip).withOpacity(0.2),
|
||||
color: _getStatusColor(trip).withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
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/data/models/trip.dart';
|
||||
|
||||
@@ -92,8 +97,8 @@ class _ShowTripDetailsContentState extends State<ShowTripDetailsContent> {
|
||||
MaterialPageRoute(
|
||||
builder: (context) => CreateTripContent(
|
||||
tripToEdit: widget.trip,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user