-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Try to display image #2771
- Loading branch information
Showing
5 changed files
with
208 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export 'cubit/chat_room_cubit.dart'; | ||
export 'matrix_chat/matrix_chat.dart'; | ||
export 'view/chat_room_view.dart'; | ||
export 'widget/mxc_image.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:matrix/matrix.dart'; | ||
|
||
class MxcImage extends StatefulWidget { | ||
const MxcImage({ | ||
required this.client, | ||
this.uri, | ||
this.event, | ||
this.width, | ||
this.height, | ||
this.fit, | ||
this.placeholder, | ||
this.isThumbnail = true, | ||
this.animated = false, | ||
this.animationDuration = const Duration(milliseconds: 250), | ||
this.retryDuration = const Duration(seconds: 2), | ||
this.animationCurve = Curves.easeInOut, | ||
this.thumbnailMethod = ThumbnailMethod.scale, | ||
this.cacheKey, | ||
super.key, | ||
}); | ||
|
||
final Uri? uri; | ||
final Event? event; | ||
final double? width; | ||
final double? height; | ||
final BoxFit? fit; | ||
final bool isThumbnail; | ||
final bool animated; | ||
final Duration retryDuration; | ||
final Duration animationDuration; | ||
final Curve animationCurve; | ||
final ThumbnailMethod thumbnailMethod; | ||
final Widget Function(BuildContext context)? placeholder; | ||
final String? cacheKey; | ||
final Client client; | ||
|
||
@override | ||
State<MxcImage> createState() => _MxcImageState(); | ||
} | ||
|
||
class _MxcImageState extends State<MxcImage> { | ||
static final Map<String, Uint8List> _imageDataCache = {}; | ||
Uint8List? _imageDataNoCache; | ||
|
||
Uint8List? get _imageData => widget.cacheKey == null | ||
? _imageDataNoCache | ||
: _imageDataCache[widget.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 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( | ||
widget.client, | ||
width: realWidth, | ||
height: realHeight, | ||
animated: widget.animated, | ||
method: widget.thumbnailMethod, | ||
) | ||
: uri.getDownloadLink(widget.client); | ||
|
||
final storeKey = widget.isThumbnail ? httpUri : uri; | ||
|
||
if (_isCached == null) { | ||
final cachedData = await widget.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 widget.client.database?.storeFile(storeKey, remoteData, 0); | ||
} | ||
|
||
if (event != null) { | ||
final data = await event.downloadAndDecryptAttachment( | ||
getThumbnail: widget.isThumbnail, | ||
); | ||
if (data.msgType == MessageTypes.Image) { | ||
if (!mounted) return; | ||
setState(() { | ||
_imageData = data.bytes; | ||
}); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
void _tryLoad(_) async { | ||
if (_imageData != null) { | ||
return; | ||
} | ||
try { | ||
await _load(); | ||
} catch (_) { | ||
if (!mounted) return; | ||
await Future<void>.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; | ||
|
||
return AnimatedCrossFade( | ||
crossFadeState: | ||
hasData ? CrossFadeState.showSecond : CrossFadeState.showFirst, | ||
duration: const Duration(milliseconds: 250), | ||
firstChild: placeholder(context), | ||
secondChild: hasData | ||
? Image.memory( | ||
data, | ||
width: widget.width, | ||
height: widget.height, | ||
fit: widget.fit, | ||
filterQuality: FilterQuality.none, | ||
errorBuilder: (context, __, s) { | ||
_isCached = false; | ||
_imageData = null; | ||
WidgetsBinding.instance.addPostFrameCallback(_tryLoad); | ||
return placeholder(context); | ||
}, | ||
) | ||
: SizedBox( | ||
width: widget.width, | ||
height: widget.height, | ||
), | ||
); | ||
} | ||
} |