file_info_widget.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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/object_tags_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. file.displayName,
  151. ),
  152. subtitle: Row(
  153. children: [
  154. showDimension
  155. ? Text(
  156. "${_exifData["megaPixels"]}MP "
  157. "${_exifData["resolution"]} ",
  158. )
  159. : const SizedBox.shrink(),
  160. _getFileSize(),
  161. (file.fileType == FileType.video) &&
  162. (file.localID != null || file.duration != 0)
  163. ? Padding(
  164. padding: const EdgeInsets.only(left: 8.0),
  165. child: _getVideoDuration(),
  166. )
  167. : const SizedBox.shrink(),
  168. ],
  169. ),
  170. trailing: file.uploadedFileID == null || file.ownerID != _currentUserID
  171. ? const SizedBox.shrink()
  172. : IconButton(
  173. onPressed: () async {
  174. await editFilename(context, file);
  175. setState(() {});
  176. },
  177. icon: const Icon(Icons.edit),
  178. ),
  179. ),
  180. showExifListTile
  181. ? ListTile(
  182. horizontalTitleGap: 2,
  183. leading: const Icon(Icons.camera_rounded),
  184. title: Text(_exifData["takenOnDevice"] ?? "--"),
  185. subtitle: Row(
  186. children: [
  187. _exifData["fNumber"] != null
  188. ? Padding(
  189. padding: const EdgeInsets.only(right: 10),
  190. child: Text('ƒ/' + _exifData["fNumber"].toString()),
  191. )
  192. : const SizedBox.shrink(),
  193. _exifData["exposureTime"] != null
  194. ? Padding(
  195. padding: const EdgeInsets.only(right: 10),
  196. child: Text(_exifData["exposureTime"]),
  197. )
  198. : const SizedBox.shrink(),
  199. _exifData["focalLength"] != null
  200. ? Padding(
  201. padding: const EdgeInsets.only(right: 10),
  202. child:
  203. Text(_exifData["focalLength"].toString() + "mm"),
  204. )
  205. : const SizedBox.shrink(),
  206. _exifData["ISO"] != null
  207. ? Padding(
  208. padding: const EdgeInsets.only(right: 10),
  209. child: Text("ISO" + _exifData["ISO"].toString()),
  210. )
  211. : const SizedBox.shrink(),
  212. ],
  213. ),
  214. )
  215. : null,
  216. SizedBox(
  217. height: 62,
  218. child: ListTile(
  219. horizontalTitleGap: 0,
  220. leading: const Icon(Icons.folder_outlined),
  221. title: fileIsBackedup
  222. ? CollectionsListOfFileWidget(
  223. allCollectionIDsOfFile,
  224. _currentUserID!,
  225. )
  226. : DeviceFoldersListOfFileWidget(allDeviceFoldersOfFile),
  227. ),
  228. ),
  229. SizedBox(
  230. height: 62,
  231. child: ListTile(
  232. horizontalTitleGap: 0,
  233. leading: const Icon(Icons.image_search),
  234. title: ObjectTagsWidget(file),
  235. ),
  236. ),
  237. (file.uploadedFileID != null && file.updationTime != null)
  238. ? ListTile(
  239. horizontalTitleGap: 2,
  240. leading: const Padding(
  241. padding: EdgeInsets.only(top: 8),
  242. child: Icon(Icons.cloud_upload_outlined),
  243. ),
  244. title: Text(
  245. getFullDate(
  246. DateTime.fromMicrosecondsSinceEpoch(file.updationTime!),
  247. ),
  248. ),
  249. subtitle: Text(
  250. getTimeIn12hrFormat(dateTimeForUpdationTime) +
  251. " " +
  252. dateTimeForUpdationTime.timeZoneName,
  253. style: Theme.of(context).textTheme.bodyText2!.copyWith(
  254. color: Theme.of(context)
  255. .colorScheme
  256. .defaultTextColor
  257. .withOpacity(0.5),
  258. ),
  259. ),
  260. )
  261. : null,
  262. _isImage ? RawExifListTileWidget(_exif, widget.file) : null,
  263. ];
  264. listTiles.removeWhere(
  265. (element) => element == null,
  266. );
  267. return SafeArea(
  268. top: false,
  269. child: Scrollbar(
  270. thickness: 4,
  271. radius: const Radius.circular(2),
  272. thumbVisibility: true,
  273. child: Padding(
  274. padding: const EdgeInsets.all(8.0),
  275. child: CustomScrollView(
  276. physics: const ClampingScrollPhysics(),
  277. shrinkWrap: true,
  278. slivers: <Widget>[
  279. TitleBarWidget(
  280. isFlexibleSpaceDisabled: true,
  281. title: "Details",
  282. isOnTopOfScreen: false,
  283. backgroundColor: getEnteColorScheme(context).backgroundElevated,
  284. leading: IconButtonWidget(
  285. icon: Icons.close_outlined,
  286. iconButtonType: IconButtonType.primary,
  287. onTap: () => Navigator.pop(context),
  288. ),
  289. ),
  290. SliverToBoxAdapter(child: addedBy(widget.file)),
  291. SliverList(
  292. delegate: SliverChildBuilderDelegate(
  293. (context, index) {
  294. if (index.isOdd) {
  295. return index == 1
  296. ? const SizedBox.shrink()
  297. : const DividerWidget(
  298. dividerType: DividerType.menu,
  299. );
  300. } else {
  301. return listTiles[index ~/ 2];
  302. }
  303. },
  304. childCount: (listTiles.length * 2) - 1,
  305. ),
  306. )
  307. ],
  308. ),
  309. ),
  310. ),
  311. );
  312. }
  313. Widget addedBy(File file) {
  314. if (file.uploadedFileID == null) {
  315. return const SizedBox.shrink();
  316. }
  317. String? addedBy;
  318. if (file.ownerID == _currentUserID) {
  319. if (file.pubMagicMetadata!.uploaderName != null) {
  320. addedBy = file.pubMagicMetadata!.uploaderName;
  321. }
  322. } else {
  323. final fileOwner = CollectionsService.instance
  324. .getFileOwner(file.ownerID!, file.collectionID);
  325. addedBy = fileOwner.email;
  326. }
  327. if (addedBy == null || addedBy.isEmpty) {
  328. return const SizedBox.shrink();
  329. }
  330. final enteTheme = Theme.of(context).colorScheme.enteTheme;
  331. return Padding(
  332. padding: const EdgeInsets.only(top: 4.0, bottom: 4.0, left: 16),
  333. child: Text(
  334. "Added by $addedBy",
  335. style: enteTheme.textTheme.mini
  336. .copyWith(color: enteTheme.colorScheme.textMuted),
  337. ),
  338. );
  339. }
  340. _generateExifForDetails(Map<String, IfdTag> exif) {
  341. if (exif["EXIF FocalLength"] != null) {
  342. _exifData["focalLength"] =
  343. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio).numerator /
  344. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio)
  345. .denominator;
  346. }
  347. if (exif["EXIF FNumber"] != null) {
  348. _exifData["fNumber"] =
  349. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).numerator /
  350. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).denominator;
  351. }
  352. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  353. final imageLength = exif["EXIF ExifImageLength"] ??
  354. exif["Image "
  355. "ImageLength"];
  356. if (imageWidth != null && imageLength != null) {
  357. _exifData["resolution"] = '$imageWidth x $imageLength';
  358. _exifData['megaPixels'] =
  359. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  360. 1000000)
  361. .toStringAsFixed(1);
  362. } else {
  363. debugPrint("No image width/height");
  364. }
  365. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  366. _exifData["takenOnDevice"] =
  367. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  368. }
  369. if (exif["EXIF ExposureTime"] != null) {
  370. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  371. }
  372. if (exif["EXIF ISOSpeedRatings"] != null) {
  373. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  374. }
  375. }
  376. Widget _getFileSize() {
  377. Future<int> fileSizeFuture;
  378. if (widget.file.fileSize != null) {
  379. fileSizeFuture = Future.value(widget.file.fileSize);
  380. } else {
  381. fileSizeFuture = getFile(widget.file).then((f) => f!.length());
  382. }
  383. return FutureBuilder<int>(
  384. future: fileSizeFuture,
  385. builder: (context, snapshot) {
  386. if (snapshot.hasData) {
  387. return Text(
  388. (snapshot.data! / (1024 * 1024)).toStringAsFixed(2) + " MB",
  389. );
  390. } else {
  391. return Center(
  392. child: SizedBox.fromSize(
  393. size: const Size.square(24),
  394. child: const CupertinoActivityIndicator(
  395. radius: 8,
  396. ),
  397. ),
  398. );
  399. }
  400. },
  401. );
  402. }
  403. Widget _getVideoDuration() {
  404. if (widget.file.duration != 0) {
  405. return Text(
  406. secondsToHHMMSS(widget.file.duration!),
  407. );
  408. }
  409. return FutureBuilder<AssetEntity?>(
  410. future: widget.file.getAsset,
  411. builder: (context, snapshot) {
  412. if (snapshot.hasData) {
  413. return Text(
  414. snapshot.data!.videoDuration.toString().split(".")[0],
  415. );
  416. } else {
  417. return Center(
  418. child: SizedBox.fromSize(
  419. size: const Size.square(24),
  420. child: const CupertinoActivityIndicator(
  421. radius: 8,
  422. ),
  423. ),
  424. );
  425. }
  426. },
  427. );
  428. }
  429. void _showDateTimePicker(File file) async {
  430. final dateResult = await DatePicker.showDatePicker(
  431. context,
  432. minTime: DateTime(1800, 1, 1),
  433. maxTime: DateTime.now(),
  434. currentTime: DateTime.fromMicrosecondsSinceEpoch(file.creationTime!),
  435. locale: LocaleType.en,
  436. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  437. );
  438. if (dateResult == null) {
  439. return;
  440. }
  441. final dateWithTimeResult = await DatePicker.showTime12hPicker(
  442. context,
  443. showTitleActions: true,
  444. currentTime: dateResult,
  445. locale: LocaleType.en,
  446. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  447. );
  448. if (dateWithTimeResult != null) {
  449. if (await editTime(
  450. context,
  451. List.of([widget.file]),
  452. dateWithTimeResult.microsecondsSinceEpoch,
  453. )) {
  454. widget.file.creationTime = dateWithTimeResult.microsecondsSinceEpoch;
  455. setState(() {});
  456. }
  457. }
  458. }
  459. }