zoomable_image.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // @dart=2.9
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/widgets.dart';
  5. import 'package:logging/logging.dart';
  6. import 'package:photo_view/photo_view.dart';
  7. import 'package:photos/core/cache/thumbnail_cache.dart';
  8. import 'package:photos/core/constants.dart';
  9. import 'package:photos/core/event_bus.dart';
  10. import 'package:photos/db/files_db.dart';
  11. import 'package:photos/events/files_updated_event.dart';
  12. import 'package:photos/events/local_photos_updated_event.dart';
  13. import 'package:photos/models/file.dart';
  14. import 'package:photos/ui/common/loading_widget.dart';
  15. import 'package:photos/utils/file_util.dart';
  16. import 'package:photos/utils/thumbnail_util.dart';
  17. class ZoomableImage extends StatefulWidget {
  18. final File photo;
  19. final Function(bool) shouldDisableScroll;
  20. final String tagPrefix;
  21. final Decoration backgroundDecoration;
  22. const ZoomableImage(
  23. this.photo, {
  24. Key key,
  25. this.shouldDisableScroll,
  26. @required this.tagPrefix,
  27. this.backgroundDecoration,
  28. }) : super(key: key);
  29. @override
  30. State<ZoomableImage> createState() => _ZoomableImageState();
  31. }
  32. class _ZoomableImageState extends State<ZoomableImage>
  33. with SingleTickerProviderStateMixin {
  34. final Logger _logger = Logger("ZoomableImage");
  35. File _photo;
  36. ImageProvider _imageProvider;
  37. bool _loadedSmallThumbnail = false;
  38. bool _loadingLargeThumbnail = false;
  39. bool _loadedLargeThumbnail = false;
  40. bool _loadingFinalImage = false;
  41. bool _loadedFinalImage = false;
  42. ValueChanged<PhotoViewScaleState> _scaleStateChangedCallback;
  43. bool _isZooming = false;
  44. @override
  45. void initState() {
  46. _photo = widget.photo;
  47. debugPrint('initState for ${_photo.toString()}');
  48. _scaleStateChangedCallback = (value) {
  49. if (widget.shouldDisableScroll != null) {
  50. widget.shouldDisableScroll(value != PhotoViewScaleState.initial);
  51. }
  52. _isZooming = value != PhotoViewScaleState.initial;
  53. debugPrint("isZooming = $_isZooming, currentState $value");
  54. // _logger.info('is reakky zooming $_isZooming with state $value');
  55. };
  56. super.initState();
  57. }
  58. @override
  59. Widget build(BuildContext context) {
  60. if (_photo.isRemoteFile()) {
  61. _loadNetworkImage();
  62. } else {
  63. _loadLocalImage(context);
  64. }
  65. Widget content;
  66. if (_imageProvider != null) {
  67. content = PhotoViewGestureDetectorScope(
  68. axis: Axis.vertical,
  69. child: PhotoView(
  70. imageProvider: _imageProvider,
  71. scaleStateChangedCallback: _scaleStateChangedCallback,
  72. minScale: PhotoViewComputedScale.contained,
  73. gaplessPlayback: true,
  74. heroAttributes: PhotoViewHeroAttributes(
  75. tag: widget.tagPrefix + _photo.tag(),
  76. ),
  77. backgroundDecoration: widget.backgroundDecoration,
  78. ),
  79. );
  80. } else {
  81. content = const EnteLoadingWidget();
  82. }
  83. final GestureDragUpdateCallback verticalDragCallback = _isZooming
  84. ? null
  85. : (d) => {
  86. if (!_isZooming && d.delta.dy > dragSensitivity)
  87. {Navigator.of(context).pop()}
  88. };
  89. return GestureDetector(
  90. onVerticalDragUpdate: verticalDragCallback,
  91. child: content,
  92. );
  93. }
  94. void _loadNetworkImage() {
  95. if (!_loadedSmallThumbnail && !_loadedFinalImage) {
  96. final cachedThumbnail = ThumbnailLruCache.get(_photo);
  97. if (cachedThumbnail != null) {
  98. _imageProvider = Image.memory(cachedThumbnail).image;
  99. _loadedSmallThumbnail = true;
  100. } else {
  101. getThumbnailFromServer(_photo).then((file) {
  102. final imageProvider = Image.memory(file).image;
  103. if (mounted) {
  104. precacheImage(imageProvider, context).then((value) {
  105. if (mounted) {
  106. setState(() {
  107. _imageProvider = imageProvider;
  108. _loadedSmallThumbnail = true;
  109. });
  110. }
  111. }).catchError((e) {
  112. _logger.severe("Could not load image " + _photo.toString());
  113. _loadedSmallThumbnail = true;
  114. });
  115. }
  116. });
  117. }
  118. }
  119. if (!_loadedFinalImage) {
  120. getFileFromServer(_photo).then((file) {
  121. _onFinalImageLoaded(
  122. Image.file(
  123. file,
  124. gaplessPlayback: true,
  125. ).image,
  126. );
  127. });
  128. }
  129. }
  130. void _loadLocalImage(BuildContext context) {
  131. if (!_loadedSmallThumbnail &&
  132. !_loadedLargeThumbnail &&
  133. !_loadedFinalImage) {
  134. final cachedThumbnail = ThumbnailLruCache.get(_photo, thumbnailSmallSize);
  135. if (cachedThumbnail != null) {
  136. _imageProvider = Image.memory(cachedThumbnail).image;
  137. _loadedSmallThumbnail = true;
  138. }
  139. }
  140. if (!_loadingLargeThumbnail &&
  141. !_loadedLargeThumbnail &&
  142. !_loadedFinalImage) {
  143. _loadingLargeThumbnail = true;
  144. getThumbnailFromLocal(_photo, size: thumbnailLargeSize, quality: 100)
  145. .then((cachedThumbnail) {
  146. if (cachedThumbnail != null) {
  147. _onLargeThumbnailLoaded(Image.memory(cachedThumbnail).image, context);
  148. }
  149. });
  150. }
  151. if (!_loadingFinalImage && !_loadedFinalImage) {
  152. _loadingFinalImage = true;
  153. getFile(
  154. _photo,
  155. isOrigin: Platform.isIOS &&
  156. _isGIF(), // since on iOS GIFs playback only when origin-files are loaded
  157. ).then((file) {
  158. if (file != null && file.existsSync()) {
  159. _onFinalImageLoaded(Image.file(file).image);
  160. } else {
  161. _logger.info("File was deleted " + _photo.toString());
  162. if (_photo.uploadedFileID != null) {
  163. _photo.localID = null;
  164. FilesDB.instance.update(_photo);
  165. _loadNetworkImage();
  166. } else {
  167. FilesDB.instance.deleteLocalFile(_photo);
  168. Bus.instance.fire(
  169. LocalPhotosUpdatedEvent(
  170. [_photo],
  171. type: EventType.deletedFromDevice,
  172. ),
  173. );
  174. }
  175. }
  176. });
  177. }
  178. }
  179. void _onLargeThumbnailLoaded(
  180. ImageProvider imageProvider,
  181. BuildContext context,
  182. ) {
  183. if (mounted && !_loadedFinalImage) {
  184. precacheImage(imageProvider, context).then((value) {
  185. if (mounted && !_loadedFinalImage) {
  186. setState(() {
  187. _imageProvider = imageProvider;
  188. _loadedLargeThumbnail = true;
  189. });
  190. }
  191. });
  192. }
  193. }
  194. void _onFinalImageLoaded(ImageProvider imageProvider) {
  195. if (mounted) {
  196. precacheImage(imageProvider, context).then((value) {
  197. if (mounted) {
  198. setState(() {
  199. _imageProvider = imageProvider;
  200. _loadedFinalImage = true;
  201. });
  202. }
  203. });
  204. }
  205. }
  206. bool _isGIF() => _photo.getDisplayName().toLowerCase().endsWith(".gif");
  207. }