- 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.
108 lines
3.3 KiB
Dart
108 lines
3.3 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import '../data/models/trip.dart';
|
|
|
|
class TripRepository {
|
|
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
|
|
|
CollectionReference get _tripsCollection => _firestore.collection('trips');
|
|
|
|
// Récupérer tous les voyages d'un utilisateur
|
|
Stream<List<Trip>> getTripsByUserId(String userId) {
|
|
print('🔍 Chargement des trips pour userId: $userId');
|
|
|
|
try {
|
|
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 un voyage par son ID
|
|
Future<Trip?> getTripById(String tripId) async {
|
|
try {
|
|
final doc = await _tripsCollection.doc(tripId).get();
|
|
|
|
if (!doc.exists) {
|
|
print('⚠️ Voyage $tripId non trouvé');
|
|
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<void> updateTrip(String tripId, Trip trip) async {
|
|
try {
|
|
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<void> deleteTrip(String tripId) async {
|
|
try {
|
|
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');
|
|
}
|
|
}
|
|
} |