file_info_widget.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. final bool isFileOwner =
  69. file.ownerID == null || file.ownerID == _currentUserID;
  70. Future<Set<int>> allCollectionIDsOfFile;
  71. Future<Set<String>>
  72. allDeviceFoldersOfFile; //Typing this as Future<Set<T>> as it would be easier to implement showing multiple device folders for a file in the future
  73. if (fileIsBackedup) {
  74. allCollectionIDsOfFile = FilesDB.instance.getAllCollectionIDsOfFile(
  75. file.uploadedFileID,
  76. );
  77. } else {
  78. allDeviceFoldersOfFile = Future.sync(() => {file.deviceFolder});
  79. }
  80. final dateTime = DateTime.fromMicrosecondsSinceEpoch(file.creationTime);
  81. final dateTimeForUpdationTime =
  82. DateTime.fromMicrosecondsSinceEpoch(file.updationTime);
  83. if (_isImage && _exif != null) {
  84. _generateExifForDetails(_exif);
  85. }
  86. final bool showExifListTile = _exifData["focalLength"] != null ||
  87. _exifData["fNumber"] != null ||
  88. _exifData["takenOnDevice"] != null ||
  89. _exifData["exposureTime"] != null ||
  90. _exifData["ISO"] != null;
  91. final bool showDimension =
  92. _exifData["resolution"] != null && _exifData["megaPixels"] != null;
  93. final listTiles = <Widget>[
  94. !widget.file.isUploaded ||
  95. (!isFileOwner && (widget.file.caption?.isEmpty ?? true))
  96. ? const SizedBox.shrink()
  97. : Padding(
  98. padding: const EdgeInsets.only(top: 8, bottom: 4),
  99. child: isFileOwner
  100. ? FileCaptionWidget(file: widget.file)
  101. : FileCaptionReadyOnly(caption: widget.file.caption),
  102. ),
  103. ListTile(
  104. horizontalTitleGap: 2,
  105. leading: const Padding(
  106. padding: EdgeInsets.only(top: 8),
  107. child: Icon(Icons.calendar_today_rounded),
  108. ),
  109. title: Text(
  110. getFullDate(
  111. DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  112. ),
  113. ),
  114. subtitle: Text(
  115. getTimeIn12hrFormat(dateTime) + " " + dateTime.timeZoneName,
  116. style: Theme.of(context).textTheme.bodyText2.copyWith(
  117. color: Theme.of(context)
  118. .colorScheme
  119. .defaultTextColor
  120. .withOpacity(0.5),
  121. ),
  122. ),
  123. trailing: (widget.file.ownerID == null ||
  124. widget.file.ownerID == _currentUserID) &&
  125. widget.file.uploadedFileID != null
  126. ? IconButton(
  127. onPressed: () {
  128. _showDateTimePicker(widget.file);
  129. },
  130. icon: const Icon(Icons.edit),
  131. )
  132. : const SizedBox.shrink(),
  133. ),
  134. ListTile(
  135. horizontalTitleGap: 2,
  136. leading: _isImage
  137. ? const Padding(
  138. padding: EdgeInsets.only(top: 8),
  139. child: Icon(
  140. Icons.image,
  141. ),
  142. )
  143. : const Padding(
  144. padding: EdgeInsets.only(top: 8),
  145. child: Icon(
  146. Icons.video_camera_back,
  147. size: 27,
  148. ),
  149. ),
  150. title: Text(
  151. file.displayName,
  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. if (fileOwner != null) {
  319. addedBy = fileOwner.email;
  320. }
  321. }
  322. if (addedBy == null || addedBy.isEmpty) {
  323. return const SizedBox.shrink();
  324. }
  325. final enteTheme = Theme.of(context).colorScheme.enteTheme;
  326. return Padding(
  327. padding: const EdgeInsets.only(top: 4.0, bottom: 4.0, left: 16),
  328. child: Text(
  329. "Added by $addedBy",
  330. style: enteTheme.textTheme.mini
  331. .copyWith(color: enteTheme.colorScheme.textMuted),
  332. ),
  333. );
  334. }
  335. _generateExifForDetails(Map<String, IfdTag> exif) {
  336. if (exif["EXIF FocalLength"] != null) {
  337. _exifData["focalLength"] =
  338. (exif["EXIF FocalLength"].values.toList()[0] as Ratio).numerator /
  339. (exif["EXIF FocalLength"].values.toList()[0] as Ratio)
  340. .denominator;
  341. }
  342. if (exif["EXIF FNumber"] != null) {
  343. _exifData["fNumber"] =
  344. (exif["EXIF FNumber"].values.toList()[0] as Ratio).numerator /
  345. (exif["EXIF FNumber"].values.toList()[0] as Ratio).denominator;
  346. }
  347. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  348. final imageLength = exif["EXIF ExifImageLength"] ??
  349. exif["Image "
  350. "ImageLength"];
  351. if (imageWidth != null && imageLength != null) {
  352. _exifData["resolution"] = '$imageWidth x $imageLength';
  353. _exifData['megaPixels'] =
  354. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  355. 1000000)
  356. .toStringAsFixed(1);
  357. } else {
  358. debugPrint("No image width/height");
  359. }
  360. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  361. _exifData["takenOnDevice"] =
  362. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  363. }
  364. if (exif["EXIF ExposureTime"] != null) {
  365. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  366. }
  367. if (exif["EXIF ISOSpeedRatings"] != null) {
  368. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  369. }
  370. }
  371. Widget _getFileSize() {
  372. Future<int> fileSizeFuture;
  373. if (widget.file.fileSize != null) {
  374. fileSizeFuture = Future.value(widget.file.fileSize);
  375. } else {
  376. fileSizeFuture = getFile(widget.file).then((f) => f.length());
  377. }
  378. return FutureBuilder(
  379. future: fileSizeFuture,
  380. builder: (context, snapshot) {
  381. if (snapshot.hasData) {
  382. return Text(
  383. (snapshot.data / (1024 * 1024)).toStringAsFixed(2) + " MB",
  384. );
  385. } else {
  386. return Center(
  387. child: SizedBox.fromSize(
  388. size: const Size.square(24),
  389. child: const CupertinoActivityIndicator(
  390. radius: 8,
  391. ),
  392. ),
  393. );
  394. }
  395. },
  396. );
  397. }
  398. Widget _getVideoDuration() {
  399. if (widget.file.duration != 0) {
  400. return Text(
  401. secondsToHHMMSS(widget.file.duration),
  402. );
  403. }
  404. return FutureBuilder(
  405. future: widget.file.getAsset,
  406. builder: (context, snapshot) {
  407. if (snapshot.hasData) {
  408. return Text(
  409. snapshot.data.videoDuration.toString().split(".")[0],
  410. );
  411. } else {
  412. return Center(
  413. child: SizedBox.fromSize(
  414. size: const Size.square(24),
  415. child: const CupertinoActivityIndicator(
  416. radius: 8,
  417. ),
  418. ),
  419. );
  420. }
  421. },
  422. );
  423. }
  424. void _showDateTimePicker(File file) async {
  425. final dateResult = await DatePicker.showDatePicker(
  426. context,
  427. minTime: DateTime(1800, 1, 1),
  428. maxTime: DateTime.now(),
  429. currentTime: DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  430. locale: LocaleType.en,
  431. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  432. );
  433. if (dateResult == null) {
  434. return;
  435. }
  436. final dateWithTimeResult = await DatePicker.showTime12hPicker(
  437. context,
  438. showTitleActions: true,
  439. currentTime: dateResult,
  440. locale: LocaleType.en,
  441. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  442. );
  443. if (dateWithTimeResult != null) {
  444. if (await editTime(
  445. context,
  446. List.of([widget.file]),
  447. dateWithTimeResult.microsecondsSinceEpoch,
  448. )) {
  449. widget.file.creationTime = dateWithTimeResult.microsecondsSinceEpoch;
  450. setState(() {});
  451. }
  452. }
  453. }
  454. }