Add UserStateWrapper and ProfileImageService for user state management and profile image handling

This commit is contained in:
Van Leemput Dayron
2025-11-05 09:31:58 +01:00
parent 30dca05e15
commit 75c12e35a5
6 changed files with 275 additions and 131 deletions

View File

@@ -11,17 +11,17 @@ class TripImageService {
/// Charge les images manquantes pour une liste de voyages
Future<void> loadMissingImages(List<Trip> trips) async {
final tripsWithoutImage = trips.where(
(trip) => trip.imageUrl == null || trip.imageUrl!.isEmpty
).toList();
final tripsWithoutImage = trips
.where((trip) => trip.imageUrl == null || trip.imageUrl!.isEmpty)
.toList();
if (tripsWithoutImage.isEmpty) {
return;
}
}
for (final trip in tripsWithoutImage) {
try {
await _loadImageForTrip(trip);
// Petite pause entre les requêtes pour éviter de surcharger l'API
await Future.delayed(const Duration(milliseconds: 500));
} catch (e) {
@@ -35,42 +35,40 @@ class TripImageService {
/// Charge l'image pour un voyage spécifique
Future<void> _loadImageForTrip(Trip trip) async {
// D'abord vérifier si une image existe déjà dans le Storage
String? imageUrl = await _placeImageService.getExistingImageUrl(trip.location);
String? imageUrl = await _placeImageService.getExistingImageUrl(
trip.location,
);
// Si aucune image n'existe, en télécharger une nouvelle
if (imageUrl == null) {
imageUrl = await _placeImageService.getPlaceImageUrl(trip.location);
}
imageUrl ??= await _placeImageService.getPlaceImageUrl(trip.location);
if (imageUrl != null && trip.id != null) {
// Mettre à jour le voyage avec l'image (existante ou nouvelle)
final updatedTrip = trip.copyWith(
imageUrl: imageUrl,
updatedAt: DateTime.now(),
);
await _tripRepository.updateTrip(trip.id!, updatedTrip);
} else {
}
} else {}
}
/// Recharge l'image d'un voyage spécifique (force le rechargement)
Future<String?> reloadImageForTrip(Trip trip) async {
try {
final imageUrl = await _placeImageService.getPlaceImageUrl(trip.location);
if (imageUrl != null && trip.id != null) {
final updatedTrip = trip.copyWith(
imageUrl: imageUrl,
updatedAt: DateTime.now(),
);
await _tripRepository.updateTrip(trip.id!, updatedTrip);
return imageUrl;
}
return null;
} catch (e) {
_errorService.logError(
@@ -87,16 +85,15 @@ class TripImageService {
// Récupérer tous les voyages de l'utilisateur
final tripsStream = _tripRepository.getTripsByUserId(userId);
final trips = await tripsStream.first;
// Extraire toutes les URLs d'images utilisées
final usedImageUrls = trips
.where((trip) => trip.imageUrl != null && trip.imageUrl!.isNotEmpty)
.map((trip) => trip.imageUrl!)
.toList();
// Nettoyer les images inutilisées
await _placeImageService.cleanupUnusedImages(usedImageUrls);
} catch (e) {
_errorService.logError(
'TripImageService',
@@ -110,10 +107,12 @@ class TripImageService {
try {
final tripsStream = _tripRepository.getTripsByUserId(userId);
final trips = await tripsStream.first;
final tripsWithImages = trips.where((trip) => trip.imageUrl != null && trip.imageUrl!.isNotEmpty).length;
final tripsWithImages = trips
.where((trip) => trip.imageUrl != null && trip.imageUrl!.isNotEmpty)
.length;
final tripsWithoutImages = trips.length - tripsWithImages;
return {
'totalTrips': trips.length,
'tripsWithImages': tripsWithImages,
@@ -121,7 +120,10 @@ class TripImageService {
'timestamp': DateTime.now().toIso8601String(),
};
} catch (e) {
_errorService.logError('TripImageService', 'Erreur lors de l\'obtention des statistiques: $e');
_errorService.logError(
'TripImageService',
'Erreur lors de l\'obtention des statistiques: $e',
);
return {
'error': e.toString(),
'timestamp': DateTime.now().toIso8601String(),
@@ -140,4 +142,4 @@ class TripImageService {
);
}
}
}
}