copy method

Future<void> copy(
  1. Post post,
  2. PostItem item
)

Puts the provided PostItem to the copy buffer.

Implementation

Future<void> copy(Post post, PostItem item) async {
  Log.debug('copy($item)', '$runtimeType');

  final String extension = item.attachment.original.name
      .split('.')
      .last
      .toLowerCase();

  final SimpleFileFormat? format = switch (extension) {
    'jpg' || 'jpeg' => Formats.jpeg,
    'png' => Formats.png,
    'svg' => Formats.svg,
    'gif' => Formats.gif,
    'tiff' => Formats.tiff,
    'bmp' => Formats.bmp,
    'webp' => Formats.webp,
    (_) => null,
  };

  if (format != null) {
    try {
      final response = await (await PlatformUtils.dio).get(
        item.attachment.original.url,
        options: Options(responseType: ResponseType.bytes),
      );

      final bytes = response.data;
      if (bytes is Uint8List) {
        await PlatformUtils.copy(format: format, data: bytes);
        MessagePopup.success('label_copied'.l10n);
      }
    } on DioException catch (e) {
      if (e.response?.statusCode == 403) {
        reload(post);
      } else {
        Log.error('copy() -> $e', '$runtimeType');
      }

      MessagePopup.error('err_data_transfer'.l10n);
    } catch (e) {
      Log.error('copy() -> $e', '$runtimeType');
      MessagePopup.error('err_data_transfer'.l10n);
    }
  } else {
    MessagePopup.error('Unsupported format: $extension');
  }
}