file_info_widget.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. physics: const ClampingScrollPhysics(),
  260. shrinkWrap: true,
  261. slivers: <Widget>[
  262. TitleBarWidget(
  263. isFlexibleSpaceDisabled: true,
  264. title: "Details",
  265. isOnTopOfScreen: false,
  266. leading: IconButtonWidget(
  267. icon: Icons.close_outlined,
  268. iconButtonType: IconButtonType.primary,
  269. onTap: () => Navigator.pop(context),
  270. ),
  271. ),
  272. SliverList(
  273. delegate: SliverChildBuilderDelegate(
  274. (context, index) {
  275. if (index.isOdd) {
  276. return index == 1
  277. ? const SizedBox.shrink()
  278. : const DividerWidget(
  279. dividerType: DividerType.menu,
  280. );
  281. } else {
  282. return listTiles[index ~/ 2];
  283. }
  284. },
  285. childCount: (listTiles.length * 2) - 1,
  286. ),
  287. )
  288. ],
  289. ),
  290. ),
  291. ),
  292. );
  293. }
  294. _generateExifForDetails(Map<String, IfdTag> exif) {
  295. if (exif["EXIF FocalLength"] != null) {
  296. _exifData["focalLength"] =
  297. (exif["EXIF FocalLength"].values.toList()[0] as Ratio).numerator /
  298. (exif["EXIF FocalLength"].values.toList()[0] as Ratio)
  299. .denominator;
  300. }
  301. if (exif["EXIF FNumber"] != null) {
  302. _exifData["fNumber"] =
  303. (exif["EXIF FNumber"].values.toList()[0] as Ratio).numerator /
  304. (exif["EXIF FNumber"].values.toList()[0] as Ratio).denominator;
  305. }
  306. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  307. final imageLength = exif["EXIF ExifImageLength"] ??
  308. exif["Image "
  309. "ImageLength"];
  310. if (imageWidth != null && imageLength != null) {
  311. _exifData["resolution"] = '$imageWidth x $imageLength';
  312. _exifData['megaPixels'] =
  313. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  314. 1000000)
  315. .toStringAsFixed(1);
  316. } else {
  317. debugPrint("No image width/height");
  318. }
  319. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  320. _exifData["takenOnDevice"] =
  321. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  322. }
  323. if (exif["EXIF ExposureTime"] != null) {
  324. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  325. }
  326. if (exif["EXIF ISOSpeedRatings"] != null) {
  327. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  328. }
  329. }
  330. Widget _getFileSize() {
  331. return FutureBuilder(
  332. future: getFile(widget.file).then((f) => f.length()),
  333. builder: (context, snapshot) {
  334. if (snapshot.hasData) {
  335. return Text(
  336. (snapshot.data / (1024 * 1024)).toStringAsFixed(2) + " MB",
  337. );
  338. } else {
  339. return Center(
  340. child: SizedBox.fromSize(
  341. size: const Size.square(24),
  342. child: const CupertinoActivityIndicator(
  343. radius: 8,
  344. ),
  345. ),
  346. );
  347. }
  348. },
  349. );
  350. }
  351. Widget _getVideoDuration() {
  352. if (widget.file.duration != 0) {
  353. return Text(
  354. secondsToHHMMSS(widget.file.duration),
  355. );
  356. }
  357. return FutureBuilder(
  358. future: widget.file.getAsset,
  359. builder: (context, snapshot) {
  360. if (snapshot.hasData) {
  361. return Text(
  362. snapshot.data.videoDuration.toString().split(".")[0],
  363. );
  364. } else {
  365. return Center(
  366. child: SizedBox.fromSize(
  367. size: const Size.square(24),
  368. child: const CupertinoActivityIndicator(
  369. radius: 8,
  370. ),
  371. ),
  372. );
  373. }
  374. },
  375. );
  376. }
  377. void _showDateTimePicker(File file) async {
  378. final dateResult = await DatePicker.showDatePicker(
  379. context,
  380. minTime: DateTime(1800, 1, 1),
  381. maxTime: DateTime.now(),
  382. currentTime: DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  383. locale: LocaleType.en,
  384. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  385. );
  386. if (dateResult == null) {
  387. return;
  388. }
  389. final dateWithTimeResult = await DatePicker.showTime12hPicker(
  390. context,
  391. showTitleActions: true,
  392. currentTime: dateResult,
  393. locale: LocaleType.en,
  394. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  395. );
  396. if (dateWithTimeResult != null) {
  397. if (await editTime(
  398. context,
  399. List.of([widget.file]),
  400. dateWithTimeResult.microsecondsSinceEpoch,
  401. )) {
  402. widget.file.creationTime = dateWithTimeResult.microsecondsSinceEpoch;
  403. setState(() {});
  404. }
  405. }
  406. }
  407. }