file_info_widget.dart 15 KB

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