file_info_widget.dart 15 KB

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