onResume method

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

Callback when this executor is resumed. Returns true if successfully resumed, false otherwise.

Implementation

@override
Future<bool> onResume() async {
  final from = triggeredTask.hasBeenScheduledUntil ?? DateTime.now();
  final to = from.add(Duration(days: 10)); // look 10 days ahead
  final schedule = triggerExecutor.getSchedule(from, to, 10);

  if (schedule.isNotEmpty) {
    // enqueue the first 6 (max) app tasks in the future
    var remainingNotifications =
        NotificationController.PENDING_NOTIFICATION_LIMIT -
            (await SmartPhoneClientManager()
                    .notificationController
                    ?.pendingNotificationRequestsCount ??
                0);
    remainingNotifications = min(remainingNotifications, 6);
    Iterator<DateTime> it = schedule.iterator;
    var count = 0;
    DateTime current = DateTime.now();
    while (it.moveNext() && count++ < remainingNotifications) {
      current = it.current;
      await AppTaskController().enqueue(
        taskExecutor,
        triggerTime: current,
      );
    }

    // save timestamp
    triggeredTask.hasBeenScheduledUntil = current;

    // now pause and resume again when the time has passed
    // this in the case where the app keeps running in the background
    pause();
    var duration = current.millisecondsSinceEpoch -
        DateTime.now().millisecondsSinceEpoch;
    Timer(Duration(milliseconds: duration), () => resume());
  }

  return true;
}