onResume method

  1. @override
Future<bool> onResume()
override

Called by resume. Returns true if successfully resumed.

If false, the executor moves to ExecutorState.PausedButShouldBeResumed.

Implementation

@override
Future<bool> onResume() async {
  debug('$runtimeType - ${taskControl.taskName} hasBeenScheduledUntil: ${taskControl.hasBeenScheduledUntil}');
  final from = taskControl.hasBeenScheduledUntil ?? DateTime.now();
  final to = DateTime.now().add(const Duration(days: 15)); // 15 days ahead
  // get all the instances where the task should be scheduled in the given range
  final schedule = triggerExecutor.getSchedule(from, to);

  if (schedule.isEmpty) {
    // Pause since the schedule is empty and there is not more to schedule.
    info('$runtimeType - No scheduled app tasks for task ${taskExecutor.task.name} - pausing executor again.');
    pause();
  } else {
    info('$runtimeType - Buffering ${schedule.length} app tasks ($schedule) for task ${taskExecutor.task.name}');

    Iterator<DateTime> it = schedule.iterator;
    DateTime current = DateTime.now();
    while (it.moveNext()) {
      current = it.current;
      AppTaskController().buffer(taskExecutor, taskControl, triggerTime: current);
    }

    // Now stop since the schedule has all been enqueued.
    pause();

    // .. but start again when the scheduled time has passed.
    // This in the case where the app keeps running in the background
    var duration = current.millisecondsSinceEpoch - DateTime.now().millisecondsSinceEpoch;

    Timer(Duration(milliseconds: duration), () => resume());
  }

  return true;
}