file_info_dialog.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. var 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. 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. const DividerWithPadding(left: 70, right: 20),
  219. (file.uploadedFileID != null && file.updationTime != null)
  220. ? ListTile(
  221. leading: const Padding(
  222. padding: EdgeInsets.only(top: 8, left: 6),
  223. child: Icon(Icons.cloud_upload_outlined),
  224. ),
  225. title: Text(
  226. getFullDate(
  227. DateTime.fromMicrosecondsSinceEpoch(file.updationTime),
  228. ),
  229. ),
  230. subtitle: Text(
  231. getTimeIn12hrFormat(dateTimeForUpdationTime) +
  232. " " +
  233. dateTimeForUpdationTime.timeZoneName,
  234. style: Theme.of(context).textTheme.bodyText2.copyWith(
  235. color: Theme.of(context)
  236. .colorScheme
  237. .defaultTextColor
  238. .withOpacity(0.5),
  239. ),
  240. ),
  241. )
  242. : const SizedBox.shrink(),
  243. _isImage
  244. ? Padding(
  245. padding: const EdgeInsets.fromLTRB(0, 24, 0, 16),
  246. child: SafeArea(
  247. child: RawExifButton(_exif, widget.file),
  248. ),
  249. )
  250. : const SizedBox(
  251. height: 12,
  252. )
  253. ];
  254. return Column(
  255. mainAxisSize: MainAxisSize.min,
  256. children: [
  257. Padding(
  258. padding: const EdgeInsets.all(10),
  259. child: Row(
  260. crossAxisAlignment: CrossAxisAlignment.center,
  261. children: [
  262. IconButton(
  263. onPressed: () {
  264. Navigator.pop(context);
  265. },
  266. icon: const Icon(
  267. Icons.close,
  268. ),
  269. ),
  270. const SizedBox(width: 6),
  271. Padding(
  272. padding: const EdgeInsets.only(bottom: 2),
  273. child: Text(
  274. "Details",
  275. style: Theme.of(context).textTheme.bodyText1,
  276. ),
  277. ),
  278. ],
  279. ),
  280. ),
  281. ...listTiles
  282. ],
  283. );
  284. }
  285. _generateExifForDetails(Map<String, IfdTag> exif) {
  286. if (exif["EXIF FocalLength"] != null) {
  287. _exifData["focalLength"] =
  288. (exif["EXIF FocalLength"].values.toList()[0] as Ratio).numerator /
  289. (exif["EXIF FocalLength"].values.toList()[0] as Ratio)
  290. .denominator;
  291. }
  292. if (exif["EXIF FNumber"] != null) {
  293. _exifData["fNumber"] =
  294. (exif["EXIF FNumber"].values.toList()[0] as Ratio).numerator /
  295. (exif["EXIF FNumber"].values.toList()[0] as Ratio).denominator;
  296. }
  297. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  298. final imageLength = exif["EXIF ExifImageLength"] ??
  299. exif["Image "
  300. "ImageLength"];
  301. if (imageWidth != null && imageLength != null) {
  302. _exifData["resolution"] = '$imageWidth x $imageLength';
  303. _exifData['megaPixels'] =
  304. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  305. 1000000)
  306. .toStringAsFixed(1);
  307. } else {
  308. debugPrint("No image width/height");
  309. }
  310. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  311. _exifData["takenOnDevice"] =
  312. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  313. }
  314. if (exif["EXIF ExposureTime"] != null) {
  315. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  316. }
  317. if (exif["EXIF ISOSpeedRatings"] != null) {
  318. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  319. }
  320. }
  321. Widget _getFileSize() {
  322. return FutureBuilder(
  323. future: getFile(widget.file).then((f) => f.length()),
  324. builder: (context, snapshot) {
  325. if (snapshot.hasData) {
  326. return Text(
  327. (snapshot.data / (1024 * 1024)).toStringAsFixed(2) + " MB",
  328. );
  329. } else {
  330. return Center(
  331. child: SizedBox.fromSize(
  332. size: const Size.square(24),
  333. child: const CupertinoActivityIndicator(
  334. radius: 8,
  335. ),
  336. ),
  337. );
  338. }
  339. },
  340. );
  341. }
  342. Widget _getVideoDuration() {
  343. if (widget.file.duration != 0) {
  344. return Text(
  345. secondsToHHMMSS(widget.file.duration),
  346. );
  347. }
  348. return FutureBuilder(
  349. future: widget.file.getAsset(),
  350. builder: (context, snapshot) {
  351. if (snapshot.hasData) {
  352. return Text(
  353. snapshot.data.videoDuration.toString().split(".")[0],
  354. );
  355. } else {
  356. return Center(
  357. child: SizedBox.fromSize(
  358. size: const Size.square(24),
  359. child: const CupertinoActivityIndicator(
  360. radius: 8,
  361. ),
  362. ),
  363. );
  364. }
  365. },
  366. );
  367. }
  368. void _showDateTimePicker(File file) async {
  369. final dateResult = await DatePicker.showDatePicker(
  370. context,
  371. minTime: DateTime(1800, 1, 1),
  372. maxTime: DateTime.now(),
  373. currentTime: DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  374. locale: LocaleType.en,
  375. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  376. );
  377. if (dateResult == null) {
  378. return;
  379. }
  380. final dateWithTimeResult = await DatePicker.showTime12hPicker(
  381. context,
  382. showTitleActions: true,
  383. currentTime: dateResult,
  384. locale: LocaleType.en,
  385. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  386. );
  387. if (dateWithTimeResult != null) {
  388. if (await editTime(
  389. context,
  390. List.of([widget.file]),
  391. dateWithTimeResult.microsecondsSinceEpoch,
  392. )) {
  393. widget.file.creationTime = dateWithTimeResult.microsecondsSinceEpoch;
  394. setState(() {});
  395. }
  396. }
  397. }
  398. }