saveToGallery method

Future<void> saveToGallery(
  1. List<Attachment> attachments,
  2. ChatItem item
)

Saves the provided attachments to the gallery.

Implementation

Future<void> saveToGallery(
  List<Attachment> attachments,
  ChatItem item,
) async {
  // Tries downloading the [attachments].
  Future<void> download() async {
    for (Attachment attachment in attachments) {
      if (attachment is! LocalAttachment) {
        if (attachment is FileAttachment && attachment.isVideo) {
          MessagePopup.success('label_video_downloading'.l10n);
        }
        try {
          await PlatformUtils.saveToGallery(
            attachment.original.url,
            attachment.filename,
            checksum: attachment.original.checksum,
            size: attachment.original.size,
            isImage: attachment is ImageAttachment,
          );
        } on UnsupportedError catch (_) {
          MessagePopup.error('err_unsupported_format'.l10n);
          continue;
        }
      } else {
        // TODO: Implement [LocalAttachment] download.
        throw UnimplementedError();
      }
    }

    MessagePopup.success(
      attachments.length > 1
          ? 'label_files_saved_to_gallery'.l10n
          : attachments.first is ImageAttachment
          ? 'label_image_saved_to_gallery'.l10n
          : 'label_video_saved_to_gallery'.l10n,
    );
  }

  try {
    try {
      await download();
    } on DioException catch (e) {
      if (e.response?.statusCode == 403) {
        await chat?.updateAttachments(item);
        await Future.delayed(Duration.zero);
        await download();
      } else {
        rethrow;
      }
    }
  } on UnsupportedError catch (_) {
    MessagePopup.error('err_unsupported_format'.l10n);
  } catch (_) {
    MessagePopup.error('err_could_not_download'.l10n);
    rethrow;
  }
}