scheduleRecurringNotification method

Future<NotificationFailure?> scheduleRecurringNotification({
  1. required NotificationPayload payload,
  2. required DateTime firstScheduledTime,
  3. required RepeatInterval repeatInterval,
  4. String? timeZone,
  5. DateTime? endDate,
})

Schedules a recurring notification.

Implementation

Future<NotificationFailure?> scheduleRecurringNotification({
  required NotificationPayload payload,
  required DateTime firstScheduledTime,
  required RepeatInterval repeatInterval,
  String? timeZone,
  DateTime? endDate,
}) async {
  try {
    final tz.TZDateTime firstTZDateTime = _convertToTZDateTime(
      firstScheduledTime,
      timeZone,
    );

    if (firstTZDateTime.isBefore(tz.TZDateTime.now(tz.local))) {
      return NotificationFailure.invalidInput(
        details: 'First scheduled time must be in the future',
      );
    }

    if (repeatInterval == RepeatInterval.none) {
      return NotificationFailure.invalidInput(
        details: 'Repeat interval cannot be none for recurring notifications',
      );
    }

    // Generate schedule instances up to the end date or reasonable limit
    final List<tz.TZDateTime> scheduledTimes = _generateRecurringSchedule(
      firstTZDateTime,
      repeatInterval,
      endDate != null ? _convertToTZDateTime(endDate, timeZone) : null,
    );

    if (scheduledTimes.isEmpty) {
      return NotificationFailure.invalidInput(
        details: 'No valid schedule times generated',
      );
    }

    // The actual scheduling would be handled by the concrete repository
    return null;
  } catch (e, stackTrace) {
    safeDebugLog('Failed to schedule recurring notification: $e');
    return NotificationFailure.scheduling(
      details: e.toString(),
      stackTrace: stackTrace,
    );
  }
}