Improve the speed of fetching attachments meta via batch api

This commit is contained in:
2024-08-02 22:46:48 +08:00
parent 0ad4854443
commit 07771e8979
3 changed files with 56 additions and 29 deletions

View File

@ -222,19 +222,13 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
setState(() => _isBusy = true);
int progress = 0;
for (var idx = 0; idx < widget.initialAttachments.length; idx++) {
attach.getMetadata(widget.initialAttachments[idx]).then((resp) {
progress++;
_attachments[idx] = resp;
if (progress == widget.initialAttachments.length) {
setState(() {
_isBusy = false;
_isFirstTimeBusy = false;
});
}
attach.listMetadata(widget.initialAttachments).then((result) {
setState(() {
_attachments = result;
_isBusy = false;
_isFirstTimeBusy = false;
});
}
});
}
void _showAttachmentPreview(Attachment element) {

View File

@ -45,7 +45,7 @@ class _AttachmentListState extends State<AttachmentList> {
List<Attachment?> _attachmentsMeta = List.empty();
void _getMetadataList() {
final AttachmentProvider provider = Get.find();
final AttachmentProvider attach = Get.find();
if (widget.attachmentsId.isEmpty) {
return;
@ -53,25 +53,15 @@ class _AttachmentListState extends State<AttachmentList> {
_attachmentsMeta = List.filled(widget.attachmentsId.length, null);
}
int progress = 0;
for (var idx = 0; idx < widget.attachmentsId.length; idx++) {
provider.getMetadata(widget.attachmentsId[idx]).then((resp) {
progress++;
if (resp != null) {
_attachmentsMeta[idx] = resp;
}
if (progress == widget.attachmentsId.length) {
calculateAspectRatio();
if (mounted) {
setState(() => _isLoading = false);
}
}
attach.listMetadata(widget.attachmentsId).then((result) {
setState(() {
_attachmentsMeta = result;
_isLoading = false;
});
}
});
}
void calculateAspectRatio() {
void _calculateAspectRatio() {
bool isConsistent = true;
double? consistentValue;
int portrait = 0, square = 0, landscape = 0;