file_details_widget.dart 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import "package:exif/exif.dart";
  2. import "package:flutter/cupertino.dart";
  3. import "package:flutter/material.dart";
  4. import "package:photos/core/configuration.dart";
  5. import "package:photos/ente_theme_data.dart";
  6. import "package:photos/models/file.dart";
  7. import "package:photos/models/file_type.dart";
  8. import 'package:photos/services/collections_service.dart';
  9. import "package:photos/services/feature_flag_service.dart";
  10. import 'package:photos/theme/ente_theme.dart';
  11. import 'package:photos/ui/components/buttons/icon_button_widget.dart';
  12. import 'package:photos/ui/components/divider_widget.dart';
  13. import 'package:photos/ui/components/title_bar_widget.dart';
  14. import 'package:photos/ui/viewer/file/file_caption_widget.dart';
  15. import "package:photos/ui/viewer/file_details/albums_item_widget.dart";
  16. import "package:photos/ui/viewer/file_details/backed_up_date_item_widget.dart";
  17. import "package:photos/ui/viewer/file_details/creation_time_item_widget.dart";
  18. import "package:photos/ui/viewer/file_details/exif_item_widget.dart";
  19. import "package:photos/ui/viewer/file_details/file_properties_item_widget.dart";
  20. import "package:photos/ui/viewer/file_details/objects_item_widget.dart";
  21. import "package:photos/utils/exif_util.dart";
  22. class FileDetailsWidget extends StatefulWidget {
  23. final File file;
  24. const FileDetailsWidget(
  25. this.file, {
  26. Key? key,
  27. }) : super(key: key);
  28. @override
  29. State<FileDetailsWidget> createState() => _FileDetailsWidgetState();
  30. }
  31. class _FileDetailsWidgetState extends State<FileDetailsWidget> {
  32. Map<String, IfdTag>? _exif;
  33. final Map<String, dynamic> _exifData = {
  34. "focalLength": null,
  35. "fNumber": null,
  36. "resolution": null,
  37. "takenOnDevice": null,
  38. "exposureTime": null,
  39. "ISO": null,
  40. "megaPixels": null
  41. };
  42. bool _isImage = false;
  43. late int _currentUserID;
  44. @override
  45. void initState() {
  46. debugPrint('file_details_sheet initState');
  47. _currentUserID = Configuration.instance.getUserID()!;
  48. _isImage = widget.file.fileType == FileType.image ||
  49. widget.file.fileType == FileType.livePhoto;
  50. if (_isImage) {
  51. getExif(widget.file).then((exif) {
  52. if (mounted) {
  53. setState(() {
  54. _exif = exif;
  55. });
  56. }
  57. });
  58. }
  59. super.initState();
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. final file = widget.file;
  64. final bool isFileOwner =
  65. file.ownerID == null || file.ownerID == _currentUserID;
  66. if (_isImage && _exif != null) {
  67. _generateExifForDetails(_exif!);
  68. }
  69. final bool showExifListTile = _exifData["focalLength"] != null ||
  70. _exifData["fNumber"] != null ||
  71. _exifData["takenOnDevice"] != null ||
  72. _exifData["exposureTime"] != null ||
  73. _exifData["ISO"] != null;
  74. final fileDetailsTiles = <Widget?>[
  75. !widget.file.isUploaded ||
  76. (!isFileOwner && (widget.file.caption?.isEmpty ?? true))
  77. ? const SizedBox(height: 16)
  78. : Padding(
  79. padding: const EdgeInsets.only(top: 8, bottom: 24),
  80. child: isFileOwner
  81. ? FileCaptionWidget(file: widget.file)
  82. : FileCaptionReadyOnly(caption: widget.file.caption!),
  83. ),
  84. CreationTimeItem(file, _currentUserID),
  85. FilePropertiesWidget(file, _isImage, _exifData, _currentUserID),
  86. showExifListTile ? BasicExifItemWidget(_exifData) : null,
  87. _isImage ? AllExifItemWidget(file, _exif) : null,
  88. FeatureFlagService.instance.isInternalUserOrDebugBuild()
  89. ? ObjectsItemWidget(file)
  90. : null,
  91. (file.uploadedFileID != null && file.updationTime != null)
  92. ? BackedUpDateItemWidget(file)
  93. : null,
  94. AlbumsItemWidget(file, _currentUserID),
  95. ];
  96. fileDetailsTiles.removeWhere(
  97. (element) => element == null,
  98. );
  99. return SafeArea(
  100. top: false,
  101. child: Scrollbar(
  102. thickness: 4,
  103. radius: const Radius.circular(2),
  104. thumbVisibility: true,
  105. child: Padding(
  106. padding: const EdgeInsets.all(8.0),
  107. child: CustomScrollView(
  108. physics: const ClampingScrollPhysics(),
  109. shrinkWrap: true,
  110. slivers: <Widget>[
  111. TitleBarWidget(
  112. isFlexibleSpaceDisabled: true,
  113. title: "Details",
  114. isOnTopOfScreen: false,
  115. backgroundColor: getEnteColorScheme(context).backgroundElevated,
  116. leading: IconButtonWidget(
  117. icon: Icons.expand_more_outlined,
  118. iconButtonType: IconButtonType.primary,
  119. onTap: () => Navigator.pop(context),
  120. ),
  121. ),
  122. SliverToBoxAdapter(child: addedBy(widget.file)),
  123. SliverList(
  124. delegate: SliverChildBuilderDelegate(
  125. (context, index) {
  126. //Dividers occupy odd indexes
  127. if (index.isOdd) {
  128. return index == 1
  129. ? const SizedBox.shrink()
  130. : const Padding(
  131. padding: EdgeInsets.symmetric(vertical: 15.5),
  132. child: DividerWidget(
  133. dividerType: DividerType.menu,
  134. divColorHasBlur: false,
  135. ),
  136. );
  137. } else {
  138. return fileDetailsTiles[index ~/ 2];
  139. }
  140. },
  141. childCount: (fileDetailsTiles.length * 2) - 1,
  142. ),
  143. )
  144. ],
  145. ),
  146. ),
  147. ),
  148. );
  149. }
  150. Widget addedBy(File file) {
  151. if (file.uploadedFileID == null) {
  152. return const SizedBox.shrink();
  153. }
  154. String? addedBy;
  155. if (file.ownerID == _currentUserID) {
  156. if (file.pubMagicMetadata!.uploaderName != null) {
  157. addedBy = file.pubMagicMetadata!.uploaderName;
  158. }
  159. } else {
  160. final fileOwner = CollectionsService.instance
  161. .getFileOwner(file.ownerID!, file.collectionID);
  162. addedBy = fileOwner.email;
  163. }
  164. if (addedBy == null || addedBy.isEmpty) {
  165. return const SizedBox.shrink();
  166. }
  167. final enteTheme = Theme.of(context).colorScheme.enteTheme;
  168. return Padding(
  169. padding: const EdgeInsets.only(top: 4.0, bottom: 4.0, left: 16),
  170. child: Text(
  171. "Added by $addedBy",
  172. style: enteTheme.textTheme.mini
  173. .copyWith(color: enteTheme.colorScheme.textMuted),
  174. ),
  175. );
  176. }
  177. _generateExifForDetails(Map<String, IfdTag> exif) {
  178. if (exif["EXIF FocalLength"] != null) {
  179. _exifData["focalLength"] =
  180. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio).numerator /
  181. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio)
  182. .denominator;
  183. }
  184. if (exif["EXIF FNumber"] != null) {
  185. _exifData["fNumber"] =
  186. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).numerator /
  187. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).denominator;
  188. }
  189. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  190. final imageLength = exif["EXIF ExifImageLength"] ??
  191. exif["Image "
  192. "ImageLength"];
  193. if (imageWidth != null && imageLength != null) {
  194. _exifData["resolution"] = '$imageWidth x $imageLength';
  195. _exifData['megaPixels'] =
  196. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  197. 1000000)
  198. .toStringAsFixed(1);
  199. } else {
  200. debugPrint("No image width/height");
  201. }
  202. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  203. _exifData["takenOnDevice"] =
  204. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  205. }
  206. if (exif["EXIF ExposureTime"] != null) {
  207. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  208. }
  209. if (exif["EXIF ISOSpeedRatings"] != null) {
  210. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  211. }
  212. }
  213. }