Skip to content

Commit

Permalink
TW-1650: fix memory overuse in avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed May 13, 2024
1 parent 6da6a67 commit c0e6e7f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
46 changes: 24 additions & 22 deletions lib/pages/chat/events/image_bubble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,28 @@ class ImageBubble extends StatelessWidget {
borderRadius: rounded
? MessageContentStyle.borderRadiusBubble
: BorderRadius.zero,
child: Stack(
alignment: Alignment.center,
children: [
SizedBox(
width: bubbleWidth,
height: bubbleHeight,
child: const BlurHash(hash: MessageContentStyle.defaultBlurHash),
),
PlatformInfos.isWeb &&
event.isEventEncrypted(isThumbnail: thumbnailOnly)
? ImageBuilderWeb(
event: event,
isThumbnail: thumbnailOnly,
width: width,
height: height,
onTapPreview: onTapPreview,
onTapSelectMode: onTapSelectMode,
fit: fit,
)
: MxcImage(
child: (PlatformInfos.isWeb &&
!event.isEventEncrypted(isThumbnail: thumbnailOnly))
? UnencryptedImageBuilderWeb(
event: event,
isThumbnail: thumbnailOnly,
width: width,
height: height,
onTapPreview: onTapPreview,
onTapSelectMode: onTapSelectMode,
fit: fit,
)
: Stack(
alignment: Alignment.center,
children: [
SizedBox(
width: bubbleWidth,
height: bubbleHeight,
child: const BlurHash(
hash: MessageContentStyle.defaultBlurHash,
),
),
MxcImage(
event: event,
width: width,
height: height,
Expand All @@ -107,8 +109,8 @@ class ImageBubble extends StatelessWidget {
cacheMap: thumbnailCacheMap,
noResize: noResizeThumbnail,
),
],
),
],
),
),
);
}
Expand Down
13 changes: 10 additions & 3 deletions lib/pages/chat/events/image_builder_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_blurhash/flutter_blurhash.dart';
import 'package:matrix/matrix.dart';

class ImageBuilderWeb extends StatelessWidget {
class UnencryptedImageBuilderWeb extends StatelessWidget {
final Event event;

final bool isThumbnail;
Expand All @@ -27,10 +27,10 @@ class ImageBuilderWeb extends StatelessWidget {

final void Function()? onTapSelectMode;

const ImageBuilderWeb({
const UnencryptedImageBuilderWeb({
super.key,
required this.event,
this.isThumbnail = false,
this.isThumbnail = true,
this.width = 256,
this.height = 300,
this.fit = BoxFit.cover,
Expand Down Expand Up @@ -131,6 +131,13 @@ class UnencryptedImageWidget extends StatelessWidget {
fit: fit,
width: width,
height: height,
cacheWidth: (width * MediaQuery.of(context).devicePixelRatio).toInt(),
filterQuality: FilterQuality.none,
errorBuilder: (context, error, stackTrace) {
return BlurHash(
hash: event.blurHash ?? MessageContentStyle.defaultBlurHash,
);
},
loadingBuilder: (context, child, loadingProgress) {
if (loadingProgress == null) {
return child;
Expand Down
29 changes: 17 additions & 12 deletions lib/widgets/avatar/avatar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Avatar extends StatelessWidget {

@override
Widget build(BuildContext context) {
final fallbackLetters = name?.getShortcutNameForAvatar() ?? '@';
return InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(size / 2),
Expand All @@ -44,23 +43,29 @@ class Avatar extends StatelessWidget {
fit: BoxFit.cover,
width: size,
height: size,
cacheWidth: (size * MediaQuery.of(context).devicePixelRatio).toInt(),
cacheKey: mxContent.toString(),
placeholder: (context) => RoundAvatar(
size: size,
text: fallbackLetters,
boxShadows: boxShadows,
textStyle: TextStyle(
fontSize: fontSize,
color: textColor ?? AvatarStyle.defaultTextColor(_havePicture),
fontFamily: AvatarStyle.fontFamily,
fontWeight: AvatarStyle.fontWeight,
),
),
placeholder: (context) => _fallbackAvatar(),
),
),
);
}

Widget _fallbackAvatar() {
final fallbackLetters = name?.getShortcutNameForAvatar() ?? '@';
return RoundAvatar(
size: size,
text: fallbackLetters,
boxShadows: boxShadows,
textStyle: TextStyle(
fontSize: fontSize,
color: textColor ?? AvatarStyle.defaultTextColor(_havePicture),
fontFamily: AvatarStyle.fontFamily,
fontWeight: AvatarStyle.fontWeight,
),
);
}

bool get _havePicture {
return mxContent == null ||
mxContent.toString().isEmpty ||
Expand Down
22 changes: 16 additions & 6 deletions lib/widgets/mxc_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class MxcImage extends StatefulWidget {

final VoidCallback? closeRightColumn;

final int? cacheWidth;

const MxcImage({
this.uri,
this.event,
Expand All @@ -65,6 +67,7 @@ class MxcImage extends StatefulWidget {
this.cacheMap,
this.noResize = false,
this.closeRightColumn,
this.cacheWidth,
Key? key,
}) : super(key: key);

Expand Down Expand Up @@ -303,6 +306,7 @@ class _MxcImageState extends State<MxcImage> {
height: widget.height,
fit: widget.fit,
needResize: needResize,
cacheWidth: widget.cacheWidth,
imageErrorWidgetBuilder: (context, __, ___) {
_isCached = false;
_imageData = null;
Expand All @@ -322,6 +326,7 @@ class _ImageWidget extends StatelessWidget {
final bool needResize;
final BoxFit? fit;
final ImageErrorWidgetBuilder imageErrorWidgetBuilder;
final int? cacheWidth;

const _ImageWidget({
this.filePath,
Expand All @@ -331,6 +336,7 @@ class _ImageWidget extends StatelessWidget {
required this.needResize,
this.fit,
required this.imageErrorWidgetBuilder,
this.cacheWidth,
});

@override
Expand All @@ -341,9 +347,11 @@ class _ImageWidget extends StatelessWidget {
File(filePath!),
width: width,
height: height,
cacheWidth: (width != null && needResize)
? (width! * devicePixelRatio).toInt()
: null,
cacheWidth: cacheWidth != null
? cacheWidth!
: (width != null && needResize)
? (width! * devicePixelRatio).toInt()
: null,
cacheHeight: (height != null && needResize)
? (height! * devicePixelRatio).toInt()
: null,
Expand All @@ -356,9 +364,11 @@ class _ImageWidget extends StatelessWidget {
data!,
width: width,
height: height,
cacheWidth: (width != null && needResize)
? (width! * devicePixelRatio).toInt()
: null,
cacheWidth: cacheWidth != null
? cacheWidth!
: (width != null && needResize)
? (width! * devicePixelRatio).toInt()
: null,
cacheHeight: (height != null && needResize)
? (height! * devicePixelRatio).toInt()
: null,
Expand Down

0 comments on commit c0e6e7f

Please sign in to comment.