Save method
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 RemovedItems) {
if (!attachment!.IsNew) {
attachments.add(attachment);
}
}
// If any, delete them by calling the DeleteAttachment web method.
if (attachments.isNotEmpty) {
await _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.isNotEmpty) {
if (_owner!.IsAttachment) {
await _InternalCreateAttachments(
_owner!.ParentAttachment!.Id,
attachments,
);
} else {
await _InternalCreateAttachments(_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();
}