file_info_widget.dart 13 KB

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