file_details_widget.dart 10 KB

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