file_info_dialog.dart 12 KB

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