enqueue method

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

Create and add a user task to the userTasks queue. The user task is created from the AppTask that the executor is executing.

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.

Returns the UserTask added to userTasks. Returns null if not successful.

Implementation

Future<UserTask?> enqueue(
  AppTaskExecutor executor, {
  DateTime? triggerTime,
  bool sendNotification = true,
}) async {
  if (_userTaskFactories[executor.task.type] == null) {
    warning(
        '$runtimeType - 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;
    _controller.sink.add(userTask);
    debug('$runtimeType - Enqueued $userTask');

    if (notificationsEnabled && sendNotification) {
      // create notification
      (triggerTime == null)
          ? await SmartPhoneClientManager()
              .notificationController
              ?.createTaskNotification(userTask)
          : await SmartPhoneClientManager()
              .notificationController
              ?.scheduleTaskNotification(userTask);
    }
    return userTask;
  }
}