1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 04:49:21 +00:00

Improve management of cache key

This commit is contained in:
Pierre HUBERT 2022-03-12 10:57:11 +01:00
parent 2a50190e94
commit 2ee4590364
4 changed files with 45 additions and 24 deletions

View File

@ -10,8 +10,9 @@ import 'package:url_launcher/url_launcher.dart';
class FullScreenImageRoute extends StatefulWidget { class FullScreenImageRoute extends StatefulWidget {
final String url; final String url;
final String? cacheKey;
FullScreenImageRoute(this.url); FullScreenImageRoute({required this.url, this.cacheKey});
@override @override
_FullScreenImageRouteState createState() => _FullScreenImageRouteState(); _FullScreenImageRouteState createState() => _FullScreenImageRouteState();
@ -28,7 +29,12 @@ class _FullScreenImageRouteState extends State<FullScreenImageRoute> {
icon: Icon(Icons.launch), onPressed: () => launch(widget.url)) icon: Icon(Icons.launch), onPressed: () => launch(widget.url))
], ],
), ),
body: PhotoView(imageProvider: CachedNetworkImageProvider(widget.url)), body: PhotoView(
loadingBuilder: (c, i) => Center(child: CircularProgressIndicator()),
imageProvider: CachedNetworkImageProvider(
widget.url,
cacheKey: widget.cacheKey,
)),
); );
} }
} }

View File

@ -31,6 +31,10 @@ class ConversationFileWidget extends StatefulWidget {
class _ConversationFileWidgetState extends State<ConversationFileWidget> { class _ConversationFileWidgetState extends State<ConversationFileWidget> {
ConversationMessageFile get file => widget.file; ConversationMessageFile get file => widget.file;
String get _thumbCacheKey => "conv-msg-thumb-${widget.messageID}";
String get _fileCacheKey => "conv-msg-file-${widget.messageID}";
@override @override
Widget build(BuildContext context) => Stack( Widget build(BuildContext context) => Stack(
children: [ children: [
@ -43,7 +47,7 @@ class _ConversationFileWidgetState extends State<ConversationFileWidget> {
opacity: 0.8, opacity: 0.8,
child: CachedNetworkImage( child: CachedNetworkImage(
imageUrl: file.thumbnail!, imageUrl: file.thumbnail!,
cacheKey: "conv-file-${widget.messageID}", cacheKey: _thumbCacheKey,
width: _AreaWidth, width: _AreaWidth,
height: _AreaHeight, height: _AreaHeight,
fit: BoxFit.cover, fit: BoxFit.cover,
@ -64,7 +68,9 @@ class _ConversationFileWidgetState extends State<ConversationFileWidget> {
return Center( return Center(
child: NetworkImageWidget( child: NetworkImageWidget(
url: file.url!, url: file.url!,
cacheKey: _fileCacheKey,
thumbnailURL: file.thumbnail, thumbnailURL: file.thumbnail,
thumbnailCacheKey: _thumbCacheKey,
allowFullScreen: true, allowFullScreen: true,
), ),
); );

View File

@ -10,7 +10,9 @@ import 'package:flutter/material.dart';
class NetworkImageWidget extends StatelessWidget { class NetworkImageWidget extends StatelessWidget {
final String url; final String url;
final String? cacheKey;
final String? thumbnailURL; final String? thumbnailURL;
final String? thumbnailCacheKey;
final bool allowFullScreen; final bool allowFullScreen;
final bool roundedEdges; final bool roundedEdges;
final double? width; final double? width;
@ -20,7 +22,9 @@ class NetworkImageWidget extends StatelessWidget {
const NetworkImageWidget({ const NetworkImageWidget({
Key? key, Key? key,
required this.url, required this.url,
this.cacheKey,
this.thumbnailURL, this.thumbnailURL,
this.thumbnailCacheKey,
this.allowFullScreen = false, this.allowFullScreen = false,
this.width, this.width,
this.height, this.height,
@ -38,11 +42,12 @@ class NetworkImageWidget extends StatelessWidget {
child: InkWell( child: InkWell(
onTap: allowFullScreen onTap: allowFullScreen
? () { ? () {
showImageFullScreen(context, url); showImageFullScreen(context, url, cacheKey: cacheKey);
} }
: null, : null,
child: CachedNetworkImage( child: CachedNetworkImage(
imageUrl: thumbnailURL ?? url, imageUrl: thumbnailURL ?? url,
cacheKey: thumbnailURL != null ? thumbnailCacheKey : cacheKey,
width: width, width: width,
height: height, height: height,
fit: BoxFit.cover, fit: BoxFit.cover,

View File

@ -74,9 +74,13 @@ Widget buildErrorCard(String? message,
} }
/// Show an image with a given [url] in full screen /// Show an image with a given [url] in full screen
void showImageFullScreen(BuildContext context, String? url) { void showImageFullScreen(BuildContext context, String? url,
{String? cacheKey}) {
Navigator.of(context).push(MaterialPageRoute(builder: (c) { Navigator.of(context).push(MaterialPageRoute(builder: (c) {
return FullScreenImageRoute(url!); return FullScreenImageRoute(
url: url!,
cacheKey: cacheKey,
);
})); }));
} }