ente/lib/ui/thumbnail_widget.dart
2020-05-27 01:33:54 +05:30

110 lines
3.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:photos/core/cache/thumbnail_cache.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/models/photo.dart';
import 'package:logging/logging.dart';
import 'package:photos/core/constants.dart';
class ThumbnailWidget extends StatefulWidget {
final Photo photo;
const ThumbnailWidget(
this.photo, {
Key key,
}) : super(key: key);
@override
_ThumbnailWidgetState createState() => _ThumbnailWidgetState();
}
class _ThumbnailWidgetState extends State<ThumbnailWidget> {
static final _logger = Logger("ThumbnailWidget");
static final Widget loadingWidget = Container(
alignment: Alignment.center,
color: Colors.grey[500],
);
bool _hasLoadedThumbnail = false;
bool _encounteredErrorLoadingThumbnail = false;
ImageProvider _imageProvider;
@override
Widget build(BuildContext context) {
if (widget.photo.localId == null) {
_loadNetworkImage();
} else {
_loadLocalImage(context);
}
if (_imageProvider != null) {
return Image(
image: _imageProvider,
fit: BoxFit.cover,
);
} else {
return loadingWidget;
}
}
void _loadLocalImage(BuildContext context) {
if (!_hasLoadedThumbnail && !_encounteredErrorLoadingThumbnail) {
final cachedSmallThumbnail =
ThumbnailLruCache.get(widget.photo, THUMBNAIL_SMALL_SIZE);
if (cachedSmallThumbnail != null) {
final imageProvider = Image.memory(cachedSmallThumbnail).image;
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_hasLoadedThumbnail = true;
});
}
});
} else {
widget.photo
.getAsset()
.thumbDataWithSize(THUMBNAIL_SMALL_SIZE, THUMBNAIL_SMALL_SIZE)
.catchError((e) {
_logger.warning("Could not load image: ", e);
_encounteredErrorLoadingThumbnail = true;
}).then((data) {
if (mounted) {
setState(() {
if (data != null) {
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);
});
}
}
}
void _loadNetworkImage() {
_imageProvider = Image.network(Configuration.instance.getHttpEndpoint() +
"/" +
widget.photo.thumbnailPath ??
widget.photo.remotePath)
.image;
}
@override
void didUpdateWidget(ThumbnailWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.photo.generatedId != oldWidget.photo.generatedId) {
setState(() {
_hasLoadedThumbnail = false;
_imageProvider = null;
});
}
}
}