file_info_dialog.dart 13 KB

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