deleteFile method

Future<Attachment> deleteFile({
  1. required String attachmentId,
  2. required Future<void> updateHook(
    1. SqliteWriteContext context,
    2. Attachment attachment
    ),
})

Queues an attachment for delete. The default implementation assumes the attachment record already exists locally.

Implementation

Future<Attachment> deleteFile({
  required String attachmentId,
  required Future<void> Function(
          SqliteWriteContext context, Attachment attachment)
      updateHook,
}) async {
  return await _attachmentsService.withContext((attachmentContext) async {
    final attachment = await attachmentContext.getAttachment(attachmentId);
    if (attachment == null) {
      throw Exception(
        'Attachment record with id $attachmentId was not found.',
      );
    }

    return await _db.writeTransaction((tx) async {
      await updateHook(tx, attachment);
      return await attachmentContext.upsertAttachment(
        attachment.copyWith(
          state: AttachmentState.queuedDelete,
          hasSynced: false,
        ),
        tx,
      );
    });
  });
}