file_info_widget.dart 16 KB

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