Merge branch 'soru/fix-image-bubble-thumbnails' into 'main'

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

See merge request famedly/fluffychat!594
This commit is contained in:
Sorunome 2021-11-30 15:12:51 +00:00
commit 296af38b89

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,9 +310,22 @@ 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 FutureBuilder<bool>(
future: (() async {
return await DefaultCacheManager()
.getFileFromCache(thumbnailUrl) !=
null;
})(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return getPlaceholderWidget();
}
final effectiveUrl = snapshot.data == true
? thumbnailUrl
: thumbnailUrlNoAnimated;
return CachedNetworkImage( return CachedNetworkImage(
key: ValueKey(thumbnailUrl), key: ValueKey(effectiveUrl),
imageUrl: thumbnailUrl, imageUrl: effectiveUrl,
placeholder: (c, u) => getPlaceholderWidget(), placeholder: (c, u) => getPlaceholderWidget(),
imageBuilder: (context, imageProvider) => Image( imageBuilder: (context, imageProvider) => Image(
image: imageProvider, image: imageProvider,
@ -314,6 +333,8 @@ class _ImageBubbleState extends State<ImageBubble> {
fit: widget.fit, 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)