zoomable_image.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:logging/logging.dart';
  5. import 'package:photo_view/photo_view.dart';
  6. import 'package:photos/core/cache/thumbnail_cache.dart';
  7. import 'package:photos/core/constants.dart';
  8. import 'package:photos/core/event_bus.dart';
  9. import 'package:photos/db/files_db.dart';
  10. import 'package:photos/events/files_updated_event.dart';
  11. import 'package:photos/events/local_photos_updated_event.dart';
  12. import 'package:photos/models/file.dart';
  13. import 'package:photos/ui/common/loading_widget.dart';
  14. import 'package:photos/utils/file_util.dart';
  15. import 'package:photos/utils/thumbnail_util.dart';
  16. class ZoomableImage extends StatefulWidget {
  17. final File photo;
  18. final Function(bool) shouldDisableScroll;
  19. final String tagPrefix;
  20. final Decoration backgroundDecoration;
  21. const ZoomableImage(
  22. this.photo, {
  23. Key key,
  24. this.shouldDisableScroll,
  25. @required this.tagPrefix,
  26. this.backgroundDecoration,
  27. }) : super(key: key);
  28. @override
  29. State<ZoomableImage> createState() => _ZoomableImageState();
  30. }
  31. class _ZoomableImageState extends State<ZoomableImage>
  32. with SingleTickerProviderStateMixin {
  33. final Logger _logger = Logger("ZoomableImage");
  34. File _photo;
  35. ImageProvider _imageProvider;
  36. bool _loadedSmallThumbnail = false;
  37. bool _loadingLargeThumbnail = false;
  38. bool _loadedLargeThumbnail = false;
  39. bool _loadingFinalImage = false;
  40. bool _loadedFinalImage = false;
  41. ValueChanged<PhotoViewScaleState> _scaleStateChangedCallback;
  42. bool _isZooming = false;
  43. @override
  44. void initState() {
  45. _photo = widget.photo;
  46. debugPrint('initState for ${_photo.toString()}');
  47. _scaleStateChangedCallback = (value) {
  48. if (widget.shouldDisableScroll != null) {
  49. widget.shouldDisableScroll(value != PhotoViewScaleState.initial);
  50. }
  51. _isZooming = value != PhotoViewScaleState.initial;
  52. debugPrint("isZooming = $_isZooming, currentState $value");
  53. // _logger.info('is reakky zooming $_isZooming with state $value');
  54. };
  55. super.initState();
  56. }
  57. @override
  58. Widget build(BuildContext context) {
  59. if (_photo.isRemoteFile()) {
  60. _loadNetworkImage();
  61. } else {
  62. _loadLocalImage(context);
  63. }
  64. Widget content;
  65. if (_imageProvider != null) {
  66. content = PhotoViewGestureDetectorScope(
  67. axis: Axis.vertical,
  68. child: PhotoView(
  69. imageProvider: _imageProvider,
  70. scaleStateChangedCallback: _scaleStateChangedCallback,
  71. minScale: PhotoViewComputedScale.contained,
  72. gaplessPlayback: true,
  73. heroAttributes: PhotoViewHeroAttributes(
  74. tag: widget.tagPrefix + _photo.tag(),
  75. ),
  76. backgroundDecoration: widget.backgroundDecoration,
  77. ),
  78. );
  79. } else {
  80. content = const EnteLoadingWidget();
  81. }
  82. final GestureDragUpdateCallback verticalDragCallback = _isZooming
  83. ? null
  84. : (d) => {
  85. if (!_isZooming && d.delta.dy > kDragSensitivity)
  86. {Navigator.of(context).pop()}
  87. };
  88. return GestureDetector(
  89. onVerticalDragUpdate: verticalDragCallback,
  90. child: content,
  91. );
  92. }
  93. void _loadNetworkImage() {
  94. if (!_loadedSmallThumbnail && !_loadedFinalImage) {
  95. final cachedThumbnail = ThumbnailLruCache.get(_photo);
  96. if (cachedThumbnail != null) {
  97. _imageProvider = Image.memory(cachedThumbnail).image;
  98. _loadedSmallThumbnail = true;
  99. } else {
  100. getThumbnailFromServer(_photo).then((file) {
  101. final imageProvider = Image.memory(file).image;
  102. if (mounted) {
  103. precacheImage(imageProvider, context).then((value) {
  104. if (mounted) {
  105. setState(() {
  106. _imageProvider = imageProvider;
  107. _loadedSmallThumbnail = true;
  108. });
  109. }
  110. }).catchError((e) {
  111. _logger.severe("Could not load image " + _photo.toString());
  112. _loadedSmallThumbnail = true;
  113. });
  114. }
  115. });
  116. }
  117. }
  118. if (!_loadedFinalImage) {
  119. getFileFromServer(_photo).then((file) {
  120. _onFinalImageLoaded(
  121. Image.file(
  122. file,
  123. gaplessPlayback: true,
  124. ).image,
  125. );
  126. });
  127. }
  128. }
  129. void _loadLocalImage(BuildContext context) {
  130. if (!_loadedSmallThumbnail &&
  131. !_loadedLargeThumbnail &&
  132. !_loadedFinalImage) {
  133. final cachedThumbnail =
  134. ThumbnailLruCache.get(_photo, kThumbnailSmallSize);
  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: kThumbnailLargeSize, 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. }