ente/lib/ui/thumbnail_widget.dart

207 lines
5.9 KiB
Dart
Raw Normal View History

2020-03-28 18:18:27 +00:00
import 'package:flutter/material.dart';
import 'package:photos/core/cache/image_cache.dart';
2020-05-04 20:44:34 +00:00
import 'package:photos/core/cache/thumbnail_cache.dart';
import 'package:photos/db/files_db.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';
import 'package:photos/repositories/file_repository.dart';
2020-07-29 07:57:25 +00:00
import 'package:photos/utils/file_util.dart';
2020-03-28 18:18:27 +00:00
2020-04-25 09:12:13 +00:00
class ThumbnailWidget extends StatefulWidget {
final File file;
final BoxFit fit;
2020-04-25 09:12:13 +00:00
const ThumbnailWidget(
this.file, {
2020-03-28 18:18:27 +00:00
Key key,
this.fit = BoxFit.cover,
2020-03-28 18:18:27 +00:00
}) : 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[900],
);
2020-04-25 09:12:13 +00:00
2020-05-04 14:08:26 +00:00
bool _hasLoadedThumbnail = false;
2020-08-13 21:33:31 +00:00
bool _isLoadingThumbnail = false;
2020-05-12 16:47:02 +00:00
bool _encounteredErrorLoadingThumbnail = false;
2020-04-25 10:28:22 +00:00
ImageProvider _imageProvider;
@override
void initState() {
super.initState();
}
@override
void dispose() {
removePendingGetThumbnailRequestIfAny(widget.file);
super.dispose();
}
2020-03-28 18:18:27 +00:00
@override
Widget build(BuildContext context) {
if (widget.file.localID == null) {
2020-08-13 21:33:31 +00:00
_loadNetworkImage();
} else {
_loadLocalImage(context);
2020-05-25 14:35:06 +00:00
}
2020-08-13 21:33:31 +00:00
var image;
if (_imageProvider != null) {
image = Image(
image: _imageProvider,
fit: widget.fit,
);
}
2020-06-23 18:50:10 +00:00
var content;
if (image != null) {
if (widget.file.fileType == FileType.video) {
2020-06-23 18:50:10 +00:00
content = Stack(
2020-06-20 10:03:45 +00:00
children: [
image,
2020-12-12 00:31:06 +00:00
Container(
height: 64,
child: Icon(
Icons.play_circle_outline,
size: 40,
color: Colors.white70,
),
),
2020-06-20 10:03:45 +00:00
],
fit: StackFit.expand,
);
} else {
2020-06-23 18:50:10 +00:00
content = image;
2020-06-20 10:03:45 +00:00
}
2020-05-25 14:35:06 +00:00
}
2020-06-23 18:50:10 +00:00
return Stack(
children: [
loadingWidget,
AnimatedOpacity(
opacity: content == null ? 0 : 1.0,
duration: Duration(milliseconds: 400),
child: content,
),
widget.file.uploadedFileID == null
? Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.only(right: 8, bottom: 4),
child: Icon(
Icons.cloud_off_outlined,
size: 18,
color: Colors.white.withOpacity(0.8),
),
),
)
: Container(),
2020-06-23 18:50:10 +00:00
],
fit: StackFit.expand,
);
2020-05-25 14:35:06 +00:00
}
void _loadLocalImage(BuildContext context) {
2020-08-13 21:33:31 +00:00
if (!_hasLoadedThumbnail &&
!_encounteredErrorLoadingThumbnail &&
!_isLoadingThumbnail) {
_isLoadingThumbnail = true;
2020-04-25 10:28:22 +00:00
final cachedSmallThumbnail =
ThumbnailLruCache.get(widget.file, THUMBNAIL_SMALL_SIZE);
2020-04-25 10:28:22 +00:00
if (cachedSmallThumbnail != null) {
_imageProvider = Image.memory(cachedSmallThumbnail).image;
_hasLoadedThumbnail = true;
2020-05-04 14:08:26 +00:00
} else {
widget.file.getAsset().then((asset) async {
if (asset == null || !(await asset.exists)) {
if (widget.file.uploadedFileID != null) {
widget.file.localID = null;
FilesDB.instance.update(widget.file);
_loadNetworkImage();
} else {
FilesDB.instance.deleteLocalFile(widget.file.localID);
FileRepository.instance.reloadFiles();
}
2020-07-29 07:57:25 +00:00
return;
}
2020-06-13 16:44:16 +00:00
asset
.thumbDataWithSize(
THUMBNAIL_SMALL_SIZE,
THUMBNAIL_SMALL_SIZE,
2021-03-01 18:42:06 +00:00
quality: THUMBNAIL_QUALITY,
)
2020-06-13 16:44:16 +00:00
.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.file, THUMBNAIL_SMALL_SIZE, data);
2020-06-13 16:44:16 +00:00
});
}).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-08-13 21:33:31 +00:00
void _loadNetworkImage() {
if (!_hasLoadedThumbnail &&
!_encounteredErrorLoadingThumbnail &&
!_isLoadingThumbnail) {
_isLoadingThumbnail = true;
final cachedThumbnail = ThumbnailFileLruCache.get(widget.file);
if (cachedThumbnail != null) {
_imageProvider = Image.file(cachedThumbnail).image;
_hasLoadedThumbnail = true;
return;
}
_getThumbnailFromServer();
}
2020-03-28 18:18:27 +00:00
}
2020-04-27 13:02:29 +00:00
void _getThumbnailFromServer() {
getThumbnailFromServer(widget.file).then((file) async {
if (mounted) {
final imageProvider = Image.file(file).image;
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_hasLoadedThumbnail = true;
});
}
}).catchError((e) {
_logger.severe("Could not load image " + widget.file.toString());
_encounteredErrorLoadingThumbnail = true;
});
}
});
}
2020-04-27 13:02:29 +00:00
@override
void didUpdateWidget(ThumbnailWidget oldWidget) {
super.didUpdateWidget(oldWidget);
2020-08-09 22:34:59 +00:00
if (widget.file.generatedID != oldWidget.file.generatedID) {
2020-04-27 13:02:29 +00:00
setState(() {
2020-05-04 14:08:26 +00:00
_hasLoadedThumbnail = false;
2020-08-13 21:33:31 +00:00
_isLoadingThumbnail = false;
_encounteredErrorLoadingThumbnail = false;
2020-04-27 13:02:29 +00:00
_imageProvider = null;
});
}
}
2020-03-28 18:18:27 +00:00
}