file_info_widget.dart 13 KB

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