Skip to content

Commit

Permalink
fixup! TW-1650: reorganize image_builder folder
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed May 8, 2024
1 parent dbc66fb commit eef4d7b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 63 deletions.
56 changes: 6 additions & 50 deletions lib/pages/chat/events/images_builder/image_bubble.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<EventId, ImageData>? thumbnailCacheMap;
Expand All @@ -42,6 +45,7 @@ class ImageBubble extends StatelessWidget {
this.thumbnailCacheMap,
this.noResizeThumbnail = false,
this.isPreview = true,
this.imageData,
Key? key,
}) : super(key: key);

Expand Down Expand Up @@ -108,59 +112,11 @@ class ImageBubble extends StatelessWidget {
cacheKey: thumbnailCacheKey,
cacheMap: thumbnailCacheMap,
noResize: noResizeThumbnail,
imageData: imageData,
),
],
),
),
);
}
}

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,
),
),
);
}
}
54 changes: 54 additions & 0 deletions lib/pages/chat/events/images_builder/image_placeholder.dart
Original file line number Diff line number Diff line change
@@ -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,
),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 29 additions & 12 deletions lib/pages/chat/events/images_builder/sending_image_info_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit eef4d7b

Please sign in to comment.