file_info_dialog.dart 13 KB

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