Skip to content

armgit / ente

Public Imported from GitHub
0

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

Integrate notifications to OnThisDay memories

laurenspriem5a4d8950af5d
diff --git a/mobile/lib/services/memories_cache_service.dart b/mobile/lib/services/memories_cache_service.dart
index bb8f8d2042..d15306e8c1 100644
--- a/mobile/lib/services/memories_cache_service.dart
+++ b/mobile/lib/services/memories_cache_service.dart
@@ -18,6 +18,7 @@ import "package:photos/models/memories/memory.dart";
import "package:photos/models/memories/smart_memory.dart";
import "package:photos/models/memories/smart_memory_constants.dart";
import "package:photos/service_locator.dart";
+import "package:photos/services/notification_service.dart";
import "package:photos/services/search_service.dart";
import "package:photos/ui/home/memories/full_screen_memory.dart";
import "package:photos/utils/navigation_util.dart";
@@ -213,6 +214,9 @@ class MemoriesCacheService {
_cachedMemories = nowResult.memories
.where((memory) => memory.shouldShowNow())
.toList();
+ await _scheduleOnThisDayNotifications(
+ [...nowResult.memories, ...nextResult.memories],
+ );
locationService.baseLocations = nowResult.baseLocations;
await file.writeAsBytes(
MemoriesCache.encodeToJsonString(newCache).codeUnits,
@@ -233,6 +237,51 @@ class MemoriesCacheService {
return newCache;
}
+ Future<void> _scheduleOnThisDayNotifications(
+ List<SmartMemory> allMemories,
+ ) async {
+ await NotificationService.instance
+ .clearAllScheduledNotifications(containingPayload: "onThisDay");
+ final scheduledDates = <DateTime>{};
+ for (final memory in allMemories) {
+ if (memory.type != MemoryType.onThisDay) {
+ continue;
+ }
+ final numberOfMemories = memory.memories.length;
+ if (numberOfMemories < 5) continue;
+ final firstDateToShow =
+ DateTime.fromMicrosecondsSinceEpoch(memory.firstDateToShow);
+ final scheduleTime = DateTime(
+ firstDateToShow.year,
+ firstDateToShow.month,
+ firstDateToShow.day,
+ 8,
+ );
+ if (scheduleTime.isBefore(DateTime.now())) {
+ _logger.info(
+ "Skipping scheduling notification for memory ${memory.id} because the date is in the past",
+ );
+ continue;
+ }
+ if (scheduledDates.contains(scheduleTime)) {
+ _logger.info(
+ "Skipping scheduling notification for memory ${memory.id} because the date is already scheduled",
+ );
+ continue;
+ }
+ await NotificationService.instance.scheduleNotification(
+ "On this day",
+ "Look back on $numberOfMemories memories πŸŒ„",
+ id: memory.id.hashCode,
+ channelID: "onThisDay",
+ channelName: "On this day",
+ payload: "onThisDay_${memory.id}",
+ dateTime: scheduleTime,
+ );
+ scheduledDates.add(scheduleTime);
+ }
+ }
+
MemoriesCache _processOldCache(MemoriesCache? oldCache) {
final List<PeopleShownLog> peopleShownLogs = [];
final List<ClipShownLog> clipShownLogs = [];
diff --git a/mobile/lib/services/notification_service.dart b/mobile/lib/services/notification_service.dart
index a8b3288678..b56e9c5f50 100644
--- a/mobile/lib/services/notification_service.dart
+++ b/mobile/lib/services/notification_service.dart
@@ -202,7 +202,9 @@ class NotificationService {
);
}
- Future<void> clearAllScheduledNotifications() async {
+ Future<void> clearAllScheduledNotifications({
+ String? containingPayload,
+ }) async {
_logger.info("Clearing all scheduled notifications");
final pending = await _notificationsPlugin.pendingNotificationRequests();
if (pending.isEmpty) {
@@ -210,6 +212,13 @@ class NotificationService {
return;
}
for (final request in pending) {
+ if (containingPayload != null &&
+ !request.payload.toString().contains(containingPayload)) {
+ _logger.info(
+ "Skip clearing of notification with id: ${request.id} and payload: ${request.payload}",
+ );
+ continue;
+ }
_logger.info("Clearing notification with id: ${request.id}");
await _notificationsPlugin.cancel(request.id);
}