ente/lib/ui/viewer/file/zoomable_image.dart

298 lines
9.3 KiB
Dart
Raw Normal View History

import 'dart:async';
2022-08-25 09:40:53 +00:00
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2020-06-17 12:38:18 +00:00
import 'package:logging/logging.dart';
2021-07-21 20:47:43 +00:00
import 'package:photo_view/photo_view.dart';
2023-01-11 04:39:33 +00:00
import 'package:photos/core/cache/thumbnail_in_memory_cache.dart';
import "package:photos/core/configuration.dart";
2021-07-21 20:47:43 +00:00
import 'package:photos/core/constants.dart';
2021-04-21 13:09:18 +00:00
import 'package:photos/core/event_bus.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/events/files_updated_event.dart';
2021-04-21 13:09:18 +00:00
import 'package:photos/events/local_photos_updated_event.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file.dart';
import "package:photos/models/metadata/file_magic.dart";
import "package:photos/services/file_magic_service.dart";
import 'package:photos/ui/common/loading_widget.dart';
import 'package:photos/utils/file_util.dart';
import 'package:photos/utils/image_util.dart';
2021-05-02 20:52:56 +00:00
import 'package:photos/utils/thumbnail_util.dart';
class ZoomableImage extends StatefulWidget {
2023-08-24 16:56:24 +00:00
final EnteFile photo;
final Function(bool)? shouldDisableScroll;
final String? tagPrefix;
final Decoration? backgroundDecoration;
2023-05-26 09:23:40 +00:00
final bool shouldCover;
const ZoomableImage(
this.photo, {
Key? key,
this.shouldDisableScroll,
required this.tagPrefix,
this.backgroundDecoration,
2023-05-26 09:23:40 +00:00
this.shouldCover = false,
}) : super(key: key);
@override
2022-07-03 09:45:00 +00:00
State<ZoomableImage> createState() => _ZoomableImageState();
}
2022-07-03 09:49:33 +00:00
class _ZoomableImageState extends State<ZoomableImage>
with SingleTickerProviderStateMixin {
late Logger _logger;
2023-08-24 16:56:24 +00:00
late EnteFile _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;
2022-03-14 11:44:18 +00:00
bool _isZooming = false;
PhotoViewController _photoViewController = PhotoViewController();
int? _thumbnailWidth;
late int _currentUserID;
@override
void initState() {
_photo = widget.photo;
_logger = Logger("ZoomableImage_" + _photo.tag);
2022-06-26 20:22:07 +00:00
debugPrint('initState for ${_photo.toString()}');
_scaleStateChangedCallback = (value) {
if (widget.shouldDisableScroll != null) {
widget.shouldDisableScroll!(value != PhotoViewScaleState.initial);
}
2022-03-20 08:55:44 +00:00
_isZooming = value != PhotoViewScaleState.initial;
2022-06-26 20:22:07 +00:00
debugPrint("isZooming = $_isZooming, currentState $value");
2022-03-20 08:55:44 +00:00
// _logger.info('is reakky zooming $_isZooming with state $value');
};
_currentUserID = Configuration.instance.getUserID()!;
super.initState();
}
@override
void dispose() {
_photoViewController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
2023-01-11 08:39:23 +00:00
if (_photo.isRemoteFile) {
2020-05-25 14:54:54 +00:00
_loadNetworkImage();
} else {
_loadLocalImage(context);
}
2022-03-14 11:44:18 +00:00
Widget content;
2020-05-25 14:54:54 +00:00
if (_imageProvider != null) {
content = PhotoViewGestureDetectorScope(
axis: Axis.vertical,
child: PhotoView(
imageProvider: _imageProvider,
controller: _photoViewController,
scaleStateChangedCallback: _scaleStateChangedCallback,
2023-05-26 09:23:40 +00:00
minScale: widget.shouldCover
? PhotoViewComputedScale.covered
: PhotoViewComputedScale.contained,
gaplessPlayback: true,
heroAttributes: PhotoViewHeroAttributes(
2023-01-11 08:39:23 +00:00
tag: widget.tagPrefix! + _photo.tag,
),
backgroundDecoration: widget.backgroundDecoration as BoxDecoration?,
2020-06-15 00:53:12 +00:00
),
2020-05-25 14:54:54 +00:00
);
} else {
content = const EnteLoadingWidget();
2020-05-25 14:54:54 +00:00
}
2022-03-20 09:33:46 +00:00
final GestureDragUpdateCallback? verticalDragCallback = _isZooming
2022-03-20 09:33:46 +00:00
? null
: (d) => {
if (!_isZooming && d.delta.dy > dragSensitivity)
2023-08-19 11:39:56 +00:00
{Navigator.of(context).pop()},
2022-03-20 09:33:46 +00:00
};
return GestureDetector(
onVerticalDragUpdate: verticalDragCallback,
2022-07-03 09:45:00 +00:00
child: content,
2022-03-20 09:33:46 +00:00
);
2020-05-25 14:54:54 +00:00
}
void _loadNetworkImage() {
2020-08-13 21:33:31 +00:00
if (!_loadedSmallThumbnail && !_loadedFinalImage) {
2023-01-12 04:40:08 +00:00
final cachedThumbnail = ThumbnailInMemoryLruCache.get(_photo);
if (cachedThumbnail != null) {
_imageProvider = Image.memory(cachedThumbnail).image;
_loadedSmallThumbnail = true;
_captureThumbnailDimensions(_imageProvider!);
} else {
2023-01-11 08:39:23 +00:00
getThumbnailFromServer(_photo).then((file) {
final imageProvider = Image.memory(file).image;
if (mounted) {
precacheImage(imageProvider, context).then((value) {
if (mounted) {
setState(() {
_imageProvider = imageProvider;
_loadedSmallThumbnail = true;
_captureThumbnailDimensions(_imageProvider!);
});
}
}).catchError((e) {
_logger.severe("Could not load image " + _photo.toString());
_loadedSmallThumbnail = true;
});
}
});
}
}
2023-01-18 06:35:57 +00:00
if (!_loadedFinalImage && !_loadingFinalImage) {
_loadingFinalImage = true;
2023-01-11 08:39:23 +00:00
getFileFromServer(_photo).then((file) {
2022-06-11 08:23:52 +00:00
_onFinalImageLoaded(
Image.file(
file!,
2022-06-11 08:23:52 +00:00
gaplessPlayback: true,
).image,
);
2020-08-13 01:07:44 +00:00
});
}
2020-05-25 14:54:54 +00:00
}
void _loadLocalImage(BuildContext context) {
2022-07-03 09:49:33 +00:00
if (!_loadedSmallThumbnail &&
!_loadedLargeThumbnail &&
!_loadedFinalImage) {
final cachedThumbnail =
2023-01-12 04:40:08 +00:00
ThumbnailInMemoryLruCache.get(_photo, thumbnailSmallSize);
if (cachedThumbnail != null) {
_imageProvider = Image.memory(cachedThumbnail).image;
2020-05-04 15:35:23 +00:00
_loadedSmallThumbnail = true;
}
}
2022-07-03 09:49:33 +00:00
if (!_loadingLargeThumbnail &&
!_loadedLargeThumbnail &&
!_loadedFinalImage) {
2020-06-10 00:04:22 +00:00
_loadingLargeThumbnail = true;
2023-01-11 08:39:23 +00:00
getThumbnailFromLocal(_photo, size: thumbnailLargeSize, quality: 100)
.then((cachedThumbnail) {
2021-10-01 20:45:40 +00:00
if (cachedThumbnail != null) {
_onLargeThumbnailLoaded(Image.memory(cachedThumbnail).image, context);
}
});
}
2020-06-10 00:04:22 +00:00
if (!_loadingFinalImage && !_loadedFinalImage) {
_loadingFinalImage = true;
2022-08-25 09:37:36 +00:00
getFile(
2023-01-11 08:39:23 +00:00
_photo,
2022-08-25 09:40:53 +00:00
isOrigin: Platform.isIOS &&
_isGIF(), // since on iOS GIFs playback only when origin-files are loaded
2022-08-25 09:37:36 +00:00
).then((file) {
if (file != null && file.existsSync()) {
_onFinalImageLoaded(Image.file(file).image);
} else {
_logger.info("File was deleted " + _photo.toString());
2023-01-11 08:39:23 +00:00
if (_photo.uploadedFileID != null) {
_photo.localID = null;
FilesDB.instance.update(_photo);
_loadNetworkImage();
} else {
2023-01-11 08:39:23 +00:00
FilesDB.instance.deleteLocalFile(_photo);
2022-06-11 08:23:52 +00:00
Bus.instance.fire(
LocalPhotosUpdatedEvent(
[_photo],
type: EventType.deletedFromDevice,
2022-11-11 13:09:22 +00:00
source: "zoomPreview",
2022-06-11 08:23:52 +00:00
),
);
}
}
});
}
}
2020-04-24 20:59:11 +00:00
2020-05-04 15:35:23 +00:00
void _onLargeThumbnailLoaded(
2022-06-11 08:23:52 +00:00
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
}
2021-04-05 13:02:25 +00:00
void _onFinalImageLoaded(ImageProvider imageProvider) {
2020-06-14 23:11:48 +00:00
if (mounted) {
precacheImage(imageProvider, context).then((value) async {
2020-06-14 23:11:48 +00:00
if (mounted) {
await _updatePhotoViewController(imageProvider);
2020-06-14 23:11:48 +00:00
setState(() {
_imageProvider = imageProvider;
_loadedFinalImage = true;
_logger.info("Final image loaded");
2020-06-14 23:11:48 +00:00
});
}
});
}
2020-04-24 20:59:11 +00:00
}
2022-08-25 09:37:36 +00:00
Future<void> _captureThumbnailDimensions(ImageProvider imageProvider) async {
final imageInfo = await getImageInfo(imageProvider);
_thumbnailWidth = imageInfo.image.width;
}
Future<void> _updatePhotoViewController(ImageProvider imageProvider) async {
if (_thumbnailWidth == null || _photoViewController.scale == null) {
return;
}
final imageInfo = await getImageInfo(imageProvider);
final scale = _photoViewController.scale! /
(imageInfo.image.width / _thumbnailWidth!);
final currentPosition = _photoViewController.value.position;
final positionScaleFactor = 1 / scale;
final newPosition = currentPosition.scale(
positionScaleFactor,
positionScaleFactor,
);
_photoViewController = PhotoViewController(
initialPosition: newPosition,
initialScale: scale,
);
_updateAspectRatioIfNeeded(imageInfo).ignore();
}
// Fallback logic to finish back fill and update aspect
// ratio if needed.
Future<void> _updateAspectRatioIfNeeded(ImageInfo imageInfo) async {
if (_imageProvider != null &&
widget.photo.isUploaded &&
widget.photo.ownerID == _currentUserID) {
final int h = imageInfo.image.height, w = imageInfo.image.width;
if (h != 0 &&
w != 0 &&
2023-05-03 07:18:40 +00:00
(h != widget.photo.height || w != widget.photo.width)) {
_logger.info('Updating aspect ratio for ${widget.photo} to $h:$w');
await FileMagicService.instance.updatePublicMagicMetadata([
2023-08-19 11:39:56 +00:00
widget.photo,
], {
heightKey: h,
widthKey: w,
});
}
}
}
2023-01-11 08:39:23 +00:00
bool _isGIF() => _photo.displayName.toLowerCase().endsWith(".gif");
}