Files
social-app/apps/lib/features/calendar/reminders/reminder_action_executor.dart
T

68 lines
2.0 KiB
Dart

import '../data/services/calendar_service.dart';
import '../../../core/notifications/local_notification_service.dart';
import 'models/reminder_action.dart';
import 'models/reminder_payload.dart';
class ReminderActionExecutor {
final CalendarService _calendarService;
final LocalNotificationService _notificationService;
ReminderActionExecutor({
required CalendarService calendarService,
required LocalNotificationService notificationService,
}) : _calendarService = calendarService,
_notificationService = notificationService;
Future<void> handleAction({
required ReminderAction action,
required ReminderPayload payload,
}) async {
final ids = payload.mode == ReminderPayloadMode.aggregate
? (payload.aggregateIds.isNotEmpty
? payload.aggregateIds
: <String>[payload.eventId])
: <String>[payload.eventId];
if (action == ReminderAction.archive) {
for (final id in ids) {
await _notificationService.cancelEventReminder(id);
await _archiveEvent(id);
}
return;
}
if (action == ReminderAction.snooze10m) {
for (final id in ids) {
await _snoozeEvent(id);
}
}
}
Future<void> _snoozeEvent(String eventId) async {
final event = await _calendarService.getEventById(eventId);
if (event == null) {
return;
}
final now = DateTime.now();
final endAt = event.endAt;
if (endAt != null && !now.isBefore(endAt)) {
await _notificationService.cancelEventReminder(eventId);
await _archiveEvent(eventId);
return;
}
final nextAt = now.add(const Duration(minutes: 10));
if (endAt != null && !nextAt.isBefore(endAt)) {
await _notificationService.cancelEventReminder(eventId);
await _archiveEvent(eventId);
return;
}
await _notificationService.scheduleReminderAt(event, nextAt);
}
Future<void> _archiveEvent(String eventId) async {
await _calendarService.archiveEvent(eventId);
}
}