file_info_widget.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // @dart=2.9
  2. import "package:exif/exif.dart";
  3. import "package:flutter/cupertino.dart";
  4. import "package:flutter/material.dart";
  5. import 'package:flutter_datetime_picker/flutter_datetime_picker.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/theme/ente_theme.dart';
  12. import 'package:photos/ui/components/divider_widget.dart';
  13. import 'package:photos/ui/components/icon_button_widget.dart';
  14. import 'package:photos/ui/components/title_bar_widget.dart';
  15. import 'package:photos/ui/viewer/file/collections_list_of_file_widget.dart';
  16. import 'package:photos/ui/viewer/file/device_folders_list_of_file_widget.dart';
  17. import 'package:photos/ui/viewer/file/file_caption_widget.dart';
  18. import 'package:photos/ui/viewer/file/raw_exif_list_tile_widget.dart';
  19. import "package:photos/utils/date_time_util.dart";
  20. import "package:photos/utils/exif_util.dart";
  21. import "package:photos/utils/file_util.dart";
  22. import "package:photos/utils/magic_util.dart";
  23. class FileInfoWidget extends StatefulWidget {
  24. final File file;
  25. const FileInfoWidget(
  26. this.file, {
  27. Key key,
  28. }) : super(key: key);
  29. @override
  30. State<FileInfoWidget> createState() => _FileInfoWidgetState();
  31. }
  32. class _FileInfoWidgetState extends State<FileInfoWidget> {
  33. Map<String, IfdTag> _exif;
  34. final Map<String, dynamic> _exifData = {
  35. "focalLength": null,
  36. "fNumber": null,
  37. "resolution": null,
  38. "takenOnDevice": null,
  39. "exposureTime": null,
  40. "ISO": null,
  41. "megaPixels": null
  42. };
  43. bool _isImage = false;
  44. @override
  45. void initState() {
  46. debugPrint('file_info_dialog initState');
  47. _isImage = widget.file.fileType == FileType.image ||
  48. widget.file.fileType == FileType.livePhoto;
  49. if (_isImage) {
  50. getExif(widget.file).then((exif) {
  51. if (mounted) {
  52. setState(() {
  53. _exif = exif;
  54. });
  55. }
  56. });
  57. }
  58. super.initState();
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. final file = widget.file;
  63. final fileIsBackedup = file.uploadedFileID == null ? false : true;
  64. Future<Set<int>> allCollectionIDsOfFile;
  65. Future<Set<String>>
  66. allDeviceFoldersOfFile; //Typing this as Future<Set<T>> as it would be easier to implement showing multiple device folders for a file in the future
  67. if (fileIsBackedup) {
  68. allCollectionIDsOfFile = FilesDB.instance.getAllCollectionIDsOfFile(
  69. file.uploadedFileID,
  70. );
  71. } else {
  72. allDeviceFoldersOfFile = Future.sync(() => {file.deviceFolder});
  73. }
  74. final dateTime = DateTime.fromMicrosecondsSinceEpoch(file.creationTime);
  75. final dateTimeForUpdationTime =
  76. DateTime.fromMicrosecondsSinceEpoch(file.updationTime);
  77. if (_isImage && _exif != null) {
  78. _generateExifForDetails(_exif);
  79. }
  80. final bool showExifListTile = _exifData["focalLength"] != null ||
  81. _exifData["fNumber"] != null ||
  82. _exifData["takenOnDevice"] != null ||
  83. _exifData["exposureTime"] != null ||
  84. _exifData["ISO"] != null;
  85. final bool showDimension =
  86. _exifData["resolution"] != null && _exifData["megaPixels"] != null;
  87. final listTiles = <Widget>[
  88. widget.file.uploadedFileID == null ||
  89. Configuration.instance.getUserID() != file.ownerID
  90. ? const SizedBox.shrink()
  91. : Padding(
  92. padding: const EdgeInsets.only(top: 8, bottom: 4),
  93. child: FileCaptionWidget(file: widget.file),
  94. ),
  95. ListTile(
  96. horizontalTitleGap: 2,
  97. leading: const Padding(
  98. padding: EdgeInsets.only(top: 8),
  99. child: Icon(Icons.calendar_today_rounded),
  100. ),
  101. title: Text(
  102. getFullDate(
  103. DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  104. ),
  105. ),
  106. subtitle: Text(
  107. getTimeIn12hrFormat(dateTime) + " " + dateTime.timeZoneName,
  108. style: Theme.of(context).textTheme.bodyText2.copyWith(
  109. color: Theme.of(context)
  110. .colorScheme
  111. .defaultTextColor
  112. .withOpacity(0.5),
  113. ),
  114. ),
  115. trailing: (widget.file.ownerID == null ||
  116. widget.file.ownerID ==
  117. Configuration.instance.getUserID()) &&
  118. widget.file.uploadedFileID != null
  119. ? IconButton(
  120. onPressed: () {
  121. _showDateTimePicker(widget.file);
  122. },
  123. icon: const Icon(Icons.edit),
  124. )
  125. : const SizedBox.shrink(),
  126. ),
  127. ListTile(
  128. horizontalTitleGap: 2,
  129. leading: _isImage
  130. ? const Padding(
  131. padding: EdgeInsets.only(top: 8),
  132. child: Icon(
  133. Icons.image,
  134. ),
  135. )
  136. : const Padding(
  137. padding: EdgeInsets.only(top: 8),
  138. child: Icon(
  139. Icons.video_camera_back,
  140. size: 27,
  141. ),
  142. ),
  143. title: Text(
  144. file.displayName,
  145. ),
  146. subtitle: Row(
  147. children: [
  148. showDimension
  149. ? Text(
  150. "${_exifData["megaPixels"]}MP "
  151. "${_exifData["resolution"]} ",
  152. )
  153. : const SizedBox.shrink(),
  154. _getFileSize(),
  155. (file.fileType == FileType.video) &&
  156. (file.localID != null || file.duration != 0)
  157. ? Padding(
  158. padding: const EdgeInsets.only(left: 8.0),
  159. child: _getVideoDuration(),
  160. )
  161. : const SizedBox.shrink(),
  162. ],
  163. ),
  164. trailing: file.uploadedFileID == null ||
  165. file.ownerID != Configuration.instance.getUserID()
  166. ? const SizedBox.shrink()
  167. : IconButton(
  168. onPressed: () async {
  169. await editFilename(context, file);
  170. setState(() {});
  171. },
  172. icon: const Icon(Icons.edit),
  173. ),
  174. ),
  175. showExifListTile
  176. ? ListTile(
  177. horizontalTitleGap: 2,
  178. leading: const Icon(Icons.camera_rounded),
  179. title: Text(_exifData["takenOnDevice"] ?? "--"),
  180. subtitle: Row(
  181. children: [
  182. _exifData["fNumber"] != null
  183. ? Padding(
  184. padding: const EdgeInsets.only(right: 10),
  185. child: Text('ƒ/' + _exifData["fNumber"].toString()),
  186. )
  187. : const SizedBox.shrink(),
  188. _exifData["exposureTime"] != null
  189. ? Padding(
  190. padding: const EdgeInsets.only(right: 10),
  191. child: Text(_exifData["exposureTime"]),
  192. )
  193. : const SizedBox.shrink(),
  194. _exifData["focalLength"] != null
  195. ? Padding(
  196. padding: const EdgeInsets.only(right: 10),
  197. child:
  198. Text(_exifData["focalLength"].toString() + "mm"),
  199. )
  200. : const SizedBox.shrink(),
  201. _exifData["ISO"] != null
  202. ? Padding(
  203. padding: const EdgeInsets.only(right: 10),
  204. child: Text("ISO" + _exifData["ISO"].toString()),
  205. )
  206. : const SizedBox.shrink(),
  207. ],
  208. ),
  209. )
  210. : null,
  211. SizedBox(
  212. height: 62,
  213. child: ListTile(
  214. horizontalTitleGap: 0,
  215. leading: const Icon(Icons.folder_outlined),
  216. title: fileIsBackedup
  217. ? CollectionsListOfFileWidget(allCollectionIDsOfFile)
  218. : DeviceFoldersListOfFileWidget(allDeviceFoldersOfFile),
  219. ),
  220. ),
  221. (file.uploadedFileID != null && file.updationTime != null)
  222. ? ListTile(
  223. horizontalTitleGap: 2,
  224. leading: const Padding(
  225. padding: EdgeInsets.only(top: 8),
  226. child: Icon(Icons.cloud_upload_outlined),
  227. ),
  228. title: Text(
  229. getFullDate(
  230. DateTime.fromMicrosecondsSinceEpoch(file.updationTime),
  231. ),
  232. ),
  233. subtitle: Text(
  234. getTimeIn12hrFormat(dateTimeForUpdationTime) +
  235. " " +
  236. dateTimeForUpdationTime.timeZoneName,
  237. style: Theme.of(context).textTheme.bodyText2.copyWith(
  238. color: Theme.of(context)
  239. .colorScheme
  240. .defaultTextColor
  241. .withOpacity(0.5),
  242. ),
  243. ),
  244. )
  245. : null,
  246. _isImage ? RawExifListTileWidget(_exif, widget.file) : null,
  247. ];
  248. listTiles.removeWhere(
  249. (element) => element == null,
  250. );
  251. return SafeArea(
  252. top: false,
  253. child: Scrollbar(
  254. thickness: 4,
  255. radius: const Radius.circular(2),
  256. thumbVisibility: true,
  257. child: Padding(
  258. padding: const EdgeInsets.all(8.0),
  259. child: CustomScrollView(
  260. physics: const ClampingScrollPhysics(),
  261. shrinkWrap: true,
  262. slivers: <Widget>[
  263. TitleBarWidget(
  264. isFlexibleSpaceDisabled: true,
  265. title: "Details",
  266. isOnTopOfScreen: false,
  267. backgroundColor: getEnteColorScheme(context).backgroundElevated,
  268. leading: IconButtonWidget(
  269. icon: Icons.close_outlined,
  270. iconButtonType: IconButtonType.primary,
  271. onTap: () => Navigator.pop(context),
  272. ),
  273. ),
  274. SliverList(
  275. delegate: SliverChildBuilderDelegate(
  276. (context, index) {
  277. if (index.isOdd) {
  278. return index == 1
  279. ? const SizedBox.shrink()
  280. : const DividerWidget(
  281. dividerType: DividerType.menu,
  282. );
  283. } else {
  284. return listTiles[index ~/ 2];
  285. }
  286. },
  287. childCount: (listTiles.length * 2) - 1,
  288. ),
  289. )
  290. ],
  291. ),
  292. ),
  293. ),
  294. );
  295. }
  296. _generateExifForDetails(Map<String, IfdTag> exif) {
  297. if (exif["EXIF FocalLength"] != null) {
  298. _exifData["focalLength"] =
  299. (exif["EXIF FocalLength"].values.toList()[0] as Ratio).numerator /
  300. (exif["EXIF FocalLength"].values.toList()[0] as Ratio)
  301. .denominator;
  302. }
  303. if (exif["EXIF FNumber"] != null) {
  304. _exifData["fNumber"] =
  305. (exif["EXIF FNumber"].values.toList()[0] as Ratio).numerator /
  306. (exif["EXIF FNumber"].values.toList()[0] as Ratio).denominator;
  307. }
  308. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  309. final imageLength = exif["EXIF ExifImageLength"] ??
  310. exif["Image "
  311. "ImageLength"];
  312. if (imageWidth != null && imageLength != null) {
  313. _exifData["resolution"] = '$imageWidth x $imageLength';
  314. _exifData['megaPixels'] =
  315. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  316. 1000000)
  317. .toStringAsFixed(1);
  318. } else {
  319. debugPrint("No image width/height");
  320. }
  321. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  322. _exifData["takenOnDevice"] =
  323. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  324. }
  325. if (exif["EXIF ExposureTime"] != null) {
  326. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  327. }
  328. if (exif["EXIF ISOSpeedRatings"] != null) {
  329. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  330. }
  331. }
  332. Widget _getFileSize() {
  333. return FutureBuilder(
  334. future: getFile(widget.file).then((f) => f.length()),
  335. builder: (context, snapshot) {
  336. if (snapshot.hasData) {
  337. return Text(
  338. (snapshot.data / (1024 * 1024)).toStringAsFixed(2) + " MB",
  339. );
  340. } else {
  341. return Center(
  342. child: SizedBox.fromSize(
  343. size: const Size.square(24),
  344. child: const CupertinoActivityIndicator(
  345. radius: 8,
  346. ),
  347. ),
  348. );
  349. }
  350. },
  351. );
  352. }
  353. Widget _getVideoDuration() {
  354. if (widget.file.duration != 0) {
  355. return Text(
  356. secondsToHHMMSS(widget.file.duration),
  357. );
  358. }
  359. return FutureBuilder(
  360. future: widget.file.getAsset,
  361. builder: (context, snapshot) {
  362. if (snapshot.hasData) {
  363. return Text(
  364. snapshot.data.videoDuration.toString().split(".")[0],
  365. );
  366. } else {
  367. return Center(
  368. child: SizedBox.fromSize(
  369. size: const Size.square(24),
  370. child: const CupertinoActivityIndicator(
  371. radius: 8,
  372. ),
  373. ),
  374. );
  375. }
  376. },
  377. );
  378. }
  379. void _showDateTimePicker(File file) async {
  380. final dateResult = await DatePicker.showDatePicker(
  381. context,
  382. minTime: DateTime(1800, 1, 1),
  383. maxTime: DateTime.now(),
  384. currentTime: DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  385. locale: LocaleType.en,
  386. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  387. );
  388. if (dateResult == null) {
  389. return;
  390. }
  391. final dateWithTimeResult = await DatePicker.showTime12hPicker(
  392. context,
  393. showTitleActions: true,
  394. currentTime: dateResult,
  395. locale: LocaleType.en,
  396. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  397. );
  398. if (dateWithTimeResult != null) {
  399. if (await editTime(
  400. context,
  401. List.of([widget.file]),
  402. dateWithTimeResult.microsecondsSinceEpoch,
  403. )) {
  404. widget.file.creationTime = dateWithTimeResult.microsecondsSinceEpoch;
  405. setState(() {});
  406. }
  407. }
  408. }
  409. }