feat: Add logger service and improve expense dialog with enhanced receipt management and calculation logic.

This commit is contained in:
Van Leemput Dayron
2025-11-28 12:54:54 +01:00
parent cad9d42128
commit fd710b8cb8
35 changed files with 2148 additions and 1296 deletions

View File

@@ -11,7 +11,6 @@ class PlaceImageService {
/// Récupère l'URL de l'image d'un lieu depuis Google Places API
Future<String?> getPlaceImageUrl(String location) async {
try {
// ÉTAPE 1: Vérifier d'abord si une image existe déjà dans le Storage
final existingUrl = await _checkExistingImage(location);
@@ -19,26 +18,25 @@ class PlaceImageService {
return existingUrl;
}
if (_apiKey.isEmpty) {
_errorService.logError('PlaceImageService', 'Google Maps API key manquante');
_errorService.logError(
'PlaceImageService',
'Google Maps API key manquante',
);
return null;
}
// ÉTAPE 2: Recherche via Google Places API seulement si aucune image n'existe
final searchTerms = _generateSearchTerms(location);
for (final searchTerm in searchTerms) {
// 1. Rechercher le lieu
final placeId = await _getPlaceIdForTerm(searchTerm);
if (placeId == null) continue;
// 2. Récupérer les détails du lieu avec les photos
final photoReference = await _getPhotoReference(placeId);
if (photoReference == null) continue;
// 3. Télécharger et sauvegarder l'image (seulement si pas d'image existante)
final imageUrl = await _downloadAndSaveImage(photoReference, location);
@@ -46,11 +44,13 @@ class PlaceImageService {
return imageUrl;
}
}
return null;
return null;
} catch (e) {
_errorService.logError('PlaceImageService', 'Erreur lors de la récupération de l\'image: $e');
_errorService.logError(
'PlaceImageService',
'Erreur lors de la récupération de l\'image: $e',
);
return null;
}
}
@@ -58,11 +58,11 @@ class PlaceImageService {
/// Génère différents termes de recherche pour optimiser les résultats
List<String> _generateSearchTerms(String location) {
final terms = <String>[];
// Ajouter des termes spécifiques pour les villes connues
final citySpecificTerms = _getCitySpecificTerms(location.toLowerCase());
terms.addAll(citySpecificTerms);
// Termes génériques avec attractions
terms.addAll([
'$location attractions touristiques monuments',
@@ -74,14 +74,14 @@ class PlaceImageService {
'$location skyline',
location, // Terme original en dernier
]);
return terms;
}
/// Retourne des termes spécifiques pour des villes connues
List<String> _getCitySpecificTerms(String location) {
final specific = <String>[];
if (location.contains('paris')) {
specific.addAll([
'Tour Eiffel Paris',
@@ -120,20 +120,23 @@ class PlaceImageService {
'Tokyo Skytree',
]);
}
return specific;
}
/// Recherche un place ID pour un terme spécifique
Future<String?> _getPlaceIdForTerm(String searchTerm) async {
// Essayer d'abord avec les attractions touristiques
String? placeId = await _searchPlaceWithType(searchTerm, 'tourist_attraction');
String? placeId = await _searchPlaceWithType(
searchTerm,
'tourist_attraction',
);
if (placeId != null) return placeId;
// Puis avec les points d'intérêt
placeId = await _searchPlaceWithType(searchTerm, 'point_of_interest');
if (placeId != null) return placeId;
// Enfin recherche générale
return await _searchPlaceGeneral(searchTerm);
}
@@ -146,14 +149,14 @@ class PlaceImageService {
'?query=${Uri.encodeComponent('$location attractions monuments')}'
'&type=$type'
'&fields=place_id,name,types,rating'
'&key=$_apiKey'
'&key=$_apiKey',
);
final response = await http.get(url);
if (response.statusCode == 200) {
final data = json.decode(response.body);
if (data['status'] == 'OK' && data['results'].isNotEmpty) {
// Prioriser les résultats avec des ratings élevés
final results = data['results'] as List;
@@ -162,7 +165,7 @@ class PlaceImageService {
final bRating = b['rating'] ?? 0.0;
return bRating.compareTo(aRating);
});
return results.first['place_id'];
}
}
@@ -180,14 +183,14 @@ class PlaceImageService {
'?input=${Uri.encodeComponent(location)}'
'&inputtype=textquery'
'&fields=place_id'
'&key=$_apiKey'
'&key=$_apiKey',
);
final response = await http.get(url);
if (response.statusCode == 200) {
final data = json.decode(response.body);
if (data['status'] == 'OK' && data['candidates'].isNotEmpty) {
return data['candidates'][0]['place_id'];
}
@@ -205,24 +208,23 @@ class PlaceImageService {
'https://maps.googleapis.com/maps/api/place/details/json'
'?place_id=$placeId'
'&fields=photos'
'&key=$_apiKey'
'&key=$_apiKey',
);
final response = await http.get(url);
if (response.statusCode == 200) {
final data = json.decode(response.body);
if (data['status'] == 'OK' &&
data['result'] != null &&
data['result']['photos'] != null &&
if (data['status'] == 'OK' &&
data['result'] != null &&
data['result']['photos'] != null &&
data['result']['photos'].isNotEmpty) {
final photos = data['result']['photos'] as List;
// Trier les photos pour obtenir les meilleures
final sortedPhotos = _sortPhotosByQuality(photos);
if (sortedPhotos.isNotEmpty) {
return sortedPhotos.first['photo_reference'];
}
@@ -230,7 +232,10 @@ class PlaceImageService {
}
return null;
} catch (e) {
_errorService.logError('PlaceImageService', 'Erreur lors de la récupération de la référence photo: $e');
_errorService.logError(
'PlaceImageService',
'Erreur lors de la récupération de la référence photo: $e',
);
return null;
}
}
@@ -238,64 +243,68 @@ class PlaceImageService {
/// Trie les photos par qualité (largeur/hauteur et popularité)
List<Map<String, dynamic>> _sortPhotosByQuality(List photos) {
final photoList = photos.cast<Map<String, dynamic>>();
photoList.sort((a, b) {
// Priorité 1: Photos horizontales (largeur > hauteur)
final aWidth = a['width'] ?? 0;
final aHeight = a['height'] ?? 0;
final bWidth = b['width'] ?? 0;
final bHeight = b['height'] ?? 0;
final aIsHorizontal = aWidth > aHeight;
final bIsHorizontal = bWidth > bHeight;
if (aIsHorizontal && !bIsHorizontal) return -1;
if (!aIsHorizontal && bIsHorizontal) return 1;
// Priorité 2: Résolution plus élevée
final aResolution = aWidth * aHeight;
final bResolution = bWidth * bHeight;
if (aResolution != bResolution) {
return bResolution.compareTo(aResolution);
}
// Priorité 3: Ratio d'aspect optimal pour paysage (1.5-2.0)
final aRatio = aWidth > 0 ? aWidth / aHeight : 0;
final bRatio = bWidth > 0 ? bWidth / bHeight : 0;
final idealRatio = 1.7; // Ratio 16:9 environ
final aDiff = (aRatio - idealRatio).abs();
final bDiff = (bRatio - idealRatio).abs();
return aDiff.compareTo(bDiff);
});
return photoList;
}
/// Télécharge l'image et la sauvegarde dans Firebase Storage
Future<String?> _downloadAndSaveImage(String photoReference, String location) async {
Future<String?> _downloadAndSaveImage(
String photoReference,
String location,
) async {
try {
// URL pour télécharger l'image en haute qualité et format horizontal
final imageUrl = 'https://maps.googleapis.com/maps/api/place/photo'
'?maxwidth=1200' // Augmenté pour une meilleure qualité
'&maxheight=800' // Ratio horizontal ~1.5:1
final imageUrl =
'https://maps.googleapis.com/maps/api/place/photo'
'?maxwidth=1200' // Augmenté pour une meilleure qualité
'&maxheight=800' // Ratio horizontal ~1.5:1
'&photo_reference=$photoReference'
'&key=$_apiKey';
// Télécharger l'image
final response = await http.get(Uri.parse(imageUrl));
if (response.statusCode == 200) {
// Créer un nom de fichier unique basé sur la localisation normalisée
final normalizedLocation = _normalizeLocationName(location);
final fileName = '${normalizedLocation}_${DateTime.now().millisecondsSinceEpoch}.jpg';
final fileName =
'${normalizedLocation}_${DateTime.now().millisecondsSinceEpoch}.jpg';
// Référence vers Firebase Storage
final storageRef = _storage.ref().child('trip_images/$fileName');
// Upload de l'image avec métadonnées
final uploadTask = await storageRef.putData(
response.bodyBytes,
@@ -309,15 +318,17 @@ class PlaceImageService {
},
),
);
// Récupérer l'URL de téléchargement
final downloadUrl = await uploadTask.ref.getDownloadURL();
return downloadUrl;
} else {
}
} else {}
return null;
} catch (e) {
_errorService.logError('PlaceImageService', 'Erreur lors du téléchargement/sauvegarde: $e');
_errorService.logError(
'PlaceImageService',
'Erreur lors du téléchargement/sauvegarde: $e',
);
return null;
}
}
@@ -326,45 +337,52 @@ class PlaceImageService {
Future<String?> _checkExistingImage(String location) async {
try {
final normalizedLocation = _normalizeLocationName(location);
final listResult = await _storage.ref('trip_images').listAll();
for (final item in listResult.items) {
try {
final metadata = await item.getMetadata();
final storedNormalizedLocation = metadata.customMetadata?['normalizedLocation'];
final storedNormalizedLocation =
metadata.customMetadata?['normalizedLocation'];
final storedLocation = metadata.customMetadata?['location'];
// Méthode 1: Vérifier avec la location normalisée (nouvelles images)
if (storedNormalizedLocation != null && storedNormalizedLocation == normalizedLocation) {
if (storedNormalizedLocation != null &&
storedNormalizedLocation == normalizedLocation) {
final url = await item.getDownloadURL();
return url;
}
// Méthode 2: Vérifier avec la location originale normalisée (anciennes images)
if (storedLocation != null) {
final storedLocationNormalized = _normalizeLocationName(storedLocation);
final storedLocationNormalized = _normalizeLocationName(
storedLocation,
);
if (storedLocationNormalized == normalizedLocation) {
final url = await item.getDownloadURL();
return url;
}
}
} catch (e) {
// Méthode 3: Essayer de deviner depuis le nom du fichier (fallback)
final fileName = item.name;
if (fileName.toLowerCase().contains(normalizedLocation.toLowerCase())) {
if (fileName.toLowerCase().contains(
normalizedLocation.toLowerCase(),
)) {
try {
final url = await item.getDownloadURL();
return url;
} catch (urlError) {
_errorService.logError('PlaceImageService', 'Erreur lors de la récupération de l\'URL: $urlError');
_errorService.logError(
'PlaceImageService',
'Erreur lors de la récupération de l\'URL: $urlError',
);
}
}
}
}
return null;
} catch (e) {
return null;
@@ -384,23 +402,26 @@ class PlaceImageService {
Future<void> cleanupUnusedImages(List<String> usedImageUrls) async {
try {
final listResult = await _storage.ref('trip_images').listAll();
int deletedCount = 0;
for (final item in listResult.items) {
try {
final url = await item.getDownloadURL();
if (!usedImageUrls.contains(url)) {
await item.delete();
deletedCount++;
}
} catch (e) {
_errorService.logError('PlaceImageService', 'Erreur lors du nettoyage: $e');
_errorService.logError(
'PlaceImageService',
'Erreur lors du nettoyage: $e',
);
}
}
} catch (e) {
_errorService.logError('PlaceImageService', 'Erreur lors du nettoyage: $e');
_errorService.logError(
'PlaceImageService',
'Erreur lors du nettoyage: $e',
);
}
}
@@ -408,18 +429,19 @@ class PlaceImageService {
Future<void> cleanupDuplicateImages() async {
try {
final listResult = await _storage.ref('trip_images').listAll();
// Grouper les images par location normalisée
final Map<String, List<Reference>> locationGroups = {};
for (final item in listResult.items) {
String locationKey = 'unknown';
try {
final metadata = await item.getMetadata();
final storedNormalizedLocation = metadata.customMetadata?['normalizedLocation'];
final storedNormalizedLocation =
metadata.customMetadata?['normalizedLocation'];
final storedLocation = metadata.customMetadata?['location'];
if (storedNormalizedLocation != null) {
locationKey = storedNormalizedLocation;
} else if (storedLocation != null) {
@@ -440,43 +462,44 @@ class PlaceImageService {
locationKey = parts.take(parts.length - 1).join('_');
}
}
if (!locationGroups.containsKey(locationKey)) {
locationGroups[locationKey] = [];
}
locationGroups[locationKey]!.add(item);
}
// Supprimer les doublons (garder le plus récent)
int deletedCount = 0;
for (final entry in locationGroups.entries) {
final location = entry.key;
final images = entry.value;
if (images.length > 1) {
// Trier par timestamp (garder le plus récent)
images.sort((a, b) {
final aTimestamp = _extractTimestampFromName(a.name);
final bTimestamp = _extractTimestampFromName(b.name);
return bTimestamp.compareTo(aTimestamp); // Plus récent en premier
});
// Supprimer tous sauf le premier (plus récent)
for (int i = 1; i < images.length; i++) {
try {
await images[i].delete();
deletedCount++;
} catch (e) {
_errorService.logError('PlaceImageService', 'Erreur lors de la suppression du doublon: $e');
_errorService.logError(
'PlaceImageService',
'Erreur lors de la suppression du doublon: $e',
);
}
}
}
}
} catch (e) {
_errorService.logError('PlaceImageService', 'Erreur lors du nettoyage des doublons: $e');
_errorService.logError(
'PlaceImageService',
'Erreur lors du nettoyage des doublons: $e',
);
}
}
@@ -499,7 +522,10 @@ class PlaceImageService {
}
return null;
} catch (e) {
_errorService.logError('PlaceImageService', 'Erreur lors de la recherche d\'image existante: $e');
_errorService.logError(
'PlaceImageService',
'Erreur lors de la recherche d\'image existante: $e',
);
return null;
}
}
@@ -510,7 +536,10 @@ class PlaceImageService {
final ref = _storage.refFromURL(imageUrl);
await ref.delete();
} catch (e) {
_errorService.logError('PlaceImageService', 'Erreur lors de la suppression de l\'image: $e');
_errorService.logError(
'PlaceImageService',
'Erreur lors de la suppression de l\'image: $e',
);
}
}
}
}