Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] cleanup memories strip to support various types of cards

Priyanshu Dangare79753881411b
diff --git a/mobile/apps/photos/lib/ui/home/memories/crafting_memories_card.dart b/mobile/apps/photos/lib/ui/home/memories/crafting_memories_card.dart
index 5df8d1ce91..3b0dfc73d6 100644
--- a/mobile/apps/photos/lib/ui/home/memories/crafting_memories_card.dart
+++ b/mobile/apps/photos/lib/ui/home/memories/crafting_memories_card.dart
@@ -5,44 +5,59 @@ import "package:hugeicons/hugeicons.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/notification_service.dart";
import "package:photos/ui/home/memories/horts.dart";
-import "package:photos/ui/home/memories/memory_card.dart";
+import "package:photos/ui/home/memories/memory_card_constants.dart";
class CraftingMemoriesCardWidget extends StatelessWidget {
final double width;
final double height;
- final VoidCallback? onNotificationsPermissionGranted;
+ final Function(bool)? onShouldShowChanged;
const CraftingMemoriesCardWidget({
super.key,
required this.width,
required this.height,
- this.onNotificationsPermissionGranted,
+ this.onShouldShowChanged,
});
+ static Future<bool> shouldShow() async {
+ final hasPermissions = await NotificationService.instance
+ .hasGrantedPermissions();
+ if (hasPermissions) return false;
+ final hasDismissed = await localSettings
+ .getCraftingMemoriesBannerDismissed();
+ return !hasDismissed;
+ }
+
+ Future<void> _onTap(BuildContext context) async {
+ if (await NotificationService.instance.requestPermissions(context) &&
+ context.mounted) {
+ onShouldShowChanged?.call(false);
+ }
+ }
+
+ Future<void> _onDismissTap(BuildContext context) async {
+ await localSettings.setCraftingMemoriesBannerDismissed();
+ if (!context.mounted) return;
+ onShouldShowChanged?.call(false);
+ }
+
@override
Widget build(BuildContext context) {
final l10n = context.strings;
return Padding(
padding: const EdgeInsets.symmetric(
- horizontal: MemoryCardWidget.gap / 2.0,
+ horizontal: kMemoryCardStripGap / 2.0,
),
child: SizedBox(
- width: width,
height: height,
+ width: width,
child: ClipRRect(
borderRadius: BorderRadius.circular(18),
child: Stack(
children: [
Positioned.fill(
child: GestureDetector(
- onTap: () async {
- if (await NotificationService.instance.requestPermissions(
- context,
- ) &&
- context.mounted) {
- onNotificationsPermissionGranted?.call();
- }
- },
+ onTap: () => _onTap(context),
child: Stack(
children: [
const Positioned.fill(child: Horts()),
@@ -113,11 +128,7 @@ class CraftingMemoriesCardWidget extends StatelessWidget {
message: l10n.close,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
- onTap: () async {
- await localSettings.setCraftingMemoriesBannerDismissed();
- if (!context.mounted) return;
- onNotificationsPermissionGranted?.call();
- },
+ onTap: () => _onDismissTap(context),
child: Padding(
padding: EdgeInsets.all(width * 0.08),
child: SizedBox.square(
diff --git a/mobile/apps/photos/lib/ui/home/memories/memories_strip.dart b/mobile/apps/photos/lib/ui/home/memories/memories_strip.dart
index d843555fbb..8b981f40a3 100644
--- a/mobile/apps/photos/lib/ui/home/memories/memories_strip.dart
+++ b/mobile/apps/photos/lib/ui/home/memories/memories_strip.dart
@@ -1,20 +1,29 @@
import "dart:async";
import "dart:math";
+import "package:collection/collection.dart";
import 'package:flutter/material.dart';
import "package:flutter_animate/flutter_animate.dart";
import "package:photos/core/event_bus.dart";
+import "package:photos/events/event.dart";
import "package:photos/events/memories_changed_event.dart";
import "package:photos/events/memories_setting_changed.dart";
import "package:photos/events/memory_seen_event.dart";
import "package:photos/models/memories/smart_memory.dart";
import "package:photos/service_locator.dart";
-import "package:photos/services/notification_service.dart";
import "package:photos/ui/home/memories/crafting_memories_card.dart";
import 'package:photos/ui/home/memories/memory_card.dart';
+import "package:photos/ui/home/memories/memory_card_constants.dart";
import "package:photos/ui/home/memories/memory_cover_util.dart";
import "package:photos/ui/home/memories/memory_video_prefetcher.dart";
+class MemoryCardWrapper {
+ final String id;
+ final Widget Function() widget;
+
+ const MemoryCardWrapper({required this.id, required this.widget});
+}
+
class MemoriesStripWidget extends StatefulWidget {
const MemoriesStripWidget({super.key});
@@ -26,117 +35,107 @@ class _MemoriesStripWidgetState extends State<MemoriesStripWidget> {
late StreamSubscription<MemoriesSettingChanged> _memoriesSettingSubscription;
late StreamSubscription<MemoriesChangedEvent> _memoriesChangedSubscription;
late StreamSubscription<MemorySeenEvent> _memorySeenSubscription;
- late double _memoryheight;
- late double _memoryWidth;
+ late double _cardWidth;
// Delay cover warming past startup; generations invalidate stale work.
Timer? _warmTimer;
int _warmGeneration = 0;
String? _lastWarmSignature;
final _videoPrefetcher = MemoryVideoPrefetcher();
- late Future<bool> _showCraftingMemories;
+ bool _shouldShowCraftingMemories = false;
+ late Future<void> _shouldShowCraftingMemoriesLoaded;
+ late Future<List<SmartMemory>> _memories;
@override
void initState() {
super.initState();
- _refreshShowCraftingMemories();
+ _shouldShowCraftingMemoriesLoaded = CraftingMemoriesCardWidget.shouldShow()
+ .then((value) {
+ _shouldShowCraftingMemories = value;
+ });
+
+ _fetchMemories(null);
+
_memoriesSettingSubscription = Bus.instance
.on<MemoriesSettingChanged>()
- .listen((event) {
- if (mounted) {
- setState(() {});
- }
- });
+ .listen(_fetchMemories);
_memoriesChangedSubscription = Bus.instance
.on<MemoriesChangedEvent>()
- .listen((event) {
- if (mounted) {
- setState(() {});
- }
- });
- _memorySeenSubscription = Bus.instance.on<MemorySeenEvent>().listen((
- event,
- ) {
- if (mounted) {
- setState(() {});
- }
- });
- }
-
- void _refreshShowCraftingMemories() {
- _showCraftingMemories = _getShowCraftingMemories();
+ .listen(_fetchMemories);
+ _memorySeenSubscription = Bus.instance.on<MemorySeenEvent>().listen(
+ _fetchMemories,
+ );
}
- Future<bool> _getShowCraftingMemories() async {
- final hasPermissions = await NotificationService.instance
- .hasGrantedPermissions();
- if (hasPermissions) return false;
- final hasDismissed = await localSettings
- .getCraftingMemoriesBannerDismissed();
- if (hasDismissed) return false;
- return true;
+ @override
+ void dispose() {
+ _memoriesSettingSubscription.cancel();
+ _memoriesChangedSubscription.cancel();
+ _memorySeenSubscription.cancel();
+ _warmTimer?.cancel();
+ _videoPrefetcher.dispose();
+ super.dispose();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
+ const defaultWidth = 145.011;
final screenWidth = MediaQuery.sizeOf(context).width;
final screenHeight = MediaQuery.sizeOf(context).height;
if (screenWidth < screenHeight) {
- _memoryWidth = min(
- screenWidth * (MemoryCardWidget.defaultWidth / 376.0),
- MemoryCardWidget.defaultWidth * 1.5,
+ _cardWidth = min(
+ screenWidth * (defaultWidth / 376.0),
+ defaultWidth * 1.5,
);
- _memoryheight = _memoryWidth * MemoryCardWidget.aspectRatio;
} else {
- _memoryWidth = min(
- screenHeight * .3,
- MemoryCardWidget.defaultWidth * 1.5,
- );
- _memoryheight = _memoryWidth * MemoryCardWidget.aspectRatio;
+ _cardWidth = min(screenHeight * .3, defaultWidth * 1.5);
}
}
- @override
- void dispose() {
- _memoriesSettingSubscription.cancel();
- _memoriesChangedSubscription.cancel();
- _memorySeenSubscription.cancel();
- _warmTimer?.cancel();
- _videoPrefetcher.dispose();
- super.dispose();
- }
-
@override
Widget build(BuildContext context) {
if (!memoriesCacheService.showAnyMemories) {
_cancelPendingWarm();
return const SizedBox.shrink();
}
- return _memories();
- }
-
- Widget _memories() {
- return FutureBuilder<List<SmartMemory>>(
- initialData: memoriesCacheService.currentMemoriesSync,
- future: memoriesCacheService.getMemories(),
+ return FutureBuilder(
+ future: _memories,
builder: (context, snapshot) {
- if (snapshot.hasError || !snapshot.hasData) {
- _cancelPendingWarm();
- return const SizedBox.shrink();
- }
- if (snapshot.data!.isEmpty) {
- _cancelPendingWarm();
+ final memories = snapshot.data ?? [];
+ if (memories.isEmpty) {
return const SizedBox.shrink();
}
- final orderedMemories = _orderForStrip(snapshot.data!);
- _scheduleWarmCovers(orderedMemories);
return Column(
- key: ValueKey(identityHashCode(snapshot.data)),
+ key: ValueKey(identityHashCode(_memories)),
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 12),
- _buildMemories(orderedMemories),
+ FutureBuilder(
+ future: _shouldShowCraftingMemoriesLoaded,
+ builder: (context, _) {
+ final cardHeight = _cardWidth / kMemoryCardAspectRatio;
+ final cards = _buildCards(memories, cardHeight);
+ return SizedBox(
+ height: cardHeight + 2,
+ child: ListView.builder(
+ padding: const EdgeInsets.symmetric(
+ horizontal: kMemoryCardStripGap / 2.0,
+ ),
+ physics: const AlwaysScrollableScrollPhysics(
+ parent: BouncingScrollPhysics(),
+ ),
+ scrollDirection: Axis.horizontal,
+ itemCount: cards.length,
+ itemBuilder: (context, i) => KeyedSubtree(
+ key: ValueKey(cards[i].id),
+ child: cards[i].widget(),
+ ),
+ ),
+ );
+ },
+ ),
+
const SizedBox(height: 10),
],
).animate().fadeIn(
@@ -147,20 +146,70 @@ class _MemoriesStripWidgetState extends State<MemoriesStripWidget> {
);
}
- // Keep prefetch and UI on the same unseen-first ordering.
- List<SmartMemory> _orderForStrip(List<SmartMemory> memories) {
- final List<SmartMemory> orderedMemories = [];
- final List<SmartMemory> seen = [];
- for (final memory in memories) {
- final allSeen = memory.memories.every((element) => element.isSeen());
- if (allSeen) {
- seen.add(memory);
- } else {
- orderedMemories.add(memory);
- }
- }
- orderedMemories.addAll(seen);
- return orderedMemories;
+ List<SmartMemory> _sortMemories(List<SmartMemory> memories) {
+ final indexedMemories = memories.indexed.sorted((a, b) {
+ final aIsSeen = a.$2.memories.every((item) => item.isSeen());
+ final bIsSeen = b.$2.memories.every((item) => item.isSeen());
+ if (aIsSeen == bIsSeen) return a.$1.compareTo(b.$1);
+ return aIsSeen ? 1 : -1;
+ });
+
+ return indexedMemories.map((entry) => entry.$2).toList();
+ }
+
+ List<MemoryCardWrapper> _buildCards(
+ List<SmartMemory> memories,
+ double cardHeight,
+ ) {
+ return [
+ if (_shouldShowCraftingMemories)
+ MemoryCardWrapper(
+ id: "craftingMemories",
+ widget: () => CraftingMemoriesCardWidget(
+ width: cardHeight / 2,
+ height: cardHeight,
+ onShouldShowChanged: (shouldShow) {
+ if (!mounted || shouldShow == _shouldShowCraftingMemories) {
+ return;
+ }
+ setState(() {
+ _shouldShowCraftingMemories = shouldShow;
+ });
+ },
+ ),
+ ),
+ ...memories.indexed.map(
+ (entry) => MemoryCardWrapper(
+ id: entry.$2.id,
+ widget: () => MemoryCardWidget(
+ memories: memories,
+ width: _cardWidth,
+ height: cardHeight,
+ index: entry.$1,
+ ),
+ ),
+ ),
+ ];
+ }
+
+ void _fetchMemories(Event? _) {
+ setState(() {
+ _memories = memoriesCacheService
+ .getMemories()
+ .then(_sortMemories)
+ .then((memories) {
+ if (memories.isEmpty) {
+ _cancelPendingWarm();
+ } else {
+ _scheduleWarmCovers(memories);
+ }
+ return memories;
+ })
+ .onError((_, _) {
+ _cancelPendingWarm();
+ return [];
+ });
+ });
}
void _scheduleWarmCovers(List<SmartMemory> memories) {
@@ -172,20 +221,18 @@ class _MemoriesStripWidgetState extends State<MemoriesStripWidget> {
_warmTimer?.cancel();
_warmTimer = Timer(const Duration(seconds: 5), () {
if (!mounted || gen != _warmGeneration) return;
- final memoryLists = memories
- .map((e) => e.memories)
- .toList(growable: false);
+ final itemLists = memories.map((e) => e.memories).toList(growable: false);
_videoPrefetcher.prefetchFiles(
- memoryLists
+ itemLists
.take(kMemoryCoverWarmCap)
- .where((memories) => memories.isNotEmpty)
- .map((memories) => memories[getNextMemoryIndex(memories)].file),
+ .where((items) => items.isNotEmpty)
+ .map((items) => items[getNextMemoryIndex(items)].file),
stillActive: () => mounted && gen == _warmGeneration,
replacePending: true,
);
unawaited(
warmMemoryCovers(
- memoryLists,
+ itemLists,
stillActive: () => mounted && gen == _warmGeneration,
),
);
@@ -196,9 +243,9 @@ class _MemoriesStripWidgetState extends State<MemoriesStripWidget> {
return memories
.map((e) => e.memories)
.take(kMemoryCoverWarmCap)
- .where((memories) => memories.isNotEmpty)
- .map((memories) {
- final file = memories[getNextMemoryIndex(memories)].file;
+ .where((items) => items.isNotEmpty)
+ .map((items) {
+ final file = items[getNextMemoryIndex(items)].file;
return '${file.uploadedFileID ?? ""}|'
'${file.generatedID ?? ""}|'
'${file.localID ?? ""}|'
@@ -215,48 +262,4 @@ class _MemoriesStripWidgetState extends State<MemoriesStripWidget> {
_lastWarmSignature = null;
_videoPrefetcher.clearPending();
}
-
- Widget _buildMemories(List<SmartMemory> memories) {
- return FutureBuilder<bool>(
- future: _showCraftingMemories,
- builder: (context, snapshot) {
- final showCraftingMemories = snapshot.data ?? false;
- return SizedBox(
- height: _memoryheight + MemoryCardWidget.outerStrokeWidth * 2,
- child: ListView.builder(
- padding: const EdgeInsets.symmetric(
- horizontal: MemoryCardWidget.gap / 2.0,
- ),
- physics: const AlwaysScrollableScrollPhysics(
- parent: BouncingScrollPhysics(),
- ),
- scrollDirection: Axis.horizontal,
- itemCount: (showCraftingMemories ? 1 : 0) + memories.length,
- itemBuilder: (context, itemIndex) {
- if (showCraftingMemories && itemIndex == 0) {
- return CraftingMemoriesCardWidget(
- width: _memoryheight * 0.5,
- height: _memoryheight,
- onNotificationsPermissionGranted: () {
- if (!mounted) return;
- setState(() {
- _refreshShowCraftingMemories();
- });
- },
- );
- }
- final memoryIndex = itemIndex - (showCraftingMemories ? 1 : 0);
- return MemoryCardWidget(
- smartMemory: memories[memoryIndex],
- allMemories: memories,
- height: _memoryheight,
- width: _memoryWidth,
- currentMemoryIndex: memoryIndex,
- );
- },
- ),
- );
- },
- );
- }
}
diff --git a/mobile/apps/photos/lib/ui/home/memories/memory_card.dart b/mobile/apps/photos/lib/ui/home/memories/memory_card.dart
index 16fa95e381..5abb96c140 100644
--- a/mobile/apps/photos/lib/ui/home/memories/memory_card.dart
+++ b/mobile/apps/photos/lib/ui/home/memories/memory_card.dart
@@ -5,133 +5,124 @@ import "package:photos/core/constants.dart";
import "package:photos/models/memories/smart_memory.dart";
import "package:photos/theme/colors.dart";
import "package:photos/ui/home/memories/all_memories_page.dart";
+import "package:photos/ui/home/memories/memory_card_constants.dart";
import "package:photos/ui/home/memories/memory_cover_util.dart";
import "package:photos/ui/viewer/file/thumbnail_widget.dart";
class MemoryCardWidget extends StatefulWidget {
- final SmartMemory smartMemory;
- final List<SmartMemory> allMemories;
- final double height;
+ final List<SmartMemory> memories;
final double width;
- static const defaultWidth = 145.011;
- static const defaultHeight = 210.5;
- static const outerStrokeWidth = 1.0;
- static const aspectRatio = defaultHeight / defaultWidth;
- static const gap = 5.0;
- final int currentMemoryIndex;
+ final double height;
+ final int index;
const MemoryCardWidget({
- required this.smartMemory,
- required this.allMemories,
- required this.height,
+ required this.memories,
required this.width,
- required this.currentMemoryIndex,
+ required this.height,
+ required this.index,
super.key,
- });
+ }) : assert(index >= 0);
@override
State<MemoryCardWidget> createState() => _MemoryCardWidgetState();
}
class _MemoryCardWidgetState extends State<MemoryCardWidget> {
+ Future<void> _onTap() async {
+ await routeToPage(
+ context,
+ forceCustomPageRoute: true,
+ AllMemoriesPage(
+ initialPageIndex: widget.index,
+ allMemories: widget.memories,
+ ),
+ );
+ if (!mounted) return;
+ setState(() {});
+ }
+
@override
Widget build(BuildContext context) {
// The list can be empty after deleting every memory and returning here.
- final memories = widget.smartMemory.memories;
- if (memories.isEmpty) {
+ if (widget.index >= widget.memories.length) {
return const SizedBox.shrink();
}
-
- final index = getNextMemoryIndex(memories);
- final title = widget.smartMemory.title;
-
- final memory = memories[index];
- final isSeen = memory.isSeen();
-
+ final memory = widget.memories[widget.index];
+ if (memory.memories.isEmpty) {
+ return const SizedBox.shrink();
+ }
+ final itemIndex = getNextMemoryIndex(memory.memories);
+ final item = memory.memories[itemIndex];
return Padding(
padding: const EdgeInsets.symmetric(
- horizontal: MemoryCardWidget.gap / 2.0,
+ horizontal: kMemoryCardStripGap / 2.0,
),
child: GestureDetector(
- onTap: () async {
- await routeToPage(
- context,
- forceCustomPageRoute: true,
- AllMemoriesPage(
- initialPageIndex: widget.currentMemoryIndex,
- allMemories: widget.allMemories,
- ),
- );
- if (!mounted) return;
- setState(() {});
- },
- child: Container(
- height: widget.height,
- width: widget.width,
- decoration: BoxDecoration(borderRadius: BorderRadius.circular(18)),
- child: ClipRRect(
- borderRadius: BorderRadius.circular(18),
- child: Container(
- foregroundDecoration: isSeen
- ? const BoxDecoration(
- color: Color(0xFFBFBFBF),
- backgroundBlendMode: BlendMode.saturation,
- )
- : null,
- child: Stack(
- fit: StackFit.expand,
- alignment: Alignment.bottomCenter,
- children: [
- Hero(
- tag: "memories" + memory.file.tag,
- child: ThumbnailWidget(
- memory.file,
- shouldShowSyncStatus: false,
- thumbnailSize: thumbnailLargeSize,
- key: Key("memories" + memory.file.tag),
- ),
+ onTap: _onTap,
+ child: ClipRRect(
+ borderRadius: BorderRadius.circular(kMemoryCardBorderRadius),
+ child: Container(
+ height: widget.height,
+ width: widget.width,
+ foregroundDecoration: item.isSeen()
+ ? const BoxDecoration(
+ color: Color(0xFFBFBFBF),
+ backgroundBlendMode: BlendMode.saturation,
+ )
+ : null,
+ child: Stack(
+ fit: StackFit.expand,
+ alignment: Alignment.bottomCenter,
+ children: [
+ Hero(
+ tag: "memories" + item.file.tag,
+ child: ThumbnailWidget(
+ item.file,
+ shouldShowSyncStatus: false,
+ thumbnailSize: thumbnailLargeSize,
+ key: Key("memories" + item.file.tag),
),
- Container(
- decoration: BoxDecoration(
- gradient: LinearGradient(
- colors: [
- Colors.transparent,
- Colors.black.withValues(alpha: 0.72),
- ],
- stops: const [0.53663, 0.89955],
- begin: Alignment.topCenter,
- end: Alignment.bottomCenter,
- ),
+ ),
+ Container(
+ decoration: BoxDecoration(
+ gradient: LinearGradient(
+ colors: [
+ Colors.transparent,
+ Colors.black.withValues(alpha: 0.72),
+ ],
+ stops: const [0.53663, 0.89955],
+ begin: Alignment.topCenter,
+ end: Alignment.bottomCenter,
),
),
- Positioned(
- bottom: 0,
- child: SizedBox(
- width: widget.width,
- child: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Hero(
- tag: title,
- child: Text(
- title,
- maxLines: 3,
- style: TextStyle(
- fontSize: 14,
- height: 16 / 14,
- fontFamily: TextStyles.outfitFontFamily,
- package: TextStyles.fontPackage,
- color: isSeen ? textFaintDark : Colors.white,
- fontWeight: FontWeight.w700,
- letterSpacing: 0,
- ),
- textAlign: TextAlign.left,
+ ),
+ Positioned(
+ bottom: 0,
+ child: SizedBox(
+ width: widget.width,
+ child: Padding(
+ padding: const EdgeInsets.all(16.0),
+ child: Hero(
+ tag: memory.title,
+ child: Text(
+ memory.title,
+ maxLines: 3,
+ style: TextStyle(
+ fontSize: 14,
+ height: 16 / 14,
+ fontFamily: TextStyles.outfitFontFamily,
+ package: TextStyles.fontPackage,
+ color: item.isSeen() ? textFaintDark : Colors.white,
+ fontWeight: FontWeight.w700,
+ letterSpacing: 0,
),
+ textAlign: TextAlign.left,
),
),
),
),
- ],
- ),
+ ),
+ ],
),
),
),
diff --git a/mobile/apps/photos/lib/ui/home/memories/memory_card_constants.dart b/mobile/apps/photos/lib/ui/home/memories/memory_card_constants.dart
new file mode 100644
index 0000000000..f0618d5dbb
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/home/memories/memory_card_constants.dart
@@ -0,0 +1,3 @@
+const kMemoryCardBorderRadius = 18.0;
+const kMemoryCardAspectRatio = 29 / 42;
+const kMemoryCardStripGap = 5.0;