file_info_widget.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. import "package:exif/exif.dart";
  2. import "package:flutter/cupertino.dart";
  3. import "package:flutter/material.dart";
  4. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  5. import 'package:photo_manager/photo_manager.dart';
  6. import "package:photos/core/configuration.dart";
  7. import 'package:photos/db/files_db.dart';
  8. import "package:photos/ente_theme_data.dart";
  9. import "package:photos/models/file.dart";
  10. import "package:photos/models/file_type.dart";
  11. import 'package:photos/services/collections_service.dart';
  12. import "package:photos/services/feature_flag_service.dart";
  13. import 'package:photos/theme/ente_theme.dart';
  14. import 'package:photos/ui/components/divider_widget.dart';
  15. import 'package:photos/ui/components/icon_button_widget.dart';
  16. import 'package:photos/ui/components/title_bar_widget.dart';
  17. import 'package:photos/ui/viewer/file/collections_list_of_file_widget.dart';
  18. import 'package:photos/ui/viewer/file/device_folders_list_of_file_widget.dart';
  19. import 'package:photos/ui/viewer/file/file_caption_widget.dart';
  20. import "package:photos/ui/viewer/file/object_tags_widget.dart";
  21. import 'package:photos/ui/viewer/file/raw_exif_list_tile_widget.dart';
  22. import "package:photos/utils/date_time_util.dart";
  23. import "package:photos/utils/exif_util.dart";
  24. import "package:photos/utils/file_util.dart";
  25. import "package:photos/utils/magic_util.dart";
  26. class FileInfoWidget extends StatefulWidget {
  27. final File file;
  28. const FileInfoWidget(
  29. this.file, {
  30. Key? key,
  31. }) : super(key: key);
  32. @override
  33. State<FileInfoWidget> createState() => _FileInfoWidgetState();
  34. }
  35. class _FileInfoWidgetState extends State<FileInfoWidget> {
  36. Map<String, IfdTag>? _exif;
  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. };
  46. bool _isImage = false;
  47. int? _currentUserID;
  48. @override
  49. void initState() {
  50. debugPrint('file_info_dialog initState');
  51. _currentUserID = Configuration.instance.getUserID();
  52. _isImage = widget.file.fileType == FileType.image ||
  53. widget.file.fileType == FileType.livePhoto;
  54. if (_isImage) {
  55. getExif(widget.file).then((exif) {
  56. if (mounted) {
  57. setState(() {
  58. _exif = exif;
  59. });
  60. }
  61. });
  62. }
  63. super.initState();
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. final file = widget.file;
  68. final fileIsBackedup = file.uploadedFileID == null ? false : true;
  69. final bool isFileOwner =
  70. file.ownerID == null || file.ownerID == _currentUserID;
  71. late Future<Set<int>> allCollectionIDsOfFile;
  72. //Typing this as Future<Set<T>> as it would be easier to implement showing multiple device folders for a file in the future
  73. final Future<Set<String>> allDeviceFoldersOfFile =
  74. Future.sync(() => {file.deviceFolder ?? ''});
  75. if (fileIsBackedup) {
  76. allCollectionIDsOfFile = FilesDB.instance.getAllCollectionIDsOfFile(
  77. file.uploadedFileID!,
  78. );
  79. }
  80. final dateTime = DateTime.fromMicrosecondsSinceEpoch(file.creationTime!);
  81. final dateTimeForUpdationTime =
  82. DateTime.fromMicrosecondsSinceEpoch(file.updationTime!);
  83. if (_isImage && _exif != null) {
  84. _generateExifForDetails(_exif!);
  85. }
  86. final bool showExifListTile = _exifData["focalLength"] != null ||
  87. _exifData["fNumber"] != null ||
  88. _exifData["takenOnDevice"] != null ||
  89. _exifData["exposureTime"] != null ||
  90. _exifData["ISO"] != null;
  91. final bool showDimension =
  92. _exifData["resolution"] != null && _exifData["megaPixels"] != null;
  93. final listTiles = <Widget?>[
  94. !widget.file.isUploaded ||
  95. (!isFileOwner && (widget.file.caption?.isEmpty ?? true))
  96. ? const SizedBox.shrink()
  97. : Padding(
  98. padding: const EdgeInsets.only(top: 8, bottom: 4),
  99. child: isFileOwner
  100. ? FileCaptionWidget(file: widget.file)
  101. : FileCaptionReadyOnly(caption: widget.file.caption!),
  102. ),
  103. ListTile(
  104. horizontalTitleGap: 2,
  105. leading: const Padding(
  106. padding: EdgeInsets.only(top: 8),
  107. child: Icon(Icons.calendar_today_rounded),
  108. ),
  109. title: Text(
  110. getFullDate(
  111. DateTime.fromMicrosecondsSinceEpoch(file.creationTime!),
  112. ),
  113. ),
  114. subtitle: Text(
  115. getTimeIn12hrFormat(dateTime) + " " + dateTime.timeZoneName,
  116. style: Theme.of(context).textTheme.bodyText2!.copyWith(
  117. color: Theme.of(context)
  118. .colorScheme
  119. .defaultTextColor
  120. .withOpacity(0.5),
  121. ),
  122. ),
  123. trailing: (widget.file.ownerID == null ||
  124. widget.file.ownerID == _currentUserID) &&
  125. widget.file.uploadedFileID != null
  126. ? IconButton(
  127. onPressed: () {
  128. _showDateTimePicker(widget.file);
  129. },
  130. icon: const Icon(Icons.edit),
  131. )
  132. : const SizedBox.shrink(),
  133. ),
  134. ListTile(
  135. horizontalTitleGap: 2,
  136. leading: _isImage
  137. ? const Padding(
  138. padding: EdgeInsets.only(top: 8),
  139. child: Icon(
  140. Icons.image,
  141. ),
  142. )
  143. : const Padding(
  144. padding: EdgeInsets.only(top: 8),
  145. child: Icon(
  146. Icons.video_camera_back,
  147. size: 27,
  148. ),
  149. ),
  150. title: Text(
  151. file.displayName,
  152. ),
  153. subtitle: Row(
  154. children: [
  155. showDimension
  156. ? Text(
  157. "${_exifData["megaPixels"]}MP "
  158. "${_exifData["resolution"]} ",
  159. )
  160. : const SizedBox.shrink(),
  161. _getFileSize(),
  162. (file.fileType == FileType.video) &&
  163. (file.localID != null || file.duration != 0)
  164. ? Padding(
  165. padding: const EdgeInsets.only(left: 8.0),
  166. child: _getVideoDuration(),
  167. )
  168. : const SizedBox.shrink(),
  169. ],
  170. ),
  171. trailing: file.uploadedFileID == null || file.ownerID != _currentUserID
  172. ? const SizedBox.shrink()
  173. : IconButton(
  174. onPressed: () async {
  175. await editFilename(context, file);
  176. setState(() {});
  177. },
  178. icon: const Icon(Icons.edit),
  179. ),
  180. ),
  181. showExifListTile
  182. ? ListTile(
  183. horizontalTitleGap: 2,
  184. leading: const Icon(Icons.camera_rounded),
  185. title: Text(_exifData["takenOnDevice"] ?? "--"),
  186. subtitle: Row(
  187. children: [
  188. _exifData["fNumber"] != null
  189. ? Padding(
  190. padding: const EdgeInsets.only(right: 10),
  191. child: Text('ƒ/' + _exifData["fNumber"].toString()),
  192. )
  193. : const SizedBox.shrink(),
  194. _exifData["exposureTime"] != null
  195. ? Padding(
  196. padding: const EdgeInsets.only(right: 10),
  197. child: Text(_exifData["exposureTime"]),
  198. )
  199. : const SizedBox.shrink(),
  200. _exifData["focalLength"] != null
  201. ? Padding(
  202. padding: const EdgeInsets.only(right: 10),
  203. child:
  204. Text(_exifData["focalLength"].toString() + "mm"),
  205. )
  206. : const SizedBox.shrink(),
  207. _exifData["ISO"] != null
  208. ? Padding(
  209. padding: const EdgeInsets.only(right: 10),
  210. child: Text("ISO" + _exifData["ISO"].toString()),
  211. )
  212. : const SizedBox.shrink(),
  213. ],
  214. ),
  215. )
  216. : null,
  217. SizedBox(
  218. height: 62,
  219. child: ListTile(
  220. horizontalTitleGap: 0,
  221. leading: const Icon(Icons.folder_outlined),
  222. title: fileIsBackedup
  223. ? CollectionsListOfFileWidget(
  224. allCollectionIDsOfFile,
  225. _currentUserID!,
  226. )
  227. : DeviceFoldersListOfFileWidget(allDeviceFoldersOfFile),
  228. ),
  229. ),
  230. FeatureFlagService.instance.isInternalUserOrDebugBuild()
  231. ? SizedBox(
  232. height: 62,
  233. child: ListTile(
  234. horizontalTitleGap: 0,
  235. leading: const Icon(Icons.image_search),
  236. title: ObjectTagsWidget(file),
  237. ),
  238. )
  239. : null,
  240. (file.uploadedFileID != null && file.updationTime != null)
  241. ? ListTile(
  242. horizontalTitleGap: 2,
  243. leading: const Padding(
  244. padding: EdgeInsets.only(top: 8),
  245. child: Icon(Icons.cloud_upload_outlined),
  246. ),
  247. title: Text(
  248. getFullDate(
  249. DateTime.fromMicrosecondsSinceEpoch(file.updationTime!),
  250. ),
  251. ),
  252. subtitle: Text(
  253. getTimeIn12hrFormat(dateTimeForUpdationTime) +
  254. " " +
  255. dateTimeForUpdationTime.timeZoneName,
  256. style: Theme.of(context).textTheme.bodyText2!.copyWith(
  257. color: Theme.of(context)
  258. .colorScheme
  259. .defaultTextColor
  260. .withOpacity(0.5),
  261. ),
  262. ),
  263. )
  264. : null,
  265. _isImage ? RawExifListTileWidget(_exif, widget.file) : null,
  266. ];
  267. listTiles.removeWhere(
  268. (element) => element == null,
  269. );
  270. return SafeArea(
  271. top: false,
  272. child: Scrollbar(
  273. thickness: 4,
  274. radius: const Radius.circular(2),
  275. thumbVisibility: true,
  276. child: Padding(
  277. padding: const EdgeInsets.all(8.0),
  278. child: CustomScrollView(
  279. physics: const ClampingScrollPhysics(),
  280. shrinkWrap: true,
  281. slivers: <Widget>[
  282. TitleBarWidget(
  283. isFlexibleSpaceDisabled: true,
  284. title: "Details",
  285. isOnTopOfScreen: false,
  286. backgroundColor: getEnteColorScheme(context).backgroundElevated,
  287. leading: IconButtonWidget(
  288. icon: Icons.close_outlined,
  289. iconButtonType: IconButtonType.primary,
  290. onTap: () => Navigator.pop(context),
  291. ),
  292. ),
  293. SliverToBoxAdapter(child: addedBy(widget.file)),
  294. SliverList(
  295. delegate: SliverChildBuilderDelegate(
  296. (context, index) {
  297. if (index.isOdd) {
  298. return index == 1
  299. ? const SizedBox.shrink()
  300. : const DividerWidget(
  301. dividerType: DividerType.menu,
  302. );
  303. } else {
  304. return listTiles[index ~/ 2];
  305. }
  306. },
  307. childCount: (listTiles.length * 2) - 1,
  308. ),
  309. )
  310. ],
  311. ),
  312. ),
  313. ),
  314. );
  315. }
  316. Widget addedBy(File file) {
  317. if (file.uploadedFileID == null) {
  318. return const SizedBox.shrink();
  319. }
  320. String? addedBy;
  321. if (file.ownerID == _currentUserID) {
  322. if (file.pubMagicMetadata!.uploaderName != null) {
  323. addedBy = file.pubMagicMetadata!.uploaderName;
  324. }
  325. } else {
  326. final fileOwner = CollectionsService.instance
  327. .getFileOwner(file.ownerID!, file.collectionID);
  328. addedBy = fileOwner.email;
  329. }
  330. if (addedBy == null || addedBy.isEmpty) {
  331. return const SizedBox.shrink();
  332. }
  333. final enteTheme = Theme.of(context).colorScheme.enteTheme;
  334. return Padding(
  335. padding: const EdgeInsets.only(top: 4.0, bottom: 4.0, left: 16),
  336. child: Text(
  337. "Added by $addedBy",
  338. style: enteTheme.textTheme.mini
  339. .copyWith(color: enteTheme.colorScheme.textMuted),
  340. ),
  341. );
  342. }
  343. _generateExifForDetails(Map<String, IfdTag> exif) {
  344. if (exif["EXIF FocalLength"] != null) {
  345. _exifData["focalLength"] =
  346. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio).numerator /
  347. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio)
  348. .denominator;
  349. }
  350. if (exif["EXIF FNumber"] != null) {
  351. _exifData["fNumber"] =
  352. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).numerator /
  353. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).denominator;
  354. }
  355. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  356. final imageLength = exif["EXIF ExifImageLength"] ??
  357. exif["Image "
  358. "ImageLength"];
  359. if (imageWidth != null && imageLength != null) {
  360. _exifData["resolution"] = '$imageWidth x $imageLength';
  361. _exifData['megaPixels'] =
  362. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  363. 1000000)
  364. .toStringAsFixed(1);
  365. } else {
  366. debugPrint("No image width/height");
  367. }
  368. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  369. _exifData["takenOnDevice"] =
  370. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  371. }
  372. if (exif["EXIF ExposureTime"] != null) {
  373. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  374. }
  375. if (exif["EXIF ISOSpeedRatings"] != null) {
  376. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  377. }
  378. }
  379. Widget _getFileSize() {
  380. Future<int> fileSizeFuture;
  381. if (widget.file.fileSize != null) {
  382. fileSizeFuture = Future.value(widget.file.fileSize);
  383. } else {
  384. fileSizeFuture = getFile(widget.file).then((f) => f!.length());
  385. }
  386. return FutureBuilder<int>(
  387. future: fileSizeFuture,
  388. builder: (context, snapshot) {
  389. if (snapshot.hasData) {
  390. return Text(
  391. (snapshot.data! / (1024 * 1024)).toStringAsFixed(2) + " MB",
  392. );
  393. } else {
  394. return Center(
  395. child: SizedBox.fromSize(
  396. size: const Size.square(24),
  397. child: const CupertinoActivityIndicator(
  398. radius: 8,
  399. ),
  400. ),
  401. );
  402. }
  403. },
  404. );
  405. }
  406. Widget _getVideoDuration() {
  407. if (widget.file.duration != 0) {
  408. return Text(
  409. secondsToHHMMSS(widget.file.duration!),
  410. );
  411. }
  412. return FutureBuilder<AssetEntity?>(
  413. future: widget.file.getAsset,
  414. builder: (context, snapshot) {
  415. if (snapshot.hasData) {
  416. return Text(
  417. snapshot.data!.videoDuration.toString().split(".")[0],
  418. );
  419. } else {
  420. return Center(
  421. child: SizedBox.fromSize(
  422. size: const Size.square(24),
  423. child: const CupertinoActivityIndicator(
  424. radius: 8,
  425. ),
  426. ),
  427. );
  428. }
  429. },
  430. );
  431. }
  432. void _showDateTimePicker(File file) async {
  433. final dateResult = await DatePicker.showDatePicker(
  434. context,
  435. minTime: DateTime(1800, 1, 1),
  436. maxTime: DateTime.now(),
  437. currentTime: DateTime.fromMicrosecondsSinceEpoch(file.creationTime!),
  438. locale: LocaleType.en,
  439. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  440. );
  441. if (dateResult == null) {
  442. return;
  443. }
  444. final dateWithTimeResult = await DatePicker.showTime12hPicker(
  445. context,
  446. showTitleActions: true,
  447. currentTime: dateResult,
  448. locale: LocaleType.en,
  449. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  450. );
  451. if (dateWithTimeResult != null) {
  452. if (await editTime(
  453. context,
  454. List.of([widget.file]),
  455. dateWithTimeResult.microsecondsSinceEpoch,
  456. )) {
  457. widget.file.creationTime = dateWithTimeResult.microsecondsSinceEpoch;
  458. setState(() {});
  459. }
  460. }
  461. }
  462. }