enqueue method

Future<UserTask?> enqueue(
  1. AppTaskExecutor<AppTask> executor, {
  2. DateTime? triggerTime,
  3. bool sendNotification = true,
})

Put executor on the userTasks for access by the app.

triggerTime specifies when the task should trigger, i.e., be available. Notify the user if sendNotification and notificationsEnabled is true. If triggerTime is null, a notification is send immediately. userTaskEvent specifies if an app event should be generated.

Returns the UserTask added to the userTasks.

Returns null if not successful.

Implementation

Future<UserTask?> enqueue(
  AppTaskExecutor executor, {
  DateTime? triggerTime,
  bool sendNotification = true,
  // bool userTaskEvent = true,
}) async {
  if (_userTaskFactories[executor.task.type] == null) {
    warning(
        'Could not enqueue AppTask. Could not find a factory for creating '
        "a UserTask for type '${executor.task.type}'");
    return null;
  } else {
    UserTask userTask =
        _userTaskFactories[executor.task.type]!.create(executor);
    userTask.state = UserTaskState.enqueued;
    userTask.enqueued = DateTime.now();
    userTask.triggerTime = triggerTime ?? DateTime.now();
    _userTaskMap[userTask.id] = userTask;
    // if (userTaskEvent)
    _controller.add(userTask);
    debug('$runtimeType - Enqueued $userTask');

    if (notificationsEnabled && sendNotification) {
      // create notification
      // TODO - iOS has a limit where it will only keep 64 notifications that will fire the soonest...
      // See the flutter_local_notifications plugin.
      (triggerTime == null)
          ? await SmartPhoneClientManager()
              .notificationController
              ?.sendNotification(userTask)
          : await SmartPhoneClientManager()
              .notificationController
              ?.scheduleNotification(userTask);
    }
    return userTask;
  }
}