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