file_details_widget.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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/models/file.dart";
  6. import "package:photos/models/file_type.dart";
  7. import "package:photos/models/magic_metadata.dart";
  8. import "package:photos/services/feature_flag_service.dart";
  9. import "package:photos/services/file_magic_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/added_by_widget.dart";
  16. import "package:photos/ui/viewer/file_details/albums_item_widget.dart";
  17. import 'package:photos/ui/viewer/file_details/backed_up_time_item_widget.dart';
  18. import "package:photos/ui/viewer/file_details/creation_time_item_widget.dart";
  19. import 'package:photos/ui/viewer/file_details/exif_item_widgets.dart';
  20. import "package:photos/ui/viewer/file_details/file_properties_item_widget.dart";
  21. import "package:photos/ui/viewer/file_details/location_tags_widget.dart";
  22. import "package:photos/ui/viewer/file_details/objects_item_widget.dart";
  23. import "package:photos/utils/exif_util.dart";
  24. class FileDetailsWidget extends StatefulWidget {
  25. final File file;
  26. const FileDetailsWidget(
  27. this.file, {
  28. Key? key,
  29. }) : super(key: key);
  30. @override
  31. State<FileDetailsWidget> createState() => _FileDetailsWidgetState();
  32. }
  33. class _FileDetailsWidgetState extends State<FileDetailsWidget> {
  34. final ValueNotifier<Map<String, IfdTag>?> _exifNotifier = ValueNotifier(null);
  35. final Map<String, dynamic> _exifData = {
  36. "focalLength": null,
  37. "fNumber": null,
  38. "resolution": null,
  39. "takenOnDevice": null,
  40. "exposureTime": null,
  41. "ISO": null,
  42. "megaPixels": null,
  43. "lat": null,
  44. "long": null,
  45. "latRef": null,
  46. "longRef": null,
  47. };
  48. bool _isImage = false;
  49. late int _currentUserID;
  50. bool showExifListTile = false;
  51. final hasLocationDataNotifer = ValueNotifier(false);
  52. @override
  53. void initState() {
  54. debugPrint('file_details_sheet initState');
  55. _currentUserID = Configuration.instance.getUserID()!;
  56. _isImage = widget.file.fileType == FileType.image ||
  57. widget.file.fileType == FileType.livePhoto;
  58. _exifNotifier.addListener(() {
  59. if (_exifNotifier.value != null) {
  60. hasLocationDataNotifer.value = _hasLocationData();
  61. }
  62. });
  63. if (_isImage) {
  64. _exifNotifier.addListener(() {
  65. if (_exifNotifier.value != null) {
  66. _generateExifForDetails(_exifNotifier.value!);
  67. }
  68. showExifListTile = _exifData["focalLength"] != null ||
  69. _exifData["fNumber"] != null ||
  70. _exifData["takenOnDevice"] != null ||
  71. _exifData["exposureTime"] != null ||
  72. _exifData["ISO"] != null;
  73. });
  74. }
  75. getExif(widget.file).then((exif) {
  76. _exifNotifier.value = exif;
  77. });
  78. super.initState();
  79. }
  80. @override
  81. void dispose() {
  82. _exifNotifier.dispose();
  83. super.dispose();
  84. }
  85. @override
  86. Widget build(BuildContext context) {
  87. final file = widget.file;
  88. final bool isFileOwner =
  89. file.ownerID == null || file.ownerID == _currentUserID;
  90. //Make sure the bottom most tile is always the same one, that is it should
  91. //not be rendered only if a condition is met.
  92. final fileDetailsTiles = <Widget>[];
  93. fileDetailsTiles.add(
  94. !widget.file.isUploaded ||
  95. (!isFileOwner && (widget.file.caption?.isEmpty ?? true))
  96. ? const SizedBox(height: 16)
  97. : Padding(
  98. padding: const EdgeInsets.only(top: 8, bottom: 24),
  99. child: isFileOwner
  100. ? FileCaptionWidget(file: widget.file)
  101. : FileCaptionReadyOnly(caption: widget.file.caption!),
  102. ),
  103. );
  104. fileDetailsTiles.addAll([
  105. CreationTimeItem(file, _currentUserID),
  106. const FileDetailsDivider(),
  107. ValueListenableBuilder(
  108. valueListenable: _exifNotifier,
  109. builder: (context, _, __) => FilePropertiesItemWidget(
  110. file,
  111. _isImage,
  112. _exifData,
  113. _currentUserID,
  114. ),
  115. ),
  116. const FileDetailsDivider(),
  117. ]);
  118. fileDetailsTiles.add(
  119. ValueListenableBuilder(
  120. valueListenable: _exifNotifier,
  121. builder: (context, value, _) {
  122. return showExifListTile
  123. ? Column(
  124. children: [
  125. BasicExifItemWidget(_exifData),
  126. const FileDetailsDivider(),
  127. ],
  128. )
  129. : const SizedBox.shrink();
  130. },
  131. ),
  132. );
  133. if (FeatureFlagService.instance.isInternalUserOrDebugBuild()) {
  134. fileDetailsTiles.addAll([
  135. ValueListenableBuilder(
  136. valueListenable: hasLocationDataNotifer,
  137. builder: (context, bool hasLocationData, __) {
  138. return hasLocationData
  139. ? Column(
  140. children: [
  141. LocationTagsWidget(
  142. widget.file.location!,
  143. ),
  144. const FileDetailsDivider(),
  145. ],
  146. )
  147. : const SizedBox.shrink();
  148. },
  149. )
  150. ]);
  151. }
  152. if (_isImage) {
  153. fileDetailsTiles.addAll([
  154. ValueListenableBuilder(
  155. valueListenable: _exifNotifier,
  156. builder: (context, value, _) {
  157. return Column(
  158. children: [
  159. AllExifItemWidget(file, _exifNotifier.value),
  160. const FileDetailsDivider(),
  161. ],
  162. );
  163. },
  164. )
  165. ]);
  166. }
  167. if (FeatureFlagService.instance.isInternalUserOrDebugBuild()) {
  168. fileDetailsTiles.addAll([
  169. ObjectsItemWidget(file),
  170. const FileDetailsDivider(),
  171. ]);
  172. }
  173. if (file.uploadedFileID != null && file.updationTime != null) {
  174. fileDetailsTiles.addAll(
  175. [
  176. BackedUpTimeItemWidget(file),
  177. const FileDetailsDivider(),
  178. ],
  179. );
  180. }
  181. fileDetailsTiles.add(AlbumsItemWidget(file, _currentUserID));
  182. return SafeArea(
  183. top: false,
  184. child: Scrollbar(
  185. thickness: 4,
  186. radius: const Radius.circular(2),
  187. thumbVisibility: true,
  188. child: Padding(
  189. padding: const EdgeInsets.all(8.0),
  190. child: CustomScrollView(
  191. physics: const ClampingScrollPhysics(),
  192. shrinkWrap: true,
  193. slivers: <Widget>[
  194. TitleBarWidget(
  195. isFlexibleSpaceDisabled: true,
  196. title: "Details",
  197. isOnTopOfScreen: false,
  198. backgroundColor: getEnteColorScheme(context).backgroundElevated,
  199. leading: IconButtonWidget(
  200. icon: Icons.expand_more_outlined,
  201. iconButtonType: IconButtonType.primary,
  202. onTap: () => Navigator.pop(context),
  203. ),
  204. ),
  205. SliverToBoxAdapter(
  206. child: AddedByWidget(
  207. widget.file,
  208. _currentUserID,
  209. ),
  210. ),
  211. SliverList(
  212. delegate: SliverChildBuilderDelegate(
  213. (context, index) {
  214. return fileDetailsTiles[index];
  215. },
  216. childCount: fileDetailsTiles.length,
  217. ),
  218. )
  219. ],
  220. ),
  221. ),
  222. ),
  223. );
  224. }
  225. bool _hasLocationData() {
  226. final fileLocation = widget.file.location;
  227. bool hasLocation = (fileLocation != null &&
  228. fileLocation.latitude != null &&
  229. fileLocation.longitude != null) &&
  230. (fileLocation.latitude != 0 || fileLocation.longitude != 0);
  231. //This code is for updating the location of files in which location data is
  232. //missing and the EXIF has location data. This is only happens for a
  233. //certain specific minority of devices.
  234. if (!hasLocation) {
  235. hasLocation = _setLocationDataFromExif();
  236. }
  237. return hasLocation;
  238. }
  239. bool _setLocationDataFromExif() {
  240. final locationDataFromExif = locationFromExif(_exifNotifier.value!);
  241. if (locationDataFromExif?.latitude != null &&
  242. locationDataFromExif?.longitude != null) {
  243. widget.file.location = locationDataFromExif;
  244. FileMagicService.instance.updatePublicMagicMetadata([
  245. widget.file
  246. ], {
  247. pubMagicKeyLat: locationDataFromExif!.latitude,
  248. pubMagicKeyLong: locationDataFromExif.longitude
  249. });
  250. return true;
  251. }
  252. return false;
  253. }
  254. _generateExifForDetails(Map<String, IfdTag> exif) {
  255. if (exif["EXIF FocalLength"] != null) {
  256. _exifData["focalLength"] =
  257. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio).numerator /
  258. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio)
  259. .denominator;
  260. }
  261. if (exif["EXIF FNumber"] != null) {
  262. _exifData["fNumber"] =
  263. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).numerator /
  264. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).denominator;
  265. }
  266. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  267. final imageLength = exif["EXIF ExifImageLength"] ??
  268. exif["Image "
  269. "ImageLength"];
  270. if (imageWidth != null && imageLength != null) {
  271. _exifData["resolution"] = '$imageWidth x $imageLength';
  272. _exifData['megaPixels'] =
  273. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  274. 1000000)
  275. .toStringAsFixed(1);
  276. } else {
  277. debugPrint("No image width/height");
  278. }
  279. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  280. _exifData["takenOnDevice"] =
  281. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  282. }
  283. if (exif["EXIF ExposureTime"] != null) {
  284. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  285. }
  286. if (exif["EXIF ISOSpeedRatings"] != null) {
  287. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  288. }
  289. }
  290. }
  291. class FileDetailsDivider extends StatelessWidget {
  292. const FileDetailsDivider({super.key});
  293. @override
  294. Widget build(BuildContext context) {
  295. const dividerPadding = EdgeInsets.symmetric(vertical: 15.5);
  296. return const DividerWidget(
  297. dividerType: DividerType.menu,
  298. divColorHasBlur: false,
  299. padding: dividerPadding,
  300. );
  301. }
  302. }