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-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");
|
2020-04-13 17:53:03 +00:00
|
|
|
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) {
|
|
|
|
_loadNetworkImage();
|
|
|
|
} else {
|
|
|
|
_loadLocalImage(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_imageProvider != null) {
|
|
|
|
return Image(
|
|
|
|
image: _imageProvider,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
);
|
|
|
|
} 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) {
|
2020-06-06 10:23:36 +00:00
|
|
|
_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-05-25 14:35:06 +00:00
|
|
|
void _loadNetworkImage() {
|
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();
|
|
|
|
_imageProvider = Image.network(url).image;
|
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
|
|
|
}
|