Refactor user and theme management to use BLoC pattern; remove provider classes and integrate new services for user and group functionalities
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:travel_mate/components/home/create_trip_content.dart';
|
||||
import '../../providers/user_provider.dart';
|
||||
import '../../services/trip_service.dart';
|
||||
import '../../data/models/trip.dart';
|
||||
import '../home/show_trip_details_content.dart';
|
||||
import '../../blocs/user/user_bloc.dart';
|
||||
import '../../blocs/user/user_state.dart';
|
||||
import '../../blocs/trip/trip_bloc.dart';
|
||||
import '../../blocs/trip/trip_state.dart';
|
||||
import '../../blocs/trip/trip_event.dart';
|
||||
import '../../data/models/trip.dart';
|
||||
|
||||
class HomeContent extends StatefulWidget {
|
||||
const HomeContent({super.key});
|
||||
@@ -14,61 +17,87 @@ class HomeContent extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _HomeContentState extends State<HomeContent> {
|
||||
final TripService _tripService = TripService();
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Charger les trips quand le widget est initialisé
|
||||
_loadTripsIfUserLoaded();
|
||||
}
|
||||
|
||||
void _loadTripsIfUserLoaded() {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
if (userState is UserLoaded) {
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: userState.user.id));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Consumer<UserProvider>(
|
||||
builder: (context, userProvider, child) {
|
||||
final user = userProvider.currentUser;
|
||||
|
||||
if (user == null || user.id == null) {
|
||||
return Center(
|
||||
child: Text('Utilisateur non connecté'),
|
||||
);
|
||||
}
|
||||
|
||||
return StreamBuilder<List<Trip>>(
|
||||
stream: _tripService.getTripsStreamByUser(user.id!, user.email),
|
||||
builder: (context, snapshot) {
|
||||
print('StreamBuilder - ConnectionState: ${snapshot.connectionState}');
|
||||
print('StreamBuilder - HasError: ${snapshot.hasError}');
|
||||
print('StreamBuilder - Data: ${snapshot.data?.length ?? 0} trips');
|
||||
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return _buildLoadingState();
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
print('Erreur du stream: ${snapshot.error}');
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error, size: 64, color: Colors.red),
|
||||
SizedBox(height: 16),
|
||||
Text('Erreur lors du chargement des voyages'),
|
||||
SizedBox(height: 8),
|
||||
Text('${snapshot.error}'),
|
||||
SizedBox(height: 8),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
setState(() {}); // Forcer le rebuild
|
||||
},
|
||||
child: Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final trips = snapshot.data ?? [];
|
||||
print('Trips reçus du stream: ${trips.length}');
|
||||
|
||||
return RefreshIndicator(
|
||||
return BlocBuilder<UserBloc, UserState>(
|
||||
builder: (context, userState) {
|
||||
if (userState is UserLoading) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (userState is UserError) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error, size: 64, color: Colors.red),
|
||||
SizedBox(height: 16),
|
||||
Text('Erreur: ${userState.message}'),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (userState is! UserLoaded) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Text('Veuillez vous connecter'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final user = userState.user;
|
||||
|
||||
// Charger les trips si ce n'est pas déjà fait
|
||||
if (context.read<TripBloc>().state is TripInitial) {
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: user.id));
|
||||
}
|
||||
|
||||
return BlocConsumer<TripBloc, TripState>(
|
||||
listener: (context, tripState) {
|
||||
if (tripState is TripOperationSuccess) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(tripState.message),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
// Recharger les trips après une opération réussie
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: user.id));
|
||||
} else if (tripState is TripError) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(tripState.message),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, tripState) {
|
||||
return Scaffold(
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
setState(() {}); // Forcer le rebuild du stream
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: user.id));
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
physics: AlwaysScrollableScrollPhysics(),
|
||||
@@ -88,43 +117,47 @@ class _HomeContentState extends State<HomeContent> {
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
|
||||
// Contenu principal
|
||||
trips.isEmpty ? _buildEmptyState() : _buildTripsList(trips),
|
||||
// Contenu principal basé sur l'état du TripBloc
|
||||
if (tripState is TripLoading)
|
||||
_buildLoadingState()
|
||||
else if (tripState is TripError)
|
||||
_buildErrorState(tripState.message, user.id)
|
||||
else if (tripState is TripLoaded)
|
||||
tripState.trips.isEmpty
|
||||
? _buildEmptyState()
|
||||
: _buildTripsList(tripState.trips)
|
||||
else
|
||||
_buildEmptyState(),
|
||||
|
||||
// Espacement en bas pour éviter que le FAB cache le contenu
|
||||
const SizedBox(height: 80),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
// FloatingActionButton
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () async {
|
||||
final result = await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => CreateTripContent()),
|
||||
);
|
||||
|
||||
// Le stream se mettra à jour automatiquement
|
||||
if (result == true) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Voyage créé ! Il apparaîtra dans quelques secondes.'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
|
||||
// FloatingActionButton
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () async {
|
||||
final result = await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => CreateTripContent()),
|
||||
);
|
||||
|
||||
if (result == true) {
|
||||
// Recharger les trips
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: user.id));
|
||||
}
|
||||
},
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
|
||||
);
|
||||
}
|
||||
},
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -137,6 +170,35 @@ class _HomeContentState extends State<HomeContent> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildErrorState(String error, String userId) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.error, size: 64, color: Colors.red),
|
||||
SizedBox(height: 16),
|
||||
Text('Erreur lors du chargement des voyages'),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
error,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<TripBloc>().add(TripLoadRequested(userId: userId));
|
||||
},
|
||||
child: Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
return Center(
|
||||
child: Padding(
|
||||
@@ -182,7 +244,6 @@ class _HomeContentState extends State<HomeContent> {
|
||||
final colors = [Colors.blue, Colors.orange, Colors.green, Colors.purple, Colors.red];
|
||||
final color = colors[trip.title.hashCode.abs() % colors.length];
|
||||
|
||||
// Détecter le thème actuel
|
||||
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
||||
final textColor = isDarkMode ? Colors.white : Colors.black;
|
||||
final secondaryTextColor = isDarkMode ? Colors.white70 : Colors.grey[700];
|
||||
@@ -255,9 +316,9 @@ class _HomeContentState extends State<HomeContent> {
|
||||
Expanded(
|
||||
child: Text(
|
||||
trip.location,
|
||||
style: TextStyle(
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
color: textColor,
|
||||
color: Colors.white,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
@@ -341,7 +402,7 @@ class _HomeContentState extends State<HomeContent> {
|
||||
Icon(Icons.euro, size: 16, color: iconColor),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
'Budget: ${trip.budget?.toStringAsFixed(2)}€',
|
||||
'Budget: ${trip.budget!.toStringAsFixed(2)}€',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: iconColor,
|
||||
|
||||
Reference in New Issue
Block a user