feat: Enhance trip management features and improve UI responsiveness
- Implemented AutomaticKeepAliveClientMixin in HomeContent to maintain state during navigation. - Modified trip loading logic to trigger after the first frame for better performance. - Updated trip loading events to use LoadTripsByUserId for consistency. - Added temporary success messages for trip creation and operations. - Improved UI elements for better user experience, including updated text styles and spacing. - Refactored trip model to support Firestore timestamps and improved error handling during parsing. - Streamlined trip repository methods for better clarity and performance. - Enhanced trip service methods to ensure correct mapping from Firestore documents. - Removed unnecessary trip reset logic on logout.
This commit is contained in:
@@ -2,112 +2,107 @@ import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import '../data/models/trip.dart';
|
||||
|
||||
class TripRepository {
|
||||
final FirebaseFirestore _firestore;
|
||||
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
||||
|
||||
TripRepository({FirebaseFirestore? firestore})
|
||||
: _firestore = firestore ?? FirebaseFirestore.instance;
|
||||
CollectionReference get _tripsCollection => _firestore.collection('trips');
|
||||
|
||||
// Créer un voyage
|
||||
Future<Trip> createTrip(Trip trip) async {
|
||||
// Récupérer tous les voyages d'un utilisateur
|
||||
Stream<List<Trip>> getTripsByUserId(String userId) {
|
||||
print('🔍 Chargement des trips pour userId: $userId');
|
||||
|
||||
try {
|
||||
final docRef = await _firestore.collection('trips').add(trip.toMap());
|
||||
final createdTrip = trip.copyWith(id: docRef.id);
|
||||
|
||||
// Mettre à jour avec l'ID généré
|
||||
await docRef.update({'id': docRef.id});
|
||||
|
||||
return createdTrip;
|
||||
return _tripsCollection
|
||||
.where('participants', arrayContains: userId)
|
||||
.snapshots()
|
||||
.map((snapshot) {
|
||||
print('📦 Snapshot reçu: ${snapshot.docs.length} documents');
|
||||
|
||||
final trips = snapshot.docs
|
||||
.map((doc) {
|
||||
try {
|
||||
final data = doc.data() as Map<String, dynamic>;
|
||||
print('📄 Document ${doc.id}: ${data.keys.toList()}');
|
||||
return Trip.fromMap(data, doc.id);
|
||||
} catch (e) {
|
||||
print('❌ Erreur parsing trip ${doc.id}: $e');
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.whereType<Trip>()
|
||||
.toList();
|
||||
|
||||
print('✅ ${trips.length} trips parsés avec succès');
|
||||
return trips;
|
||||
});
|
||||
} catch (e) {
|
||||
print('❌ Erreur getTripsByUserId: $e');
|
||||
throw Exception('Erreur lors de la récupération des voyages: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Créer un voyage et retourner son ID
|
||||
Future<String> createTrip(Trip trip) async {
|
||||
try {
|
||||
print('📝 Création du voyage: ${trip.title}');
|
||||
|
||||
final tripData = trip.toMap();
|
||||
// Ne pas modifier les timestamps ici, ils sont déjà au bon format
|
||||
final docRef = await _tripsCollection.add(tripData);
|
||||
|
||||
print('✅ Voyage créé avec ID: ${docRef.id}');
|
||||
return docRef.id;
|
||||
} catch (e) {
|
||||
print('❌ Erreur création voyage: $e');
|
||||
throw Exception('Erreur lors de la création du voyage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Récupérer les voyages d'un utilisateur
|
||||
Stream<List<Trip>> getUserTrips(String userId) {
|
||||
return _firestore
|
||||
.collection('trips')
|
||||
.where('createdBy', isEqualTo: userId)
|
||||
.snapshots()
|
||||
.map((snapshot) {
|
||||
return snapshot.docs.map((doc) {
|
||||
final data = doc.data();
|
||||
return Trip.fromMap({...data, 'id': doc.id});
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
// Récupérer les voyages où l'utilisateur est participant
|
||||
Stream<List<Trip>> getSharedTrips(String userId) {
|
||||
return _firestore
|
||||
.collection('trips')
|
||||
.where('participants', arrayContains: userId)
|
||||
.snapshots()
|
||||
.map((snapshot) {
|
||||
return snapshot.docs.map((doc) {
|
||||
final data = doc.data();
|
||||
return Trip.fromMap({...data, 'id': doc.id});
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
// Récupérer un voyage par ID
|
||||
// Récupérer un voyage par son ID
|
||||
Future<Trip?> getTripById(String tripId) async {
|
||||
try {
|
||||
final doc = await _firestore.collection('trips').doc(tripId).get();
|
||||
if (doc.exists) {
|
||||
final data = doc.data() as Map<String, dynamic>;
|
||||
return Trip.fromMap({...data, 'id': doc.id});
|
||||
final doc = await _tripsCollection.doc(tripId).get();
|
||||
|
||||
if (!doc.exists) {
|
||||
print('⚠️ Voyage $tripId non trouvé');
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
|
||||
return Trip.fromMap(doc.data() as Map<String, dynamic>, doc.id);
|
||||
} catch (e) {
|
||||
print('❌ Erreur getTripById: $e');
|
||||
throw Exception('Erreur lors de la récupération du voyage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre à jour un voyage
|
||||
Future<bool> updateTrip(Trip trip) async {
|
||||
Future<void> updateTrip(String tripId, Trip trip) async {
|
||||
try {
|
||||
await _firestore
|
||||
.collection('trips')
|
||||
.doc(trip.id)
|
||||
.update(trip.toMap());
|
||||
return true;
|
||||
print('📝 Mise à jour du voyage: $tripId');
|
||||
|
||||
final tripData = trip.toMap();
|
||||
// Mettre à jour le timestamp de modification
|
||||
tripData['updatedAt'] = Timestamp.now();
|
||||
|
||||
await _tripsCollection.doc(tripId).update(tripData);
|
||||
|
||||
print('✅ Voyage $tripId mis à jour');
|
||||
} catch (e) {
|
||||
print('❌ Erreur mise à jour voyage: $e');
|
||||
throw Exception('Erreur lors de la mise à jour du voyage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un voyage
|
||||
Future<bool> deleteTrip(String tripId) async {
|
||||
Future<void> deleteTrip(String tripId) async {
|
||||
try {
|
||||
await _firestore.collection('trips').doc(tripId).delete();
|
||||
return true;
|
||||
print('🗑️ Suppression du voyage: $tripId');
|
||||
|
||||
await _tripsCollection.doc(tripId).delete();
|
||||
|
||||
print('✅ Voyage $tripId supprimé');
|
||||
} catch (e) {
|
||||
print('❌ Erreur suppression voyage: $e');
|
||||
throw Exception('Erreur lors de la suppression du voyage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter un participant
|
||||
Future<bool> addParticipant(String tripId, String participantEmail) async {
|
||||
try {
|
||||
await _firestore.collection('trips').doc(tripId).update({
|
||||
'participants': FieldValue.arrayUnion([participantEmail])
|
||||
});
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de l\'ajout du participant: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Retirer un participant
|
||||
Future<bool> removeParticipant(String tripId, String participantEmail) async {
|
||||
try {
|
||||
await _firestore.collection('trips').doc(tripId).update({
|
||||
'participants': FieldValue.arrayRemove([participantEmail])
|
||||
});
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors du retrait du participant: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user