file_info_widget.dart 16 KB

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