From eef4d7b522e9e5cd6c9441a95cbce3f0a4e35976 Mon Sep 17 00:00:00 2001 From: --global Date: Wed, 8 May 2024 12:13:08 +0700 Subject: [PATCH] fixup! TW-1650: reorganize image_builder folder --- .../events/images_builder/image_bubble.dart | 56 ++----------------- .../images_builder/image_placeholder.dart | 54 ++++++++++++++++++ .../message_content_image_builder.dart | 7 +++ .../sending_image_info_widget.dart | 41 ++++++++++---- .../unencrypted_image_builder_web.dart | 2 +- 5 files changed, 97 insertions(+), 63 deletions(-) create mode 100644 lib/pages/chat/events/images_builder/image_placeholder.dart diff --git a/lib/pages/chat/events/images_builder/image_bubble.dart b/lib/pages/chat/events/images_builder/image_bubble.dart index 41524433cf..d3b34a31ce 100644 --- a/lib/pages/chat/events/images_builder/image_bubble.dart +++ b/lib/pages/chat/events/images_builder/image_bubble.dart @@ -1,6 +1,8 @@ +import 'dart:typed_data'; + import 'package:fluffychat/pages/chat/events/images_builder/image_builder_web.dart'; +import 'package:fluffychat/pages/chat/events/images_builder/image_placeholder.dart'; import 'package:fluffychat/pages/chat/events/message_content_style.dart'; -import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart'; import 'package:fluffychat/utils/platform_infos.dart'; import 'package:flutter/material.dart'; @@ -21,6 +23,7 @@ class ImageBubble extends StatelessWidget { final void Function()? onTapSelectMode; final bool isPreview; final Duration animationDuration; + final Uint8List? imageData; final String? thumbnailCacheKey; final Map? thumbnailCacheMap; @@ -42,6 +45,7 @@ class ImageBubble extends StatelessWidget { this.thumbnailCacheMap, this.noResizeThumbnail = false, this.isPreview = true, + this.imageData, Key? key, }) : super(key: key); @@ -108,6 +112,7 @@ class ImageBubble extends StatelessWidget { cacheKey: thumbnailCacheKey, cacheMap: thumbnailCacheMap, noResize: noResizeThumbnail, + imageData: imageData, ), ], ), @@ -115,52 +120,3 @@ class ImageBubble extends StatelessWidget { ); } } - -class ImagePlaceholder extends StatelessWidget { - const ImagePlaceholder({ - super.key, - required this.event, - required this.width, - required this.height, - required this.fit, - }); - - final Event event; - final double width; - final double height; - final BoxFit fit; - - @override - Widget build(BuildContext context) { - if (event.messageType == MessageTypes.Sticker) { - return const Center( - child: CircularProgressIndicator.adaptive(), - ); - } - final String blurHashString = - event.blurHash ?? AppConfig.defaultImageBlurHash; - final ratio = event.infoMap['w'] is int && event.infoMap['h'] is int - ? event.infoMap['w'] / event.infoMap['h'] - : 1.0; - var width = 32; - var height = 32; - if (ratio > 1.0) { - height = (width / ratio).round(); - } else { - width = (height * ratio).round(); - } - return ClipRRect( - borderRadius: BorderRadius.zero, - child: SizedBox( - width: this.width, - height: this.height, - child: BlurHash( - hash: blurHashString, - decodingWidth: width, - decodingHeight: height, - imageFit: fit, - ), - ), - ); - } -} diff --git a/lib/pages/chat/events/images_builder/image_placeholder.dart b/lib/pages/chat/events/images_builder/image_placeholder.dart new file mode 100644 index 0000000000..a498d1ebb0 --- /dev/null +++ b/lib/pages/chat/events/images_builder/image_placeholder.dart @@ -0,0 +1,54 @@ +import 'package:fluffychat/config/app_config.dart'; +import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_blurhash/flutter_blurhash.dart'; +import 'package:matrix/matrix.dart'; + +class ImagePlaceholder extends StatelessWidget { + const ImagePlaceholder({ + super.key, + required this.event, + required this.width, + required this.height, + required this.fit, + }); + + final Event event; + final double width; + final double height; + final BoxFit fit; + + @override + Widget build(BuildContext context) { + if (event.messageType == MessageTypes.Sticker) { + return const Center( + child: CircularProgressIndicator.adaptive(), + ); + } + final String blurHashString = + event.blurHash ?? AppConfig.defaultImageBlurHash; + final ratio = event.infoMap['w'] is int && event.infoMap['h'] is int + ? event.infoMap['w'] / event.infoMap['h'] + : 1.0; + var width = 32; + var height = 32; + if (ratio > 1.0) { + height = (width / ratio).round(); + } else { + width = (height * ratio).round(); + } + return ClipRRect( + borderRadius: BorderRadius.zero, + child: SizedBox( + width: this.width, + height: this.height, + child: BlurHash( + hash: blurHashString, + decodingWidth: width, + decodingHeight: height, + imageFit: fit, + ), + ), + ); + } +} diff --git a/lib/pages/chat/events/images_builder/message_content_image_builder.dart b/lib/pages/chat/events/images_builder/message_content_image_builder.dart index 9a21f41165..1599d3941b 100644 --- a/lib/pages/chat/events/images_builder/message_content_image_builder.dart +++ b/lib/pages/chat/events/images_builder/message_content_image_builder.dart @@ -56,6 +56,13 @@ class MessageImageBuilder extends StatelessWidget { file.width?.toDouble() ?? MessageContentStyle.imageWidth(context), file.height?.toDouble() ?? MessageContentStyle.imageHeight(context), ).getDisplayImageInfo(context); + return SendingImageInfoWidget( + key: ValueKey(event.eventId), + matrixFile: file, + event: event, + onTapPreview: onTapPreview, + displayImageInfo: displayImageInfo, + ); } return ImageBubble( event, diff --git a/lib/pages/chat/events/images_builder/sending_image_info_widget.dart b/lib/pages/chat/events/images_builder/sending_image_info_widget.dart index 1350b3d459..ff04355d6f 100644 --- a/lib/pages/chat/events/images_builder/sending_image_info_widget.dart +++ b/lib/pages/chat/events/images_builder/sending_image_info_widget.dart @@ -5,6 +5,7 @@ import 'package:fluffychat/pages/image_viewer/image_viewer.dart'; import 'package:fluffychat/presentation/model/file/display_image_info.dart'; import 'package:fluffychat/utils/interactive_viewer_gallery.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart'; +import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/widgets/hero_page_route.dart'; import 'package:flutter/material.dart'; import 'package:flutter_blurhash/flutter_blurhash.dart'; @@ -99,18 +100,34 @@ class SendingImageInfoWidget extends StatelessWidget { hash: event.blurHash ?? AppConfig.defaultImageBlurHash, ), ), - Image.file( - File(matrixFile.filePath!), - width: displayImageInfo.size.width, - height: displayImageInfo.size.height, - cacheHeight: - (displayImageInfo.size.height * devicePixelRatio) - .toInt(), - cacheWidth: (displayImageInfo.size.width * devicePixelRatio) - .toInt(), - fit: BoxFit.cover, - filterQuality: FilterQuality.low, - ), + if (!PlatformInfos.isWeb && matrixFile.filePath != null) + Image.file( + File(matrixFile.filePath!), + width: displayImageInfo.size.width, + height: displayImageInfo.size.height, + cacheHeight: + (displayImageInfo.size.height * devicePixelRatio) + .toInt(), + cacheWidth: + (displayImageInfo.size.width * devicePixelRatio) + .toInt(), + fit: BoxFit.cover, + filterQuality: FilterQuality.low, + ), + if (matrixFile.bytes?.isNotEmpty == true) + Image.memory( + matrixFile.bytes!, + width: displayImageInfo.size.width, + height: displayImageInfo.size.height, + cacheHeight: + (displayImageInfo.size.height * devicePixelRatio) + .toInt(), + cacheWidth: + (displayImageInfo.size.width * devicePixelRatio) + .toInt(), + fit: BoxFit.cover, + filterQuality: FilterQuality.none, + ), ], ), ), diff --git a/lib/pages/chat/events/images_builder/unencrypted_image_builder_web.dart b/lib/pages/chat/events/images_builder/unencrypted_image_builder_web.dart index c828696722..830381235d 100644 --- a/lib/pages/chat/events/images_builder/unencrypted_image_builder_web.dart +++ b/lib/pages/chat/events/images_builder/unencrypted_image_builder_web.dart @@ -1,4 +1,4 @@ -import 'package:fluffychat/pages/chat/events/images_builder/image_bubble.dart'; +import 'package:fluffychat/pages/chat/events/images_builder/image_placeholder.dart'; import 'package:fluffychat/pages/chat/events/message_content_style.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart'; import 'package:flutter/material.dart';