ente/lib/ui/zoomable_image.dart

177 lines
5.1 KiB
Dart
Raw Normal View History

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2020-06-17 12:38:18 +00:00
import 'package:logging/logging.dart';
2020-05-04 20:44:34 +00:00
import 'package:photos/core/cache/image_cache.dart';
import 'package:photos/core/cache/thumbnail_cache.dart';
import 'package:photos/core/cache/thumbnail_cache_manager.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/ui/loading_widget.dart';
import 'package:photo_view/photo_view.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/core/constants.dart';
import 'package:photos/utils/file_util.dart';
class ZoomableImage extends StatefulWidget {
2020-06-19 23:03:26 +00:00
final File photo;
final Function(bool) shouldDisableScroll;
final String tagPrefix;
final Decoration backgroundDecoration;
ZoomableImage(
this.photo, {
Key key,
this.shouldDisableScroll,
2020-07-21 10:25:19 +00:00
@required this.tagPrefix,
this.backgroundDecoration,
}) : super(key: key);
@override
_ZoomableImageState createState() => _ZoomableImageState();
}
2020-06-10 18:17:54 +00:00
class _ZoomableImageState extends State<ZoomableImage>
with SingleTickerProviderStateMixin {
2020-06-17 12:38:18 +00:00
final Logger _logger = Logger("ZoomableImage");
2020-08-13 00:52:05 +00:00
File _photo;
ImageProvider _imageProvider;
2020-05-04 15:35:23 +00:00
bool _loadedSmallThumbnail = false;
2020-06-10 00:04:22 +00:00
bool _loadingLargeThumbnail = false;
2020-05-04 15:35:23 +00:00
bool _loadedLargeThumbnail = false;
2020-06-10 00:04:22 +00:00
bool _loadingFinalImage = false;
bool _loadedFinalImage = false;
ValueChanged<PhotoViewScaleState> _scaleStateChangedCallback;
@override
void initState() {
_scaleStateChangedCallback = (value) {
if (widget.shouldDisableScroll != null) {
widget.shouldDisableScroll(value != PhotoViewScaleState.initial);
}
};
super.initState();
}
@override
Widget build(BuildContext context) {
2020-08-13 00:52:05 +00:00
_photo = widget.photo;
if (_photo.localID == null) {
2020-05-25 14:54:54 +00:00
_loadNetworkImage();
} else {
_loadLocalImage(context);
}
if (_imageProvider != null) {
return PhotoView(
imageProvider: _imageProvider,
scaleStateChangedCallback: _scaleStateChangedCallback,
minScale: PhotoViewComputedScale.contained,
gaplessPlayback: true,
2020-06-14 23:11:48 +00:00
heroAttributes: PhotoViewHeroAttributes(
2020-08-13 00:52:05 +00:00
tag: widget.tagPrefix + _photo.tag(),
2020-06-15 00:53:12 +00:00
),
backgroundDecoration: widget.backgroundDecoration,
2020-05-25 14:54:54 +00:00
);
} else {
return loadWidget;
}
}
void _loadNetworkImage() {
2020-08-13 21:33:31 +00:00
if (!_loadedSmallThumbnail && !_loadedFinalImage) {
_imageProvider = CachedNetworkImageProvider(
_photo.getThumbnailUrl(),
cacheManager: ThumbnailCacheManager(),
);
_loadedSmallThumbnail = true;
}
if (!_loadedFinalImage) {
getFileFromServer(_photo).then((file) {
2020-06-15 04:50:26 +00:00
_onFinalImageLoaded(
2020-08-13 01:07:44 +00:00
Image.file(
file,
2020-06-15 04:50:26 +00:00
gaplessPlayback: true,
).image,
context);
2020-08-13 01:07:44 +00:00
});
}
2020-05-25 14:54:54 +00:00
}
void _loadLocalImage(BuildContext context) {
2020-05-04 15:35:23 +00:00
if (!_loadedSmallThumbnail &&
!_loadedLargeThumbnail &&
!_loadedFinalImage) {
2020-04-25 10:28:22 +00:00
final cachedThumbnail =
2020-08-13 00:52:05 +00:00
ThumbnailLruCache.get(_photo, THUMBNAIL_SMALL_SIZE);
if (cachedThumbnail != null) {
_imageProvider = Image.memory(cachedThumbnail).image;
2020-05-04 15:35:23 +00:00
_loadedSmallThumbnail = true;
}
}
2020-06-10 00:04:22 +00:00
if (!_loadingLargeThumbnail &&
!_loadedLargeThumbnail &&
!_loadedFinalImage) {
_loadingLargeThumbnail = true;
2020-05-04 15:35:23 +00:00
final cachedThumbnail =
2020-08-13 00:52:05 +00:00
ThumbnailLruCache.get(_photo, THUMBNAIL_LARGE_SIZE);
2020-05-04 15:35:23 +00:00
if (cachedThumbnail != null) {
_onLargeThumbnailLoaded(Image.memory(cachedThumbnail).image, context);
} else {
2020-08-13 00:52:05 +00:00
_photo.getAsset().then((asset) {
2020-06-13 16:44:16 +00:00
asset
.thumbDataWithSize(THUMBNAIL_LARGE_SIZE, THUMBNAIL_LARGE_SIZE)
.then((data) {
_onLargeThumbnailLoaded(Image.memory(data).image, context);
2020-08-13 00:52:05 +00:00
ThumbnailLruCache.put(_photo, THUMBNAIL_LARGE_SIZE, data);
2020-06-13 16:44:16 +00:00
});
2020-05-04 15:35:23 +00:00
});
}
}
2020-06-10 00:04:22 +00:00
if (!_loadingFinalImage && !_loadedFinalImage) {
_loadingFinalImage = true;
2020-08-13 00:52:05 +00:00
final cachedFile = FileLruCache.get(_photo);
2020-05-05 20:58:43 +00:00
if (cachedFile != null) {
2020-06-17 12:38:18 +00:00
_onFinalImageLoaded(Image.file(cachedFile).image, context);
2020-04-24 20:59:11 +00:00
} else {
2020-08-13 00:52:05 +00:00
_photo.getAsset().then((asset) {
2020-06-13 16:44:16 +00:00
asset.file.then((file) {
if (mounted) {
2020-06-17 12:38:18 +00:00
_onFinalImageLoaded(Image.file(file).image, context);
2020-08-13 00:52:05 +00:00
FileLruCache.put(_photo, file);
2020-06-13 16:44:16 +00:00
}
});
2020-04-24 20:59:11 +00:00
});
}
}
}
2020-04-24 20:59:11 +00:00
2020-05-04 15:35:23 +00:00
void _onLargeThumbnailLoaded(
ImageProvider imageProvider, BuildContext context) {
if (mounted && !_loadedFinalImage) {
2020-06-10 00:04:22 +00:00
precacheImage(imageProvider, context).then((value) {
2020-06-17 12:38:18 +00:00
if (mounted && !_loadedFinalImage) {
2020-06-10 00:04:22 +00:00
setState(() {
_imageProvider = imageProvider;
_loadedLargeThumbnail = true;
});
}
});
}
2020-05-04 15:35:23 +00:00
}
void _onFinalImageLoaded(ImageProvider imageProvider, BuildContext context) {
2020-06-14 23:11:48 +00:00
if (mounted) {
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_loadedFinalImage = true;
});
}
});
}
2020-04-24 20:59:11 +00:00
}
}