Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Favorites card

laurenspriem7712054adad0
diff --git a/mobile/apps/photos/lib/services/wrapped/builders/curation_candidate_builder.dart b/mobile/apps/photos/lib/services/wrapped/builders/curation_candidate_builder.dart
index 0dc8623b06..c1c39ad7dc 100644
--- a/mobile/apps/photos/lib/services/wrapped/builders/curation_candidate_builder.dart
+++ b/mobile/apps/photos/lib/services/wrapped/builders/curation_candidate_builder.dart
@@ -3,11 +3,137 @@ part of 'package:photos/services/wrapped/candidate_builders.dart';
class CurationCandidateBuilder extends WrappedCandidateBuilder {
const CurationCandidateBuilder();
+ static const int _kMinFavoritesForCard = 6;
+ static const int _kGalleryGroupSize = 6;
+ static const int _kMaxGalleryGroups = 3;
+ static const int _kRotationMillis = 2400;
+
@override
String get debugLabel => "curation";
@override
Future<List<WrappedCard>> build(WrappedEngineContext context) async {
- return <WrappedCard>[];
+ final Set<int> favoriteIds = context.favoriteUploadedFileIDs;
+ if (favoriteIds.length < _kMinFavoritesForCard) {
+ return const <WrappedCard>[];
+ }
+
+ final List<EnteFile> favoriteFiles = context.files
+ .where(
+ (EnteFile file) =>
+ file.uploadedFileID != null &&
+ favoriteIds.contains(file.uploadedFileID),
+ )
+ .toList(growable: false);
+ if (favoriteFiles.length < _kMinFavoritesForCard) {
+ return const <WrappedCard>[];
+ }
+
+ final math.Random randomizer = math.Random(
+ context.year * 53 + favoriteFiles.length,
+ );
+ final List<List<int>> galleryGroups =
+ _buildGalleryGroups(favoriteFiles, randomizer);
+ if (galleryGroups.isEmpty) {
+ return const <WrappedCard>[];
+ }
+
+ final List<MediaRef> heroMedia =
+ galleryGroups.first.map(MediaRef.new).toList(growable: false);
+
+ final NumberFormat numberFormat = NumberFormat.decimalPattern();
+ final int favoritesCount = favoriteFiles.length;
+ final int totalCount = context.files.length;
+ final int sharePercent =
+ totalCount <= 0 ? 0 : _percentOf(favoritesCount / totalCount);
+
+ final List<String> chips = _cleanChips(<String>[
+ "${numberFormat.format(favoritesCount)} favorites saved",
+ if (sharePercent > 0) "$sharePercent% of your year",
+ ]);
+
+ final Map<String, Object?> meta = <String, Object?>{
+ "favoriteCount": favoritesCount,
+ "totalCount": totalCount,
+ "sharePercent": sharePercent,
+ "detailChips": chips,
+ "galleryGroups": galleryGroups
+ .map((List<int> group) => group.toList(growable: false))
+ .toList(growable: false),
+ "rotationMillis": _kRotationMillis,
+ "displayDurationMillis": 9000,
+ };
+
+ final String subtitle = favoritesCount == 1
+ ? "One standout you couldn't resist starring."
+ : "${numberFormat.format(favoritesCount)} handpicked keeps.";
+
+ final WrappedCard favoritesCard = WrappedCard(
+ type: WrappedCardType.favorites,
+ title: "Your favorites of ${context.year}",
+ subtitle: subtitle,
+ media: heroMedia,
+ meta: meta,
+ );
+
+ return <WrappedCard>[favoritesCard];
+ }
+
+ List<List<int>> _buildGalleryGroups(
+ List<EnteFile> favoriteFiles,
+ math.Random randomizer,
+ ) {
+ final List<int> uploadedIds = favoriteFiles
+ .map((EnteFile file) => file.uploadedFileID)
+ .whereType<int>()
+ .toList(growable: false);
+ if (uploadedIds.length < _kMinFavoritesForCard) {
+ return const <List<int>>[];
+ }
+
+ final List<int> indices =
+ List<int>.generate(uploadedIds.length, (int index) => index);
+ _shuffle(indices, randomizer);
+
+ final int maxUsable = math.min(
+ uploadedIds.length,
+ _kGalleryGroupSize * _kMaxGalleryGroups,
+ );
+ final int groupCount = maxUsable ~/ _kGalleryGroupSize;
+ final List<List<int>> groups = <List<int>>[];
+
+ if (groupCount == 0) {
+ final List<int> fallback = indices
+ .take(_kGalleryGroupSize)
+ .map((int index) => uploadedIds[index])
+ .toList(growable: false);
+ if (fallback.length >= _kMinFavoritesForCard) {
+ groups.add(fallback);
+ }
+ return groups;
+ }
+
+ int cursor = 0;
+ for (int i = 0; i < groupCount; i += 1) {
+ final List<int> group = <int>[];
+ for (int j = 0; j < _kGalleryGroupSize; j += 1) {
+ final int index = indices[cursor++];
+ group.add(uploadedIds[index]);
+ }
+ groups.add(group);
+ }
+
+ return groups
+ .map((List<int> group) => group.toList(growable: false))
+ .toList(growable: false);
+ }
+
+ void _shuffle<T>(List<T> list, math.Random randomizer) {
+ for (int i = list.length - 1; i > 0; i -= 1) {
+ final int j = randomizer.nextInt(i + 1);
+ final T temp = list[i];
+ list[i] = list[j];
+ list[j] = temp;
+ }
}
}
diff --git a/mobile/apps/photos/lib/ui/wrapped/cards/curation_card_content.dart b/mobile/apps/photos/lib/ui/wrapped/cards/curation_card_content.dart
index 2d5d7d48a4..a301d08f98 100644
--- a/mobile/apps/photos/lib/ui/wrapped/cards/curation_card_content.dart
+++ b/mobile/apps/photos/lib/ui/wrapped/cards/curation_card_content.dart
@@ -5,5 +5,165 @@ Widget? buildCurationCardContent(
EnteColorScheme colorScheme,
EnteTextTheme textTheme,
) {
- return null;
+ switch (card.type) {
+ case WrappedCardType.favorites:
+ return _FavoritesCardContent(
+ card: card,
+ colorScheme: colorScheme,
+ textTheme: textTheme,
+ );
+ default:
+ return null;
+ }
+}
+
+class _FavoritesCardContent extends StatefulWidget {
+ const _FavoritesCardContent({
+ required this.card,
+ required this.colorScheme,
+ required this.textTheme,
+ });
+
+ final WrappedCard card;
+ final EnteColorScheme colorScheme;
+ final EnteTextTheme textTheme;
+
+ @override
+ State<_FavoritesCardContent> createState() => _FavoritesCardContentState();
+}
+
+class _FavoritesCardContentState extends State<_FavoritesCardContent> {
+ static const int _kDefaultRotationMillis = 2400;
+
+ late List<List<MediaRef>> _groups;
+ int _groupIndex = 0;
+ int _rotationMillis = _kDefaultRotationMillis;
+ Timer? _rotationTimer;
+
+ @override
+ void initState() {
+ super.initState();
+ _configureFromCard();
+ }
+
+ @override
+ void didUpdateWidget(covariant _FavoritesCardContent oldWidget) {
+ super.didUpdateWidget(oldWidget);
+ if (oldWidget.card != widget.card) {
+ _configureFromCard();
+ }
+ }
+
+ @override
+ void dispose() {
+ _rotationTimer?.cancel();
+ super.dispose();
+ }
+
+ void _configureFromCard() {
+ _rotationTimer?.cancel();
+ _groups = _extractGroups(widget.card);
+ _groupIndex = 0;
+
+ final Object? rawRotation = widget.card.meta["rotationMillis"];
+ final int? rotation = rawRotation is num ? rawRotation.toInt() : null;
+ _rotationMillis =
+ ((rotation ?? _kDefaultRotationMillis).clamp(1200, 6000)).toInt();
+
+ if (_groups.length > 1) {
+ _rotationTimer = Timer.periodic(
+ Duration(milliseconds: _rotationMillis),
+ (_) => _advanceGallery(),
+ );
+ }
+ }
+
+ void _advanceGallery() {
+ if (!mounted || _groups.isEmpty) {
+ return;
+ }
+ setState(() {
+ _groupIndex = (_groupIndex + 1) % _groups.length;
+ });
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final List<String> detailChips =
+ _stringListFromMeta(widget.card.meta, "detailChips");
+ final List<MediaRef> currentMedia = _groups.isEmpty
+ ? const <MediaRef>[]
+ : _groups[_groupIndex.clamp(0, _groups.length - 1)];
+
+ return Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(
+ widget.card.title,
+ style: widget.textTheme.h2Bold,
+ ),
+ if (widget.card.subtitle != null && widget.card.subtitle!.isNotEmpty)
+ Padding(
+ padding: const EdgeInsets.only(top: 12),
+ child: Text(
+ widget.card.subtitle!,
+ style: widget.textTheme.bodyMuted,
+ ),
+ ),
+ if (detailChips.isNotEmpty) ...[
+ const SizedBox(height: 18),
+ _DetailChips(
+ chips: detailChips,
+ colorScheme: widget.colorScheme,
+ textTheme: widget.textTheme,
+ ),
+ ],
+ const SizedBox(height: 22),
+ AnimatedSwitcher(
+ duration: const Duration(milliseconds: 420),
+ switchInCurve: Curves.easeOutCubic,
+ switchOutCurve: Curves.easeInCubic,
+ child: KeyedSubtree(
+ key: ValueKey<int>(_groupIndex),
+ child: _MediaGrid(
+ media: currentMedia,
+ colorScheme: widget.colorScheme,
+ ),
+ ),
+ ),
+ const Spacer(),
+ ],
+ );
+ }
+
+ List<List<MediaRef>> _extractGroups(WrappedCard card) {
+ final Object? rawGroups = card.meta["galleryGroups"];
+ if (rawGroups is List) {
+ final List<List<MediaRef>> parsed = rawGroups
+ .whereType<List>()
+ .map(
+ (List<dynamic> group) => group
+ .whereType<num>()
+ .map((num value) => value.toInt())
+ .where((int id) => id > 0)
+ .map(MediaRef.new)
+ .toList(growable: false),
+ )
+ .where((List<MediaRef> group) => group.isNotEmpty)
+ .toList(growable: false);
+ if (parsed.isNotEmpty) {
+ return parsed;
+ }
+ }
+
+ if (card.media.isNotEmpty) {
+ return <List<MediaRef>>[
+ card.media.toList(growable: false),
+ ];
+ }
+
+ return const <List<MediaRef>>[
+ <MediaRef>[],
+ ];
+ }
}