zoomable_image.dart 6.4 KB

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