saveUserTask method

Future<void> saveUserTask(
  1. UserTask task
)

Update or delete a task queue entry.

Implementation

Future<void> saveUserTask(UserTask task) async {
  debug("$runtimeType - Saving task to database '$task'.");
  switch (task.state) {
    case UserTaskState.initialized:
    case UserTaskState.enqueued:
    case UserTaskState.notified:
    case UserTaskState.started:
    case UserTaskState.canceled:
    case UserTaskState.done:
    case UserTaskState.expired:
    case UserTaskState.undefined:
      // all these cases we need to create or update the record
      var snapshot = UserTaskSnapshot.fromUserTask(task);
      final Map<String, dynamic> map = {
        STUDY_DEPLOYMENT_ID_COLUMN: task.studyDeploymentId,
        TASK_ID_COLUMN: task.id,
        TASK_COLUMN: jsonEncode(snapshot),
      };
      int count = await database?.update(
            TASK_QUEUE_TABLE_NAME,
            map,
            where: '$TASK_ID_COLUMN = ?',
            whereArgs: [task.id],
            conflictAlgorithm: ConflictAlgorithm.replace,
          ) ??
          0;

      if (count == 0) {
        await database?.insert(
          TASK_QUEUE_TABLE_NAME,
          map,
          conflictAlgorithm: ConflictAlgorithm.replace,
        );
      }
      break;
    case UserTaskState.dequeued:
      await database?.delete(
        TASK_QUEUE_TABLE_NAME,
        where: '$TASK_ID_COLUMN = ?',
        whereArgs: [task.id],
      );
      break;
  }
}