file_details_widget.dart 11 KB

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