fix: Thumbnails in the image bubble not always showing a cached thumbnail

When in the timeline an image bubble with animated=false was displayed and
you tapped on an image, it would attempt to show the thumbnail with animated=true.
This thumbnail, of course, was not cached, thus not showing the cached image, and
actually downloading the thumbnail multiple times. This PR fixes that by
first checking if the animated=false thumbnail is cached, and if so, display
that while the final image is loading.
This commit is contained in:
Sorunome 2021-11-30 14:40:03 +01:00
parent 745d94f46f
commit e7b4c6f848
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_blurhash/flutter_blurhash.dart'; import 'package:flutter_blurhash/flutter_blurhash.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:lottie/lottie.dart'; import 'package:lottie/lottie.dart';
@ -48,6 +49,8 @@ class ImageBubble extends StatefulWidget {
class _ImageBubbleState extends State<ImageBubble> { class _ImageBubbleState extends State<ImageBubble> {
// for plaintext: holds the http URL for the thumbnail // for plaintext: holds the http URL for the thumbnail
String thumbnailUrl; String thumbnailUrl;
// for plaintext. holds the http URL for the thumbnial, without the animated flag
String thumbnailUrlNoAnimated;
// for plaintext: holds the http URL of the original // for plaintext: holds the http URL of the original
String attachmentUrl; String attachmentUrl;
MatrixFile _file; MatrixFile _file;
@ -166,6 +169,9 @@ class _ImageBubbleState extends State<ImageBubble> {
thumbnailUrl = widget.event thumbnailUrl = widget.event
.getAttachmentUrl(getThumbnail: true, animated: widget.animated) .getAttachmentUrl(getThumbnail: true, animated: widget.animated)
?.toString(); ?.toString();
thumbnailUrlNoAnimated = widget.event
.getAttachmentUrl(getThumbnail: true, animated: false)
?.toString();
attachmentUrl = attachmentUrl =
widget.event.getAttachmentUrl(animated: widget.animated)?.toString(); widget.event.getAttachmentUrl(animated: widget.animated)?.toString();
if (thumbnailUrl == null) { if (thumbnailUrl == null) {
@ -304,15 +310,30 @@ class _ImageBubbleState extends State<ImageBubble> {
displayUrl != thumbnailUrl && displayUrl != thumbnailUrl &&
displayUrl == attachmentUrl) { displayUrl == attachmentUrl) {
// we have to display the thumbnail while loading // we have to display the thumbnail while loading
return CachedNetworkImage( return FutureBuilder<bool>(
key: ValueKey(thumbnailUrl), future: (() async {
imageUrl: thumbnailUrl, return await DefaultCacheManager()
placeholder: (c, u) => getPlaceholderWidget(), .getFileFromCache(thumbnailUrl) !=
imageBuilder: (context, imageProvider) => Image( null;
image: imageProvider, })(),
frameBuilder: frameBuilder, builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
fit: widget.fit, if (!snapshot.hasData) {
), return getPlaceholderWidget();
}
final effectiveUrl = snapshot.data == true
? thumbnailUrl
: thumbnailUrlNoAnimated;
return CachedNetworkImage(
key: ValueKey(effectiveUrl),
imageUrl: effectiveUrl,
placeholder: (c, u) => getPlaceholderWidget(),
imageBuilder: (context, imageProvider) => Image(
image: imageProvider,
frameBuilder: frameBuilder,
fit: widget.fit,
),
);
},
); );
} }
return getPlaceholderWidget(); return getPlaceholderWidget();
@ -335,6 +356,12 @@ class _ImageBubbleState extends State<ImageBubble> {
useThumbnailMxcUrl: true, useThumbnailMxcUrl: true,
animated: widget.animated) animated: widget.animated)
?.toString(); ?.toString();
thumbnailUrlNoAnimated = widget.event
.getAttachmentUrl(
getThumbnail: true,
useThumbnailMxcUrl: true,
animated: false)
?.toString();
attachmentUrl = widget.event attachmentUrl = widget.event
.getAttachmentUrl( .getAttachmentUrl(
useThumbnailMxcUrl: true, animated: widget.animated) useThumbnailMxcUrl: true, animated: widget.animated)