Revert "refactor: Move back to cached network image for better avatar performance"
This reverts commit d9ab6ad8b3
.
pull/998/head
parent
d9ab6ad8b3
commit
938e1a91ae
@ -1,69 +1,192 @@
|
|||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'package:cached_network_image/cached_network_image.dart';
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
|
|
||||||
|
import 'package:fluffychat/config/themes.dart';
|
||||||
|
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart';
|
||||||
import 'package:fluffychat/widgets/matrix.dart';
|
import 'package:fluffychat/widgets/matrix.dart';
|
||||||
|
|
||||||
class MxcImage extends StatelessWidget {
|
class MxcImage extends StatefulWidget {
|
||||||
final Uri? uri;
|
final Uri? uri;
|
||||||
|
final Event? event;
|
||||||
final double? width;
|
final double? width;
|
||||||
final double? height;
|
final double? height;
|
||||||
final BoxFit? fit;
|
final BoxFit? fit;
|
||||||
final bool isThumbnail;
|
final bool isThumbnail;
|
||||||
final bool animated;
|
final bool animated;
|
||||||
|
final Duration retryDuration;
|
||||||
|
final Duration animationDuration;
|
||||||
|
final Curve animationCurve;
|
||||||
final ThumbnailMethod thumbnailMethod;
|
final ThumbnailMethod thumbnailMethod;
|
||||||
final Widget Function(BuildContext context)? placeholder;
|
final Widget Function(BuildContext context)? placeholder;
|
||||||
final String? cacheKey;
|
final String? cacheKey;
|
||||||
|
|
||||||
const MxcImage({
|
const MxcImage({
|
||||||
this.uri,
|
this.uri,
|
||||||
|
this.event,
|
||||||
this.width,
|
this.width,
|
||||||
this.height,
|
this.height,
|
||||||
this.fit,
|
this.fit,
|
||||||
this.placeholder,
|
this.placeholder,
|
||||||
this.isThumbnail = true,
|
this.isThumbnail = true,
|
||||||
this.animated = false,
|
this.animated = false,
|
||||||
|
this.animationDuration = FluffyThemes.animationDuration,
|
||||||
|
this.retryDuration = const Duration(seconds: 2),
|
||||||
|
this.animationCurve = FluffyThemes.animationCurve,
|
||||||
this.thumbnailMethod = ThumbnailMethod.scale,
|
this.thumbnailMethod = ThumbnailMethod.scale,
|
||||||
this.cacheKey,
|
this.cacheKey,
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
State<MxcImage> createState() => _MxcImageState();
|
||||||
final uri = this.uri;
|
}
|
||||||
if (uri == null) {
|
|
||||||
return placeholder?.call(context) ?? const Placeholder();
|
class _MxcImageState extends State<MxcImage> {
|
||||||
}
|
static final Map<String, Uint8List> _imageDataCache = {};
|
||||||
|
Uint8List? _imageDataNoCache;
|
||||||
|
Uint8List? get _imageData {
|
||||||
|
final cacheKey = widget.cacheKey;
|
||||||
|
return cacheKey == null ? _imageDataNoCache : _imageDataCache[cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
set _imageData(Uint8List? data) {
|
||||||
|
if (data == null) return;
|
||||||
|
final cacheKey = widget.cacheKey;
|
||||||
|
cacheKey == null
|
||||||
|
? _imageDataNoCache = data
|
||||||
|
: _imageDataCache[cacheKey] = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool? _isCached;
|
||||||
|
|
||||||
|
Future<void> _load() async {
|
||||||
final client = Matrix.of(context).client;
|
final client = Matrix.of(context).client;
|
||||||
|
final uri = widget.uri;
|
||||||
|
final event = widget.event;
|
||||||
|
|
||||||
|
if (uri != null) {
|
||||||
|
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
|
||||||
|
final width = widget.width;
|
||||||
|
final realWidth = width == null ? null : width * devicePixelRatio;
|
||||||
|
final height = widget.height;
|
||||||
|
final realHeight = height == null ? null : height * devicePixelRatio;
|
||||||
|
|
||||||
|
final httpUri = widget.isThumbnail
|
||||||
|
? uri.getThumbnail(
|
||||||
|
client,
|
||||||
|
width: realWidth,
|
||||||
|
height: realHeight,
|
||||||
|
animated: widget.animated,
|
||||||
|
method: widget.thumbnailMethod,
|
||||||
|
)
|
||||||
|
: uri.getDownloadLink(client);
|
||||||
|
|
||||||
|
final storeKey = widget.isThumbnail ? httpUri : uri;
|
||||||
|
|
||||||
|
if (_isCached == null) {
|
||||||
|
final cachedData = await client.database?.getFile(storeKey);
|
||||||
|
if (cachedData != null) {
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() {
|
||||||
|
_imageData = cachedData;
|
||||||
|
_isCached = true;
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_isCached = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final response = await http.get(httpUri);
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
if (response.statusCode == 404) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw Exception();
|
||||||
|
}
|
||||||
|
final remoteData = response.bodyBytes;
|
||||||
|
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() {
|
||||||
|
_imageData = remoteData;
|
||||||
|
});
|
||||||
|
await client.database?.storeFile(storeKey, remoteData, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event != null) {
|
||||||
|
final data = await event.downloadAndDecryptAttachment(
|
||||||
|
getThumbnail: widget.isThumbnail,
|
||||||
|
);
|
||||||
|
if (data.detectFileType is MatrixImageFile) {
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() {
|
||||||
|
_imageData = data.bytes;
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _tryLoad(_) async {
|
||||||
|
if (_imageData != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await _load();
|
||||||
|
} catch (_) {
|
||||||
|
if (!mounted) return;
|
||||||
|
await Future.delayed(widget.retryDuration);
|
||||||
|
_tryLoad(_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback(_tryLoad);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget placeholder(BuildContext context) =>
|
||||||
|
widget.placeholder?.call(context) ??
|
||||||
|
Container(
|
||||||
|
width: widget.width,
|
||||||
|
height: widget.height,
|
||||||
|
alignment: Alignment.center,
|
||||||
|
child: const CircularProgressIndicator.adaptive(strokeWidth: 2),
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final data = _imageData;
|
||||||
|
final hasData = data != null && data.isNotEmpty;
|
||||||
|
|
||||||
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
|
return AnimatedCrossFade(
|
||||||
final width = this.width;
|
crossFadeState:
|
||||||
final realWidth = width == null ? null : width * devicePixelRatio;
|
hasData ? CrossFadeState.showSecond : CrossFadeState.showFirst,
|
||||||
final height = this.height;
|
duration: FluffyThemes.animationDuration,
|
||||||
final realHeight = height == null ? null : height * devicePixelRatio;
|
firstChild: placeholder(context),
|
||||||
|
secondChild: hasData
|
||||||
final imageUrl = isThumbnail
|
? Image.memory(
|
||||||
? uri.getThumbnail(
|
data,
|
||||||
client,
|
width: widget.width,
|
||||||
width: realWidth,
|
height: widget.height,
|
||||||
height: realHeight,
|
fit: widget.fit,
|
||||||
animated: animated,
|
filterQuality:
|
||||||
method: thumbnailMethod,
|
widget.isThumbnail ? FilterQuality.low : FilterQuality.medium,
|
||||||
)
|
errorBuilder: (context, __, ___) {
|
||||||
: uri.getDownloadLink(client);
|
_isCached = false;
|
||||||
|
_imageData = null;
|
||||||
return CachedNetworkImage(
|
WidgetsBinding.instance.addPostFrameCallback(_tryLoad);
|
||||||
imageUrl: imageUrl.toString(),
|
return placeholder(context);
|
||||||
width: width,
|
},
|
||||||
height: height,
|
)
|
||||||
fit: fit,
|
: SizedBox(
|
||||||
cacheKey: cacheKey,
|
width: widget.width,
|
||||||
filterQuality: isThumbnail ? FilterQuality.low : FilterQuality.medium,
|
height: widget.height,
|
||||||
errorWidget: placeholder == null
|
),
|
||||||
? null
|
|
||||||
: (context, __, ___) => placeholder!.call(context),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue