From 2fd5c703c92a8dac9da314bf5e7f4c0dcf7c6b84 Mon Sep 17 00:00:00 2001 From: laurenspriem Date: Thu, 13 Feb 2025 09:33:15 +0530 Subject: [PATCH] [mob][photos] Add trip location names --- mobile/lib/services/search_service.dart | 26 +++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/mobile/lib/services/search_service.dart b/mobile/lib/services/search_service.dart index 7a2fc0ebea..5267d4cb0a 100644 --- a/mobile/lib/services/search_service.dart +++ b/mobile/lib/services/search_service.dart @@ -1498,8 +1498,11 @@ class SearchService { for (final trip in tripsToShow) { final year = DateTime.fromMicrosecondsSinceEpoch(trip.averageCreationTime).year; + final String? locationName = await _tryFindLocationName(trip.files); String name = "Trip in $year!"; - if (year == currentTime.year - 1) { + if (locationName != null) { + name = "Trip to $locationName!"; + } else if (year == currentTime.year - 1) { name = "Last year's trip!"; } final photoSelection = await _bestSelection(trip.files); @@ -1546,8 +1549,11 @@ class SearchService { final year = DateTime.fromMicrosecondsSinceEpoch(trip.averageCreationTime) .year; + final String? locationName = await _tryFindLocationName(trip.files); String name = "Trip in $year!"; - if (year == currentTime.year - 1) { + if (locationName != null) { + name = "Trip to $locationName!"; + } else if (year == currentTime.year - 1) { name = "Last year's trip!"; } final photoSelection = await _bestSelection(trip.files); @@ -1843,6 +1849,22 @@ class SearchService { return ((dayOfYear - 1) ~/ 7) + 1; } + Future _tryFindLocationName(List files) async { + final results = await locationService.getFilesInCity(files, ''); + final List sortedByResultCount = results.keys.toList() + ..sort((a, b) => results[b]!.length.compareTo(results[a]!.length)); + if (sortedByResultCount.isEmpty) return null; + final biggestPlace = sortedByResultCount.first; + if (results[biggestPlace]!.length > files.length / 2) { + return biggestPlace.city; + } + if (results.length > 2 && + results.keys.map((city) => city.country).toSet().length == 1) { + return biggestPlace.country; + } + return null; + } + /// Returns the best selection of files from the given list. /// Makes sure that the selection is not more than [prefferedSize] or 10 files, /// and that each year of the original list is represented.