file_details_widget.dart 11 KB

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