Save method

Future<void> Save()
Saves this collection by creating new attachment and deleting removed ones.

Implementation

Future<void> Save() async {
  List<Attachment?> attachments = <Attachment?>[];

  // Retrieve a list of attachments that have to be deleted.
  for (Attachment? attachment in this.RemovedItems) {
    if (!attachment!.IsNew) {
      attachments.add(attachment);
    }
  }

  // If any, delete them by calling the DeleteAttachment web method.
  if (attachments.length > 0) {
    await this._InternalDeleteAttachments(attachments);
  }

  attachments.clear();

  // Retrieve a list of attachments that have to be created.
  for (Attachment attachment in this) {
    if (attachment.IsNew) {
      attachments.add(attachment);
    }
  }

  // If there are any, create them by calling the CreateAttachment web method.
  if (attachments.length > 0) {
    if (this._owner!.IsAttachment) {
      await this._InternalCreateAttachments(
          this._owner!.ParentAttachment!.Id, attachments);
    } else {
      await this
          ._InternalCreateAttachments(this._owner!.Id!.UniqueId, attachments);
    }
  }

  // Process all of the item attachments in this collection.
  for (Attachment attachment in this) {
    if (attachment is ItemAttachment) {
      // Make sure item was created/loaded before trying to create/delete sub-attachments
      if (attachment.Item != null) {
        // Create/delete any sub-attachments
        await attachment.Item!.Attachments.Save();

        // Clear the item's change log
        attachment.Item!.ClearChangeLog();
      }
    }
  }

  super.ClearChangeLog();
}