thumbnail_widget.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import 'package:flutter/material.dart';
  2. import 'package:photos/core/cache/image_cache.dart';
  3. import 'package:photos/core/cache/thumbnail_cache.dart';
  4. import 'package:photos/db/files_db.dart';
  5. import 'package:photos/models/file.dart';
  6. import 'package:logging/logging.dart';
  7. import 'package:photos/core/constants.dart';
  8. import 'package:photos/models/file_type.dart';
  9. import 'package:photos/repositories/file_repository.dart';
  10. import 'package:photos/utils/file_util.dart';
  11. class ThumbnailWidget extends StatefulWidget {
  12. final File file;
  13. final BoxFit fit;
  14. const ThumbnailWidget(
  15. this.file, {
  16. Key key,
  17. this.fit = BoxFit.cover,
  18. }) : super(key: key);
  19. @override
  20. _ThumbnailWidgetState createState() => _ThumbnailWidgetState();
  21. }
  22. class _ThumbnailWidgetState extends State<ThumbnailWidget> {
  23. static final _logger = Logger("ThumbnailWidget");
  24. static final Widget loadingWidget = Container(
  25. alignment: Alignment.center,
  26. color: Colors.grey[800],
  27. );
  28. bool _hasLoadedThumbnail = false;
  29. bool _isLoadingThumbnail = false;
  30. bool _encounteredErrorLoadingThumbnail = false;
  31. ImageProvider _imageProvider;
  32. @override
  33. void initState() {
  34. super.initState();
  35. }
  36. @override
  37. void dispose() {
  38. removePendingGetThumbnailRequestIfAny(widget.file);
  39. super.dispose();
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. if (widget.file.localID == null) {
  44. _loadNetworkImage();
  45. } else {
  46. _loadLocalImage(context);
  47. }
  48. var image;
  49. if (_imageProvider != null) {
  50. image = Image(
  51. image: _imageProvider,
  52. fit: widget.fit,
  53. );
  54. }
  55. var content;
  56. if (image != null) {
  57. if (widget.file.fileType == FileType.video) {
  58. content = Stack(
  59. children: [
  60. image,
  61. Container(
  62. height: 64,
  63. child: Icon(
  64. Icons.play_circle_outline,
  65. size: 40,
  66. color: Colors.white70,
  67. ),
  68. ),
  69. ],
  70. fit: StackFit.expand,
  71. );
  72. } else {
  73. content = image;
  74. }
  75. }
  76. return Stack(
  77. children: [
  78. loadingWidget,
  79. AnimatedOpacity(
  80. opacity: content == null ? 0 : 1.0,
  81. duration: Duration(milliseconds: 400),
  82. child: content,
  83. ),
  84. widget.file.uploadedFileID == null
  85. ? Align(
  86. alignment: Alignment.bottomRight,
  87. child: Padding(
  88. padding: const EdgeInsets.only(right: 8, bottom: 4),
  89. child: Icon(
  90. Icons.cloud_off_outlined,
  91. size: 18,
  92. color: Colors.white.withOpacity(0.8),
  93. ),
  94. ),
  95. )
  96. : Container(),
  97. ],
  98. fit: StackFit.expand,
  99. );
  100. }
  101. void _loadLocalImage(BuildContext context) {
  102. if (!_hasLoadedThumbnail &&
  103. !_encounteredErrorLoadingThumbnail &&
  104. !_isLoadingThumbnail) {
  105. _isLoadingThumbnail = true;
  106. final cachedSmallThumbnail =
  107. ThumbnailLruCache.get(widget.file, THUMBNAIL_SMALL_SIZE);
  108. if (cachedSmallThumbnail != null) {
  109. _imageProvider = Image.memory(cachedSmallThumbnail).image;
  110. _hasLoadedThumbnail = true;
  111. } else {
  112. widget.file.getAsset().then((asset) async {
  113. if (asset == null || !(await asset.exists)) {
  114. if (widget.file.uploadedFileID != null) {
  115. widget.file.localID = null;
  116. FilesDB.instance.update(widget.file);
  117. _loadNetworkImage();
  118. } else {
  119. FilesDB.instance.deleteLocalFile(widget.file.localID);
  120. FileRepository.instance.reloadFiles();
  121. }
  122. return;
  123. }
  124. asset
  125. .thumbDataWithSize(
  126. THUMBNAIL_SMALL_SIZE,
  127. THUMBNAIL_SMALL_SIZE,
  128. quality: THUMBNAIL_SMALL_SIZE_QUALITY,
  129. )
  130. .then((data) {
  131. if (data != null && mounted) {
  132. final imageProvider = Image.memory(data).image;
  133. precacheImage(imageProvider, context).then((value) {
  134. if (mounted) {
  135. setState(() {
  136. _imageProvider = imageProvider;
  137. _hasLoadedThumbnail = true;
  138. });
  139. }
  140. });
  141. }
  142. ThumbnailLruCache.put(widget.file, THUMBNAIL_SMALL_SIZE, data);
  143. });
  144. }).catchError((e) {
  145. _logger.warning("Could not load image: ", e);
  146. _encounteredErrorLoadingThumbnail = true;
  147. });
  148. }
  149. }
  150. }
  151. void _loadNetworkImage() {
  152. if (!_hasLoadedThumbnail &&
  153. !_encounteredErrorLoadingThumbnail &&
  154. !_isLoadingThumbnail) {
  155. _isLoadingThumbnail = true;
  156. final cachedThumbnail = ThumbnailFileLruCache.get(widget.file);
  157. if (cachedThumbnail != null) {
  158. _imageProvider = Image.file(cachedThumbnail).image;
  159. _hasLoadedThumbnail = true;
  160. return;
  161. }
  162. _getThumbnailFromServer();
  163. }
  164. }
  165. void _getThumbnailFromServer() {
  166. getThumbnailFromServer(widget.file).then((file) async {
  167. if (file.lengthSync() > THUMBNAIL_DATA_LIMIT) {
  168. _logger.warning(widget.file.title +
  169. " has a thumbnail of size " +
  170. file.lengthSync().toString());
  171. }
  172. if (mounted) {
  173. final imageProvider = Image.file(file).image;
  174. precacheImage(imageProvider, context).then((value) {
  175. if (mounted) {
  176. setState(() {
  177. _imageProvider = imageProvider;
  178. _hasLoadedThumbnail = true;
  179. });
  180. }
  181. }).catchError((e) {
  182. _logger.severe("Could not load image " + widget.file.toString());
  183. _encounteredErrorLoadingThumbnail = true;
  184. });
  185. }
  186. });
  187. }
  188. @override
  189. void didUpdateWidget(ThumbnailWidget oldWidget) {
  190. super.didUpdateWidget(oldWidget);
  191. if (widget.file.generatedID != oldWidget.file.generatedID) {
  192. setState(() {
  193. _hasLoadedThumbnail = false;
  194. _isLoadingThumbnail = false;
  195. _imageProvider = null;
  196. });
  197. }
  198. }
  199. }