2020-07-29 16:35:18 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2020-04-24 17:34:46 +00:00
|
|
|
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';
|
2020-05-04 20:44:34 +00:00
|
|
|
import 'package:photos/core/cache/thumbnail_cache.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';
|
2020-11-17 06:02:14 +00:00
|
|
|
import 'package:photos/db/files_db.dart';
|
2021-12-07 03:28:06 +00:00
|
|
|
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';
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/common/loading_widget.dart';
|
2020-08-13 01:35:57 +00:00
|
|
|
import 'package:photos/utils/file_util.dart';
|
2021-05-02 20:52:56 +00:00
|
|
|
import 'package:photos/utils/thumbnail_util.dart';
|
2020-04-24 17:34:46 +00:00
|
|
|
|
2020-04-24 20:47:21 +00:00
|
|
|
class ZoomableImage extends StatefulWidget {
|
2020-06-19 23:03:26 +00:00
|
|
|
final File photo;
|
2020-04-24 17:34:46 +00:00
|
|
|
final Function(bool) shouldDisableScroll;
|
2020-07-13 21:33:43 +00:00
|
|
|
final String tagPrefix;
|
2020-07-29 16:35:18 +00:00
|
|
|
final Decoration backgroundDecoration;
|
2020-04-24 17:34:46 +00:00
|
|
|
|
2022-07-04 08:43:01 +00:00
|
|
|
const ZoomableImage(
|
2020-04-24 17:34:46 +00:00
|
|
|
this.photo, {
|
|
|
|
Key key,
|
|
|
|
this.shouldDisableScroll,
|
2020-07-21 10:25:19 +00:00
|
|
|
@required this.tagPrefix,
|
2020-07-29 16:35:18 +00:00
|
|
|
this.backgroundDecoration,
|
2020-04-24 17:34:46 +00:00
|
|
|
}) : super(key: key);
|
|
|
|
|
2020-04-24 20:47:21 +00:00
|
|
|
@override
|
2022-07-03 09:45:00 +00:00
|
|
|
State<ZoomableImage> createState() => _ZoomableImageState();
|
2020-04-24 20:47:21 +00:00
|
|
|
}
|
|
|
|
|
2022-07-03 09:49:33 +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;
|
2020-04-24 20:47:21 +00:00
|
|
|
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;
|
2020-04-24 20:47:21 +00:00
|
|
|
bool _loadedFinalImage = false;
|
|
|
|
ValueChanged<PhotoViewScaleState> _scaleStateChangedCallback;
|
2022-03-14 11:44:18 +00:00
|
|
|
bool _isZooming = false;
|
2020-04-24 20:47:21 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2020-11-17 06:02:14 +00:00
|
|
|
_photo = widget.photo;
|
2022-06-26 20:22:07 +00:00
|
|
|
debugPrint('initState for ${_photo.toString()}');
|
2020-04-24 20:47:21 +00:00
|
|
|
_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');
|
2020-04-24 20:47:21 +00:00
|
|
|
};
|
|
|
|
super.initState();
|
|
|
|
}
|
2020-04-24 17:34:46 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-07-19 05:37:34 +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) {
|
2022-06-26 20:37:42 +00:00
|
|
|
content = PhotoViewGestureDetectorScope(
|
|
|
|
axis: Axis.vertical,
|
|
|
|
child: PhotoView(
|
|
|
|
imageProvider: _imageProvider,
|
|
|
|
scaleStateChangedCallback: _scaleStateChangedCallback,
|
|
|
|
minScale: PhotoViewComputedScale.contained,
|
|
|
|
gaplessPlayback: true,
|
|
|
|
heroAttributes: PhotoViewHeroAttributes(
|
|
|
|
tag: widget.tagPrefix + _photo.tag(),
|
|
|
|
),
|
|
|
|
backgroundDecoration: widget.backgroundDecoration,
|
2020-06-15 00:53:12 +00:00
|
|
|
),
|
2020-05-25 14:54:54 +00:00
|
|
|
);
|
|
|
|
} else {
|
2022-07-03 06:04:42 +00:00
|
|
|
content = const EnteLoadingWidget();
|
2020-05-25 14:54:54 +00:00
|
|
|
}
|
2022-03-20 09:33:46 +00:00
|
|
|
|
|
|
|
GestureDragUpdateCallback verticalDragCallback = _isZooming
|
|
|
|
? null
|
|
|
|
: (d) => {
|
2022-07-03 09:49:33 +00:00
|
|
|
if (!_isZooming && d.delta.dy > kDragSensitivity)
|
|
|
|
{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) {
|
2021-05-06 22:35:56 +00:00
|
|
|
final cachedThumbnail = ThumbnailLruCache.get(_photo);
|
2020-10-26 10:47:09 +00:00
|
|
|
if (cachedThumbnail != null) {
|
2021-05-06 22:35:56 +00:00
|
|
|
_imageProvider = Image.memory(cachedThumbnail).image;
|
2020-10-26 10:47:09 +00:00
|
|
|
_loadedSmallThumbnail = true;
|
|
|
|
} else {
|
|
|
|
getThumbnailFromServer(_photo).then((file) {
|
2021-05-06 22:35:56 +00:00
|
|
|
final imageProvider = Image.memory(file).image;
|
2020-10-26 10:47:09 +00:00
|
|
|
if (mounted) {
|
|
|
|
precacheImage(imageProvider, context).then((value) {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_imageProvider = imageProvider;
|
|
|
|
_loadedSmallThumbnail = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).catchError((e) {
|
|
|
|
_logger.severe("Could not load image " + _photo.toString());
|
|
|
|
_loadedSmallThumbnail = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-05-27 22:51:58 +00:00
|
|
|
}
|
|
|
|
if (!_loadedFinalImage) {
|
2020-08-13 01:35:57 +00:00
|
|
|
getFileFromServer(_photo).then((file) {
|
2022-06-11 08:23:52 +00:00
|
|
|
_onFinalImageLoaded(
|
|
|
|
Image.file(
|
|
|
|
file,
|
|
|
|
gaplessPlayback: true,
|
|
|
|
).image,
|
|
|
|
);
|
2020-08-13 01:07:44 +00:00
|
|
|
});
|
2020-05-27 22:51:58 +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 =
|
|
|
|
ThumbnailLruCache.get(_photo, kThumbnailSmallSize);
|
2020-04-24 20:47:21 +00:00
|
|
|
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;
|
2021-07-22 14:21:24 +00:00
|
|
|
getThumbnailFromLocal(_photo, size: kThumbnailLargeSize, quality: 100)
|
|
|
|
.then((cachedThumbnail) {
|
2021-10-01 20:45:40 +00:00
|
|
|
if (cachedThumbnail != null) {
|
2021-07-22 14:21:24 +00:00
|
|
|
_onLargeThumbnailLoaded(Image.memory(cachedThumbnail).image, context);
|
|
|
|
}
|
|
|
|
});
|
2020-04-24 17:34:46 +00:00
|
|
|
}
|
2020-04-24 20:47:21 +00:00
|
|
|
|
2020-06-10 00:04:22 +00:00
|
|
|
if (!_loadingFinalImage && !_loadedFinalImage) {
|
|
|
|
_loadingFinalImage = true;
|
2021-07-22 14:21:24 +00:00
|
|
|
getFile(_photo).then((file) {
|
|
|
|
if (file != null && file.existsSync()) {
|
|
|
|
_onFinalImageLoaded(Image.file(file).image);
|
|
|
|
} else {
|
|
|
|
_logger.info("File was deleted " + _photo.toString());
|
|
|
|
if (_photo.uploadedFileID != null) {
|
|
|
|
_photo.localID = null;
|
|
|
|
FilesDB.instance.update(_photo);
|
|
|
|
_loadNetworkImage();
|
|
|
|
} else {
|
2021-07-24 17:22:46 +00:00
|
|
|
FilesDB.instance.deleteLocalFile(_photo);
|
2022-06-11 08:23:52 +00:00
|
|
|
Bus.instance.fire(
|
|
|
|
LocalPhotosUpdatedEvent(
|
|
|
|
[_photo],
|
|
|
|
type: EventType.deletedFromDevice,
|
|
|
|
),
|
|
|
|
);
|
2020-11-17 06:02:14 +00:00
|
|
|
}
|
2021-07-22 14:21:24 +00:00
|
|
|
}
|
|
|
|
});
|
2020-04-24 20:47:21 +00:00
|
|
|
}
|
2020-04-24 17:34:46 +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,
|
|
|
|
) {
|
2020-06-10 21:02:24 +00:00
|
|
|
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) {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_imageProvider = imageProvider;
|
|
|
|
_loadedFinalImage = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-24 20:59:11 +00:00
|
|
|
}
|
2020-04-24 17:34:46 +00:00
|
|
|
}
|