Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

Add Wrapped places cards

laurensd088c31f4df9
diff --git a/mobile/apps/photos/lib/services/wrapped/builders/places_candidate_builder.dart b/mobile/apps/photos/lib/services/wrapped/builders/places_candidate_builder.dart
index c2c5dfb0d4..6fd14c2244 100644
--- a/mobile/apps/photos/lib/services/wrapped/builders/places_candidate_builder.dart
+++ b/mobile/apps/photos/lib/services/wrapped/builders/places_candidate_builder.dart
@@ -3,11 +3,688 @@ part of 'package:photos/services/wrapped/candidate_builders.dart';
class PlacesCandidateBuilder extends WrappedCandidateBuilder {
const PlacesCandidateBuilder();
+ static const double _kCityClusterRadiusKm = 12.0;
+ static const double _kSpotClusterRadiusKm = 0.1;
+ static const int _kMinCityCaptures = 12;
+ static const int _kMinSpotCaptures = 6;
+ static const int _kMinSpotDistinctDays = 2;
+ static const double _kMaxCityLabelDistanceKm = 120.0;
+ static const int _kTopCityMediaCount = 4;
+ static const int _kSpotMediaCount = 6;
+
@override
String get debugLabel => "places";
@override
Future<List<WrappedCard>> build(WrappedEngineContext context) async {
- return <WrappedCard>[];
+ final _PlacesDataset dataset = _PlacesDataset.fromContext(context);
+ if (!dataset.hasAnyContent) {
+ return const <WrappedCard>[];
+ }
+
+ final List<WrappedCard> cards = <WrappedCard>[];
+
+ final WrappedCard? topCities = _buildTopCitiesCard(context, dataset);
+ if (topCities != null) {
+ cards.add(topCities);
+ }
+
+ final WrappedCard? mostVisited = _buildMostVisitedSpotCard(
+ context,
+ dataset,
+ takenIds: cardsMediaIds(cards),
+ );
+ if (mostVisited != null) {
+ cards.add(mostVisited);
+ }
+
+ return cards;
+ }
+
+ WrappedCard? _buildTopCitiesCard(
+ WrappedEngineContext context,
+ _PlacesDataset dataset,
+ ) {
+ if (dataset.cityClusters.isEmpty || dataset.totalCount == 0) {
+ return null;
+ }
+ final List<_PlaceClusterSummary> topClusters =
+ dataset.cityClusters.take(3).toList(growable: false);
+ if (topClusters.isEmpty) {
+ return null;
+ }
+
+ final NumberFormat numberFormat = NumberFormat.decimalPattern();
+ final List<String> nameHighlights = <String>[];
+
+ for (final _PlaceClusterSummary cluster in topClusters) {
+ final String displayName = cluster.displayLabel;
+ nameHighlights.add(displayName);
+ }
+
+ final String title = nameHighlights.isEmpty
+ ? "Your photo map lit up"
+ : "${nameHighlights.first} headlined your ${context.year}";
+
+ final String subtitle = switch (nameHighlights.length) {
+ 0 => "You kept moving and the camera tagged along.",
+ 1 =>
+ "${nameHighlights.first} soaked up ${numberFormat.format(topClusters.first.totalCount)} memories.",
+ 2 =>
+ "${nameHighlights.first} and ${nameHighlights[1]} were your go-to cities.",
+ _ => "Top stops: ${_formatList(nameHighlights)}.",
+ };
+
+ final List<int> mediaIds = _collectMediaIds(
+ clusters: topClusters,
+ maxCount: _kTopCityMediaCount,
+ );
+ if (mediaIds.isEmpty) {
+ return null;
+ }
+
+ final Map<String, Object?> meta = <String, Object?>{
+ "year": context.year,
+ "totalGeotaggedCount": dataset.totalCount,
+ "displayDurationMillis": 6500,
+ "cities": <Map<String, Object?>>[
+ for (final _PlaceClusterSummary cluster in topClusters)
+ <String, Object?>{
+ "name": cluster.label?.city,
+ "country": cluster.label?.country,
+ "displayLabel": cluster.displayLabel,
+ "count": cluster.totalCount,
+ "distinctDays": cluster.distinctDays,
+ "centerLatitude": cluster.centerLatitude,
+ "centerLongitude": cluster.centerLongitude,
+ "sharePercent": _percentOf(
+ cluster.totalCount / dataset.totalCount.toDouble(),
+ ),
+ "distanceKm": cluster.label?.distanceKm,
+ "firstCapture": cluster.firstCaptureIsoString,
+ "lastCapture": cluster.lastCaptureIsoString,
+ },
+ ],
+ };
+
+ return WrappedCard(
+ type: WrappedCardType.topCities,
+ title: title,
+ subtitle: subtitle,
+ media: mediaIds.map(MediaRef.new).toList(growable: false),
+ meta: meta,
+ );
+ }
+
+ WrappedCard? _buildMostVisitedSpotCard(
+ WrappedEngineContext context,
+ _PlacesDataset dataset, {
+ Set<int>? takenIds,
+ }) {
+ if (dataset.spotClusters.isEmpty || dataset.totalCount == 0) {
+ return null;
+ }
+ final Iterable<_PlaceClusterSummary> eligible = dataset.spotClusters.where(
+ (_PlaceClusterSummary summary) =>
+ summary.totalCount >= _kMinSpotCaptures &&
+ summary.distinctDays >= _kMinSpotDistinctDays,
+ );
+ final _PlaceClusterSummary? cluster =
+ eligible.isEmpty ? null : eligible.first;
+ if (cluster == null) {
+ return null;
+ }
+ final NumberFormat numberFormat = NumberFormat.decimalPattern();
+
+ final List<int> mediaIds = _collectMediaIds(
+ clusters: <_PlaceClusterSummary>[cluster],
+ maxCount: _kSpotMediaCount,
+ exclude: takenIds,
+ preferDistinctDays: true,
+ );
+ if (mediaIds.isEmpty) {
+ return null;
+ }
+
+ const String title = "Your favorite spot";
+
+ final String subtitle;
+ if (cluster.distinctDays <= 0) {
+ subtitle =
+ "You made ${numberFormat.format(cluster.totalCount)} memories here.";
+ } else if (cluster.distinctDays == 1) {
+ subtitle =
+ "You made ${numberFormat.format(cluster.totalCount)} memories here in a single day.";
+ } else {
+ subtitle =
+ "You made ${numberFormat.format(cluster.totalCount)} memories here across ${numberFormat.format(cluster.distinctDays)} different days.";
+ }
+
+ final Map<String, Object?> meta = <String, Object?>{
+ "year": context.year,
+ "displayDurationMillis": 6500,
+ "spot": <String, Object?>{
+ "name": cluster.label?.city,
+ "country": cluster.label?.country,
+ "displayLabel": cluster.displayLabel,
+ "count": cluster.totalCount,
+ "distinctDays": cluster.distinctDays,
+ "centerLatitude": cluster.centerLatitude,
+ "centerLongitude": cluster.centerLongitude,
+ "sharePercent": _percentOf(
+ cluster.totalCount / dataset.totalCount.toDouble(),
+ ),
+ "distanceKm": cluster.label?.distanceKm,
+ "firstCapture": cluster.firstCaptureIsoString,
+ "lastCapture": cluster.lastCaptureIsoString,
+ },
+ };
+
+ return WrappedCard(
+ type: WrappedCardType.mostVisitedSpot,
+ title: title,
+ subtitle: subtitle,
+ media: mediaIds.map(MediaRef.new).toList(growable: false),
+ meta: meta,
+ );
+ }
+
+ Set<int> cardsMediaIds(List<WrappedCard> cards) {
+ final Set<int> ids = <int>{};
+ for (final WrappedCard card in cards) {
+ for (final MediaRef media in card.media) {
+ ids.add(media.uploadedFileID);
+ }
+ }
+ return ids;
+ }
+
+ List<int> _collectMediaIds({
+ required List<_PlaceClusterSummary> clusters,
+ required int maxCount,
+ Set<int>? exclude,
+ bool preferDistinctDays = false,
+ }) {
+ final Set<int> seen = exclude != null ? Set<int>.from(exclude) : <int>{};
+ final List<int> collected = <int>[];
+ for (final _PlaceClusterSummary cluster in clusters) {
+ final List<int> clusterIds = cluster.sampleMediaIds(
+ maxCount,
+ preferDistinctDays: preferDistinctDays,
+ );
+ for (final int id in clusterIds) {
+ if (id <= 0 || seen.contains(id)) {
+ continue;
+ }
+ collected.add(id);
+ seen.add(id);
+ if (collected.length >= maxCount) {
+ return collected;
+ }
+ }
+ }
+ return collected;
+ }
+
+ static int _percentOf(double ratio) {
+ if (!ratio.isFinite || ratio <= 0) {
+ return 0;
+ }
+ return (ratio * 100).round();
+ }
+
+ static String _formatList(List<String> values) {
+ if (values.isEmpty) {
+ return "";
+ }
+ if (values.length == 1) {
+ return values.first;
+ }
+ if (values.length == 2) {
+ return "${values.first} & ${values[1]}";
+ }
+ return "${values.take(values.length - 1).join(', ')} & ${values.last}";
+ }
+}
+
+class _PlacesDataset {
+ _PlacesDataset._({
+ required this.totalCount,
+ required List<_PlaceClusterSummary> cityClusters,
+ required List<_PlaceClusterSummary> spotClusters,
+ }) : cityClusters = List<_PlaceClusterSummary>.unmodifiable(cityClusters),
+ spotClusters = List<_PlaceClusterSummary>.unmodifiable(spotClusters);
+
+ final int totalCount;
+ final List<_PlaceClusterSummary> cityClusters;
+ final List<_PlaceClusterSummary> spotClusters;
+
+ bool get hasAnyContent => cityClusters.isNotEmpty || spotClusters.isNotEmpty;
+
+ static _PlacesDataset fromContext(WrappedEngineContext context) {
+ final List<_GeoFile> geoFiles = <_GeoFile>[];
+ for (final EnteFile file in context.files) {
+ if (!file.hasLocation) {
+ continue;
+ }
+ final Location? location = file.location;
+ if (location == null ||
+ location.latitude == null ||
+ location.longitude == null) {
+ continue;
+ }
+ final double lat = location.latitude!;
+ final double lng = location.longitude!;
+ if (!lat.isFinite || !lng.isFinite) {
+ continue;
+ }
+ final int? uploadedId = file.uploadedFileID;
+ final int? captureMicros = file.creationTime;
+ if (uploadedId == null || uploadedId <= 0) {
+ continue;
+ }
+ if (captureMicros == null || captureMicros <= 0) {
+ continue;
+ }
+ geoFiles.add(
+ _GeoFile(
+ uploadedFileID: uploadedId,
+ captureMicros: captureMicros,
+ latitude: lat,
+ longitude: lng,
+ ),
+ );
+ }
+
+ if (geoFiles.isEmpty) {
+ return _PlacesDataset._(
+ totalCount: 0,
+ cityClusters: const <_PlaceClusterSummary>[],
+ spotClusters: const <_PlaceClusterSummary>[],
+ );
+ }
+
+ final List<_PlaceClusterSummary> cityClusters = _summariesForRadius(
+ geoFiles: geoFiles,
+ radiusKm: PlacesCandidateBuilder._kCityClusterRadiusKm,
+ cities: context.cities,
+ )
+ .where(
+ (_PlaceClusterSummary summary) =>
+ summary.totalCount >= PlacesCandidateBuilder._kMinCityCaptures,
+ )
+ .toList(growable: false)
+ ..sort(
+ (_PlaceClusterSummary a, _PlaceClusterSummary b) =>
+ b.totalCount.compareTo(a.totalCount),
+ );
+
+ final List<_PlaceClusterSummary> spotClusters = _summariesForRadius(
+ geoFiles: geoFiles,
+ radiusKm: PlacesCandidateBuilder._kSpotClusterRadiusKm,
+ cities: context.cities,
+ )
+ .where(
+ (_PlaceClusterSummary summary) =>
+ summary.totalCount >= PlacesCandidateBuilder._kMinSpotCaptures,
+ )
+ .toList(growable: false)
+ ..sort(
+ (_PlaceClusterSummary a, _PlaceClusterSummary b) {
+ final int countCompare = b.totalCount.compareTo(a.totalCount);
+ if (countCompare != 0) {
+ return countCompare;
+ }
+ return b.distinctDays.compareTo(a.distinctDays);
+ },
+ );
+
+ return _PlacesDataset._(
+ totalCount: geoFiles.length,
+ cityClusters: cityClusters,
+ spotClusters: spotClusters,
+ );
+ }
+
+ static List<_PlaceClusterSummary> _summariesForRadius({
+ required List<_GeoFile> geoFiles,
+ required double radiusKm,
+ required List<WrappedCity> cities,
+ }) {
+ final List<_PlaceCluster> clusters = _clusterGeoFiles(geoFiles, radiusKm);
+ return clusters.map(
+ (_PlaceCluster cluster) {
+ final _ClusterLabel? label = _resolveLabel(
+ cluster.centerLatitude,
+ cluster.centerLongitude,
+ cities,
+ );
+ return _PlaceClusterSummary.fromCluster(
+ cluster: cluster,
+ label: label,
+ );
+ },
+ ).toList(growable: false);
+ }
+
+ static _ClusterLabel? _resolveLabel(
+ double lat,
+ double lng,
+ List<WrappedCity> cities,
+ ) {
+ if (cities.isEmpty) {
+ return null;
+ }
+ WrappedCity? closest;
+ double bestDistance = PlacesCandidateBuilder._kMaxCityLabelDistanceKm;
+ for (final WrappedCity city in cities) {
+ if (city.latitude == 0 && city.longitude == 0) {
+ continue;
+ }
+ final double distance =
+ _distanceKm(lat, lng, city.latitude, city.longitude);
+ if (distance < bestDistance) {
+ bestDistance = distance;
+ closest = city;
+ }
+ }
+ if (closest == null) {
+ return null;
+ }
+ return _ClusterLabel(
+ city: closest.name,
+ country: closest.country,
+ distanceKm: bestDistance,
+ );
}
}
+
+class _PlaceClusterSummary {
+ _PlaceClusterSummary._({
+ required List<_GeoFile> files,
+ required this.centerLatitude,
+ required this.centerLongitude,
+ required this.distinctDays,
+ required this.firstCaptureMicros,
+ required this.lastCaptureMicros,
+ this.label,
+ }) : files = List<_GeoFile>.unmodifiable(files);
+
+ final List<_GeoFile> files;
+ final double centerLatitude;
+ final double centerLongitude;
+ final int distinctDays;
+ final int firstCaptureMicros;
+ final int lastCaptureMicros;
+ final _ClusterLabel? label;
+
+ int get totalCount => files.length;
+
+ DateTime? get firstCapture => firstCaptureMicros <= 0
+ ? null
+ : DateTime.fromMicrosecondsSinceEpoch(firstCaptureMicros);
+ DateTime? get lastCapture => lastCaptureMicros <= 0
+ ? null
+ : DateTime.fromMicrosecondsSinceEpoch(lastCaptureMicros);
+
+ String? get firstCaptureIsoString => firstCapture?.toIso8601String();
+ String? get lastCaptureIsoString => lastCapture?.toIso8601String();
+
+ String get displayLabel {
+ if (label != null) {
+ final String? name = label!.city.isNotEmpty
+ ? label!.city
+ : (label!.country.isNotEmpty ? label!.country : null);
+ if (name != null && name.trim().isNotEmpty) {
+ return name;
+ }
+ }
+ return _formatCoordinates(centerLatitude, centerLongitude);
+ }
+
+ List<int> sampleMediaIds(
+ int maxCount, {
+ bool preferDistinctDays = false,
+ }) {
+ if (files.isEmpty) {
+ return const <int>[];
+ }
+ final List<_GeoFile> sorted = files.toList(growable: false)
+ ..sort(
+ (_GeoFile a, _GeoFile b) => a.captureMicros.compareTo(b.captureMicros),
+ );
+ final List<_GeoFile> unique = <_GeoFile>[];
+ final Set<int> seenIds = <int>{};
+ for (final _GeoFile file in sorted) {
+ if (seenIds.add(file.uploadedFileID)) {
+ unique.add(file);
+ }
+ }
+ if (unique.isEmpty) {
+ return const <int>[];
+ }
+
+ final List<_GeoFile> samplingSource = preferDistinctDays
+ ? _preferDistinctDaySource(unique, maxCount)
+ : unique;
+
+ if (samplingSource.isEmpty) {
+ return const <int>[];
+ }
+ if (samplingSource.length <= maxCount) {
+ return samplingSource
+ .map((_GeoFile file) => file.uploadedFileID)
+ .toList(growable: false);
+ }
+
+ final Set<int> usedIndices = <int>{};
+ final List<int> selected = <int>[];
+ for (int i = 0; i < maxCount; i++) {
+ final int denominator = math.max(maxCount - 1, 1);
+ final double ratio = maxCount == 1 ? 0 : i / denominator;
+ int index = (ratio * (samplingSource.length - 1)).round();
+ index = _findClosestAvailableIndex(
+ index: index,
+ length: samplingSource.length,
+ used: usedIndices,
+ );
+ usedIndices.add(index);
+ selected.add(samplingSource[index].uploadedFileID);
+ }
+ return selected;
+ }
+
+ List<_GeoFile> _preferDistinctDaySource(
+ List<_GeoFile> unique,
+ int maxCount,
+ ) {
+ final Map<int, _GeoFile> firstPerDay = <int, _GeoFile>{};
+ for (final _GeoFile file in unique) {
+ final DateTime timestamp =
+ DateTime.fromMicrosecondsSinceEpoch(file.captureMicros);
+ final DateTime day =
+ DateTime(timestamp.year, timestamp.month, timestamp.day);
+ final int dayKey = day.millisecondsSinceEpoch;
+ firstPerDay.putIfAbsent(dayKey, () => file);
+ }
+ final List<MapEntry<int, _GeoFile>> dayEntries =
+ firstPerDay.entries.toList()
+ ..sort(
+ (MapEntry<int, _GeoFile> a, MapEntry<int, _GeoFile> b) =>
+ a.key.compareTo(b.key),
+ );
+ final List<_GeoFile> distinctDayFiles =
+ dayEntries.map((MapEntry<int, _GeoFile> entry) => entry.value).toList();
+ if (distinctDayFiles.length >= maxCount) {
+ return distinctDayFiles;
+ }
+ final Set<int> seenIds =
+ distinctDayFiles.map((_GeoFile file) => file.uploadedFileID).toSet();
+ final List<_GeoFile> fallback = <_GeoFile>[
+ for (final _GeoFile file in unique)
+ if (!seenIds.contains(file.uploadedFileID)) file,
+ ];
+ if (fallback.isEmpty) {
+ return distinctDayFiles;
+ }
+ return <_GeoFile>[...distinctDayFiles, ...fallback];
+ }
+
+ static int _findClosestAvailableIndex({
+ required int index,
+ required int length,
+ required Set<int> used,
+ }) {
+ if (!used.contains(index)) {
+ return index;
+ }
+ for (int offset = 1; offset < length; offset++) {
+ final int before = index - offset;
+ if (before >= 0 && !used.contains(before)) {
+ return before;
+ }
+ final int after = index + offset;
+ if (after < length && !used.contains(after)) {
+ return after;
+ }
+ }
+ return index.clamp(0, length - 1);
+ }
+
+ static _PlaceClusterSummary fromCluster({
+ required _PlaceCluster cluster,
+ _ClusterLabel? label,
+ }) {
+ final List<_GeoFile> sorted = cluster.files.toList(growable: false)
+ ..sort(
+ (_GeoFile a, _GeoFile b) => a.captureMicros.compareTo(b.captureMicros),
+ );
+ final Set<int> dayKeys = <int>{};
+ for (final _GeoFile file in sorted) {
+ final DateTime timestamp =
+ DateTime.fromMicrosecondsSinceEpoch(file.captureMicros);
+ final DateTime day =
+ DateTime(timestamp.year, timestamp.month, timestamp.day);
+ dayKeys.add(day.millisecondsSinceEpoch);
+ }
+ final int firstMicros = sorted.first.captureMicros;
+ final int lastMicros = sorted.last.captureMicros;
+
+ return _PlaceClusterSummary._(
+ files: sorted,
+ centerLatitude: cluster.centerLatitude,
+ centerLongitude: cluster.centerLongitude,
+ distinctDays: dayKeys.length,
+ firstCaptureMicros: firstMicros,
+ lastCaptureMicros: lastMicros,
+ label: label,
+ );
+ }
+}
+
+class _PlaceCluster {
+ _PlaceCluster(_GeoFile initial)
+ : files = <_GeoFile>[initial],
+ _sumLat = initial.latitude,
+ _sumLng = initial.longitude;
+
+ final List<_GeoFile> files;
+ double _sumLat;
+ double _sumLng;
+
+ double get centerLatitude => _sumLat / files.length;
+ double get centerLongitude => _sumLng / files.length;
+
+ void add(_GeoFile file) {
+ files.add(file);
+ _sumLat += file.latitude;
+ _sumLng += file.longitude;
+ }
+}
+
+class _GeoFile {
+ const _GeoFile({
+ required this.uploadedFileID,
+ required this.captureMicros,
+ required this.latitude,
+ required this.longitude,
+ });
+
+ final int uploadedFileID;
+ final int captureMicros;
+ final double latitude;
+ final double longitude;
+}
+
+class _ClusterLabel {
+ const _ClusterLabel({
+ required this.city,
+ required this.country,
+ required this.distanceKm,
+ });
+
+ final String city;
+ final String country;
+ final double distanceKm;
+}
+
+List<_PlaceCluster> _clusterGeoFiles(
+ List<_GeoFile> files,
+ double radiusKm,
+) {
+ if (files.isEmpty) {
+ return const <_PlaceCluster>[];
+ }
+ final List<_PlaceCluster> clusters = <_PlaceCluster>[];
+ for (final _GeoFile file in files) {
+ _PlaceCluster? bestCluster;
+ double bestDistance = radiusKm;
+ for (final _PlaceCluster cluster in clusters) {
+ final double distance = _distanceKm(
+ file.latitude,
+ file.longitude,
+ cluster.centerLatitude,
+ cluster.centerLongitude,
+ );
+ if (distance <= radiusKm && distance < bestDistance) {
+ bestDistance = distance;
+ bestCluster = cluster;
+ }
+ }
+ if (bestCluster == null) {
+ clusters.add(_PlaceCluster(file));
+ } else {
+ bestCluster.add(file);
+ }
+ }
+ return clusters;
+}
+
+double _distanceKm(
+ double lat1,
+ double lon1,
+ double lat2,
+ double lon2,
+) {
+ const double earthRadiusKm = 6371.0;
+ final double dLat = _toRadians(lat2 - lat1);
+ final double dLon = _toRadians(lon2 - lon1);
+ final double a = math.sin(dLat / 2) * math.sin(dLat / 2) +
+ math.cos(_toRadians(lat1)) *
+ math.cos(_toRadians(lat2)) *
+ math.sin(dLon / 2) *
+ math.sin(dLon / 2);
+ final double c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a));
+ return earthRadiusKm * c;
+}
+
+double _toRadians(double degrees) {
+ return degrees * math.pi / 180;
+}
+
+String _formatCoordinates(double latitude, double longitude) {
+ final String latHemisphere = latitude >= 0 ? "N" : "S";
+ final String lonHemisphere = longitude >= 0 ? "E" : "W";
+ return "${latitude.abs().toStringAsFixed(2)}°$latHemisphere · "
+ "${longitude.abs().toStringAsFixed(2)}°$lonHemisphere";
+}
diff --git a/mobile/apps/photos/lib/services/wrapped/candidate_builders.dart b/mobile/apps/photos/lib/services/wrapped/candidate_builders.dart
index f56e367198..707ff9a110 100644
--- a/mobile/apps/photos/lib/services/wrapped/candidate_builders.dart
+++ b/mobile/apps/photos/lib/services/wrapped/candidate_builders.dart
@@ -5,6 +5,7 @@ import "package:flutter/foundation.dart" show immutable;
import "package:intl/intl.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/file/file_type.dart";
+import "package:photos/models/location/location.dart";
import "package:photos/services/machine_learning/face_ml/face_filtering/face_filtering_constants.dart";
import "package:photos/services/wrapped/models.dart";
import "package:photos/utils/standalone/data.dart";
@@ -25,15 +26,20 @@ class WrappedEngineContext {
required List<EnteFile> files,
WrappedPeopleContext? people,
WrappedAestheticsContext? aesthetics,
+ List<WrappedCity>? cities,
}) : files = List<EnteFile>.unmodifiable(files),
people = people ?? WrappedPeopleContext.empty(),
- aesthetics = aesthetics ?? WrappedAestheticsContext.empty();
+ aesthetics = aesthetics ?? WrappedAestheticsContext.empty(),
+ cities = List<WrappedCity>.unmodifiable(
+ cities ?? const <WrappedCity>[],
+ );
final int year;
final DateTime now;
final List<EnteFile> files;
final WrappedPeopleContext people;
final WrappedAestheticsContext aesthetics;
+ final List<WrappedCity> cities;
}
/// Contract for producing Wrapped candidate cards for a specific domain.
diff --git a/mobile/apps/photos/lib/services/wrapped/models.dart b/mobile/apps/photos/lib/services/wrapped/models.dart
index e46bded073..01755503db 100644
--- a/mobile/apps/photos/lib/services/wrapped/models.dart
+++ b/mobile/apps/photos/lib/services/wrapped/models.dart
@@ -75,6 +75,39 @@ class WrappedPeopleContext {
}
}
+@immutable
+class WrappedCity {
+ const WrappedCity({
+ required this.name,
+ required this.country,
+ required this.latitude,
+ required this.longitude,
+ });
+
+ final String name;
+ final String country;
+ final double latitude;
+ final double longitude;
+
+ Map<String, Object?> toJson() {
+ return <String, Object?>{
+ "name": name,
+ "country": country,
+ "latitude": latitude,
+ "longitude": longitude,
+ };
+ }
+
+ static WrappedCity fromJson(Map<String, Object?> json) {
+ return WrappedCity(
+ name: json["name"] as String? ?? "",
+ country: json["country"] as String? ?? "",
+ latitude: (json["latitude"] as num?)?.toDouble() ?? 0.0,
+ longitude: (json["longitude"] as num?)?.toDouble() ?? 0.0,
+ );
+ }
+}
+
@immutable
class WrappedAestheticsContext {
WrappedAestheticsContext({
diff --git a/mobile/apps/photos/lib/services/wrapped/wrapped_engine.dart b/mobile/apps/photos/lib/services/wrapped/wrapped_engine.dart
index d8cec44a8a..c9a54c1ef0 100644
--- a/mobile/apps/photos/lib/services/wrapped/wrapped_engine.dart
+++ b/mobile/apps/photos/lib/services/wrapped/wrapped_engine.dart
@@ -6,6 +6,8 @@ import "package:photos/models/file/file.dart";
import "package:photos/models/ml/face/face_with_embedding.dart";
import "package:photos/models/ml/face/person.dart";
import "package:photos/models/ml/vector.dart";
+import "package:photos/service_locator.dart";
+import "package:photos/services/location_service.dart";
import "package:photos/services/machine_learning/face_ml/person/person_service.dart";
import "package:photos/services/machine_learning/ml_result.dart";
import "package:photos/services/search_service.dart";
@@ -40,6 +42,25 @@ class WrappedEngine {
year: year,
yearFiles: collected.yearFiles,
);
+ List<WrappedCity> cities = const <WrappedCity>[];
+ try {
+ final List<City> loadedCities = await locationService.getCities();
+ cities = <WrappedCity>[
+ for (final City city in loadedCities)
+ WrappedCity(
+ name: city.city,
+ country: city.country,
+ latitude: city.lat,
+ longitude: city.lng,
+ ),
+ ];
+ } catch (error, stackTrace) {
+ _engineLogger.warning(
+ "Failed to load cities for Wrapped $year places context",
+ error,
+ stackTrace,
+ );
+ }
return await Computer.shared().compute(
_wrappedComputeIsolate,
@@ -49,6 +70,7 @@ class WrappedEngine {
"files": collected.yearFiles,
"people": peopleContext.toJson(),
"aesthetics": aestheticsContext.toJson(),
+ "cities": cities.map((WrappedCity city) => city.toJson()).toList(),
},
taskName: "wrapped_compute_$year",
) as WrappedResult;
@@ -317,6 +339,14 @@ Future<WrappedResult> _wrappedComputeIsolate(
<String, Object?>{};
final WrappedAestheticsContext aesthetics =
WrappedAestheticsContext.fromJson(aestheticsRaw);
+ final List<dynamic> rawCities =
+ args["cities"] as List<dynamic>? ?? const <dynamic>[];
+ final List<WrappedCity> cities = rawCities
+ .map(
+ (dynamic entry) =>
+ WrappedCity.fromJson((entry as Map).cast<String, Object?>()),
+ )
+ .toList(growable: false);
_computeLogger.fine(
"Wrapped compute isolate running for $year with ${files.length} media items",
@@ -328,6 +358,7 @@ Future<WrappedResult> _wrappedComputeIsolate(
files: files,
people: people,
aesthetics: aesthetics,
+ cities: cities,
);
final List<WrappedCard> cards = <WrappedCard>[];
for (final WrappedCandidateBuilder builder in wrappedCandidateBuilders) {
diff --git a/mobile/apps/photos/lib/ui/wrapped/cards/places_card_content.dart b/mobile/apps/photos/lib/ui/wrapped/cards/places_card_content.dart
index e0e5e64b25..e912790322 100644
--- a/mobile/apps/photos/lib/ui/wrapped/cards/places_card_content.dart
+++ b/mobile/apps/photos/lib/ui/wrapped/cards/places_card_content.dart
@@ -5,5 +5,304 @@ Widget? buildPlacesCardContent(
EnteColorScheme colorScheme,
EnteTextTheme textTheme,
) {
- return null;
+ switch (card.type) {
+ case WrappedCardType.topCities:
+ return _TopCitiesCardContent(
+ card: card,
+ colorScheme: colorScheme,
+ textTheme: textTheme,
+ );
+ case WrappedCardType.mostVisitedSpot:
+ return _MostVisitedSpotCardContent(
+ card: card,
+ colorScheme: colorScheme,
+ textTheme: textTheme,
+ );
+ default:
+ return null;
+ }
+}
+
+class _TopCitiesCardContent extends StatelessWidget {
+ const _TopCitiesCardContent({
+ required this.card,
+ required this.colorScheme,
+ required this.textTheme,
+ });
+
+ final WrappedCard card;
+ final EnteColorScheme colorScheme;
+ final EnteTextTheme textTheme;
+
+ @override
+ Widget build(BuildContext context) {
+ final List<_CityShareStat> stats = _cityShareStatsFromMeta(card.meta);
+ return Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ _HeroMediaCollage(
+ media: card.media,
+ colorScheme: colorScheme,
+ ),
+ const SizedBox(height: 24),
+ Text(
+ card.title,
+ style: textTheme.h2Bold,
+ ),
+ if (card.subtitle != null && card.subtitle!.isNotEmpty)
+ Padding(
+ padding: const EdgeInsets.only(top: 12),
+ child: Text(
+ card.subtitle!,
+ style: textTheme.bodyMuted,
+ ),
+ ),
+ if (stats.isNotEmpty) ...[
+ const SizedBox(height: 22),
+ _CityShareList(
+ stats: stats,
+ colorScheme: colorScheme,
+ textTheme: textTheme,
+ ),
+ ],
+ const Spacer(),
+ ],
+ );
+ }
+}
+
+class _CityShareList extends StatelessWidget {
+ const _CityShareList({
+ required this.stats,
+ required this.colorScheme,
+ required this.textTheme,
+ });
+
+ final List<_CityShareStat> stats;
+ final EnteColorScheme colorScheme;
+ final EnteTextTheme textTheme;
+
+ @override
+ Widget build(BuildContext context) {
+ return Column(
+ children: [
+ for (final (int index, _CityShareStat stat) in stats.indexed)
+ Padding(
+ padding: EdgeInsets.only(top: index == 0 ? 0 : 16),
+ child: _CityShareRow(
+ stat: stat,
+ colorScheme: colorScheme,
+ textTheme: textTheme,
+ ),
+ ),
+ ],
+ );
+ }
+}
+
+class _CityShareRow extends StatelessWidget {
+ const _CityShareRow({
+ required this.stat,
+ required this.colorScheme,
+ required this.textTheme,
+ });
+
+ final _CityShareStat stat;
+ final EnteColorScheme colorScheme;
+ final EnteTextTheme textTheme;
+
+ @override
+ Widget build(BuildContext context) {
+ final double progress = (stat.sharePercent.clamp(0, 100)) / 100.0;
+ final String countLabel =
+ "${stat.count} ${stat.count == 1 ? 'shot' : 'shots'}";
+ final String detailsLabel = stat.distinctDays <= 1
+ ? countLabel
+ : "$countLabel · ${stat.distinctDays} days";
+
+ return Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Row(
+ children: [
+ Expanded(
+ child: Text(
+ stat.label,
+ style: textTheme.bodyBold,
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ const SizedBox(width: 12),
+ Text(
+ "${stat.sharePercent}%",
+ style: textTheme.smallMuted,
+ ),
+ ],
+ ),
+ const SizedBox(height: 6),
+ ClipRRect(
+ borderRadius: BorderRadius.circular(6),
+ child: Stack(
+ children: [
+ Container(
+ height: 6,
+ color: colorScheme.fillFaint,
+ ),
+ FractionallySizedBox(
+ widthFactor: progress,
+ child: Container(
+ height: 6,
+ color: colorScheme.primary500,
+ ),
+ ),
+ ],
+ ),
+ ),
+ const SizedBox(height: 6),
+ Text(
+ detailsLabel,
+ style: textTheme.smallMuted,
+ ),
+ ],
+ );
+ }
+}
+
+class _CityShareStat {
+ _CityShareStat({
+ required this.label,
+ required this.count,
+ required this.sharePercent,
+ required this.distinctDays,
+ });
+
+ final String label;
+ final int count;
+ final int sharePercent;
+ final int distinctDays;
+}
+
+List<_CityShareStat> _cityShareStatsFromMeta(Map<String, Object?> meta) {
+ final Object? raw = meta["cities"];
+ if (raw is! List) {
+ return const <_CityShareStat>[];
+ }
+
+ final List<_CityShareStat> stats = <_CityShareStat>[];
+ for (final Object? entry in raw) {
+ if (entry is! Map) {
+ continue;
+ }
+ final Map<String, Object?> map = entry.cast<String, Object?>();
+ final String? displayLabel = (map["displayLabel"] as String?)?.trim();
+ final String? name = (map["name"] as String?)?.trim();
+ final String? country = (map["country"] as String?)?.trim();
+ final String? label = displayLabel?.isNotEmpty == true
+ ? displayLabel
+ : (name?.isNotEmpty == true ? name : country);
+ final int count = (map["count"] as num?)?.toInt() ?? 0;
+ final int sharePercent = (map["sharePercent"] as num?)?.round() ?? 0;
+ final int distinctDays = (map["distinctDays"] as num?)?.toInt() ?? 0;
+ if (label == null || label.isEmpty || count <= 0) {
+ continue;
+ }
+ stats.add(
+ _CityShareStat(
+ label: label,
+ count: count,
+ sharePercent: sharePercent.clamp(0, 100),
+ distinctDays: distinctDays,
+ ),
+ );
+ }
+ return stats;
+}
+
+class _MostVisitedSpotCardContent extends StatelessWidget {
+ const _MostVisitedSpotCardContent({
+ required this.card,
+ required this.colorScheme,
+ required this.textTheme,
+ });
+
+ final WrappedCard card;
+ final EnteColorScheme colorScheme;
+ final EnteTextTheme textTheme;
+
+ @override
+ Widget build(BuildContext context) {
+ final List<MediaRef> extraMedia =
+ card.media.skip(3).toList(growable: false);
+ final List<MediaRef> secondRowMedia =
+ extraMedia.take(3).toList(growable: false);
+ return Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ _HeroMediaCollage(
+ media: card.media,
+ colorScheme: colorScheme,
+ ),
+ if (secondRowMedia.isNotEmpty) ...[
+ const SizedBox(height: 12),
+ SizedBox(
+ height: 220,
+ child: Row(
+ children: [
+ Expanded(
+ child: Column(
+ children: List<Widget>.generate(2, (int index) {
+ final MediaRef? ref = index < secondRowMedia.length
+ ? secondRowMedia[index]
+ : null;
+ return Expanded(
+ child: Padding(
+ padding: EdgeInsets.only(
+ bottom: index == 0 ? 12 : 0,
+ ),
+ child: ref != null
+ ? _MediaTile(
+ mediaRef: ref,
+ borderRadius: 20,
+ )
+ : _MediaPlaceholder(
+ colorScheme: colorScheme,
+ borderRadius: 20,
+ ),
+ ),
+ );
+ }),
+ ),
+ ),
+ if (secondRowMedia.length == 3) ...[
+ const SizedBox(width: 12),
+ Expanded(
+ flex: 2,
+ child: _MediaTile(
+ mediaRef: secondRowMedia[2],
+ borderRadius: 24,
+ aspectRatio: 3 / 4,
+ ),
+ ),
+ ],
+ ],
+ ),
+ ),
+ ],
+ const SizedBox(height: 24),
+ Text(
+ card.title,
+ style: textTheme.h2Bold,
+ ),
+ if (card.subtitle != null && card.subtitle!.isNotEmpty)
+ Padding(
+ padding: const EdgeInsets.only(top: 12),
+ child: Text(
+ card.subtitle!,
+ style: textTheme.bodyMuted,
+ ),
+ ),
+ const SizedBox(height: 12),
+ ],
+ );
+ }
}