ente/lib/ui/thumbnail_widget.dart

116 lines
3.3 KiB
Dart
Raw Normal View History

2020-06-23 17:41:27 +00:00
import 'package:cached_network_image/cached_network_image.dart';
2020-03-28 18:18:27 +00:00
import 'package:flutter/material.dart';
2020-05-04 20:44:34 +00:00
import 'package:photos/core/cache/thumbnail_cache.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file.dart';
2020-05-12 16:47:02 +00:00
import 'package:logging/logging.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/core/constants.dart';
2020-06-20 10:03:45 +00:00
import 'package:photos/models/file_type.dart';
2020-06-23 17:41:27 +00:00
import 'package:photos/ui/loading_widget.dart';
2020-03-28 18:18:27 +00:00
2020-04-25 09:12:13 +00:00
class ThumbnailWidget extends StatefulWidget {
2020-06-19 23:03:26 +00:00
final File photo;
2020-03-28 18:18:27 +00:00
2020-04-25 09:12:13 +00:00
const ThumbnailWidget(
2020-04-11 21:29:07 +00:00
this.photo, {
2020-03-28 18:18:27 +00:00
Key key,
}) : super(key: key);
@override
2020-04-25 09:12:13 +00:00
_ThumbnailWidgetState createState() => _ThumbnailWidgetState();
2020-03-28 18:18:27 +00:00
}
2020-04-25 09:12:13 +00:00
class _ThumbnailWidgetState extends State<ThumbnailWidget> {
2020-05-12 16:47:02 +00:00
static final _logger = Logger("ThumbnailWidget");
static final Widget loadingWidget = Container(
alignment: Alignment.center,
color: Colors.grey[500],
);
2020-04-25 09:12:13 +00:00
2020-05-04 14:08:26 +00:00
bool _hasLoadedThumbnail = false;
2020-05-12 16:47:02 +00:00
bool _encounteredErrorLoadingThumbnail = false;
2020-04-25 10:28:22 +00:00
ImageProvider _imageProvider;
2020-03-28 18:18:27 +00:00
@override
Widget build(BuildContext context) {
2020-05-25 14:35:06 +00:00
if (widget.photo.localId == null) {
2020-06-23 17:41:27 +00:00
return _getNetworkImage();
2020-05-25 14:35:06 +00:00
}
2020-06-23 17:41:27 +00:00
_loadLocalImage(context);
2020-05-25 14:35:06 +00:00
if (_imageProvider != null) {
2020-06-20 10:03:45 +00:00
var image = Image(
2020-05-25 14:35:06 +00:00
image: _imageProvider,
fit: BoxFit.cover,
);
2020-06-20 10:03:45 +00:00
if (widget.photo.fileType == FileType.video) {
return Stack(
children: [
image,
Icon(Icons.play_circle_outline),
],
fit: StackFit.expand,
);
} else {
return image;
}
2020-05-25 14:35:06 +00:00
} else {
return loadingWidget;
}
}
void _loadLocalImage(BuildContext context) {
2020-05-12 16:47:02 +00:00
if (!_hasLoadedThumbnail && !_encounteredErrorLoadingThumbnail) {
2020-04-25 10:28:22 +00:00
final cachedSmallThumbnail =
ThumbnailLruCache.get(widget.photo, THUMBNAIL_SMALL_SIZE);
if (cachedSmallThumbnail != null) {
_imageProvider = Image.memory(cachedSmallThumbnail).image;
_hasLoadedThumbnail = true;
2020-05-04 14:08:26 +00:00
} else {
2020-06-13 16:44:16 +00:00
widget.photo.getAsset().then((asset) {
asset
.thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE)
.then((data) {
if (data != null && mounted) {
final imageProvider = Image.memory(data).image;
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_hasLoadedThumbnail = true;
});
}
});
}
ThumbnailLruCache.put(widget.photo, THUMBNAIL_SMALL_SIZE, data);
});
}).catchError((e) {
2020-05-12 16:47:02 +00:00
_logger.warning("Could not load image: ", e);
_encounteredErrorLoadingThumbnail = true;
2020-04-25 10:28:22 +00:00
});
}
}
2020-05-25 14:35:06 +00:00
}
2020-03-28 18:18:27 +00:00
2020-06-23 17:41:27 +00:00
Widget _getNetworkImage() {
2020-06-19 23:03:26 +00:00
final url = widget.photo.previewURL.isNotEmpty
2020-05-27 14:20:52 +00:00
? widget.photo.getThumbnailUrl()
: widget.photo.getRemoteUrl();
2020-06-23 17:41:27 +00:00
return CachedNetworkImage(
imageUrl: url,
placeholder: (context, url) => loadWidget,
errorWidget: (context, url, error) => Icon(Icons.error),
fit: BoxFit.cover,
);
2020-03-28 18:18:27 +00:00
}
2020-04-27 13:02:29 +00:00
@override
void didUpdateWidget(ThumbnailWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.photo.generatedId != oldWidget.photo.generatedId) {
setState(() {
2020-05-04 14:08:26 +00:00
_hasLoadedThumbnail = false;
2020-04-27 13:02:29 +00:00
_imageProvider = null;
});
}
}
2020-03-28 18:18:27 +00:00
}