file_info_widget.dart 14 KB

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