openAttachmentNoteModal method
Implementation
Future<void> openAttachmentNoteModal(
Attachment? attachment, {
bool isEditable = true,
}) async {
final String? attachmentId = attachment?.id;
if (attachmentId != null &&
attachmentId.trim().isNotEmpty &&
isAttachmentNoteModalLoading(attachmentId)) {
return;
}
// Stop any other in-flight content loads (best-effort cancellation).
_noteModalLoadRequestId++;
final int loadRequestId = _noteModalLoadRequestId;
noteName.value = attachment?.fileName ?? "";
if (attachment != null) {
try {
if (attachmentId != null && attachmentId.trim().isNotEmpty) {
attachmentIdsLoadingNoteModal.add(attachmentId);
attachmentIdsLoadingNoteModal.refresh();
}
// Clear any other loading flags so only one attachment shows loading state.
for (final Attachment a in attachments) {
if (a.id != attachment.id && a.isLoadingContent == true) {
a.isLoadingContent = false;
}
}
attachment.isLoadingContent = true;
attachments.refresh();
update();
String? content = await AttachmentService.readAttachmentContent(
attachment.id,
);
if (loadRequestId != _noteModalLoadRequestId) {
// A newer request started; ignore this result.
return;
}
noteContent.value = content ?? "";
attachment.isLoadingContent = false;
if (attachmentId != null && attachmentId.trim().isNotEmpty) {
attachmentIdsLoadingNoteModal.remove(attachmentId);
attachmentIdsLoadingNoteModal.refresh();
}
update();
attachments.refresh();
} catch (e) {
attachment.isLoadingContent = false;
noteContent.value = "";
if (attachmentId != null && attachmentId.trim().isNotEmpty) {
attachmentIdsLoadingNoteModal.remove(attachmentId);
attachmentIdsLoadingNoteModal.refresh();
}
attachments.refresh();
update();
}
} else {
noteContent.value = "";
}
noteNameController.text = noteName.value;
noteContentController.text = noteContent.value;
openAttachmentNote.value = attachment;
update();
showAttachmentNoteModal(isEditable: isEditable);
}