file_info_widget.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. leading: const Padding(
  93. padding: EdgeInsets.only(top: 8, left: 6),
  94. child: Icon(Icons.calendar_today_rounded),
  95. ),
  96. title: Text(
  97. getFullDate(
  98. DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  99. ),
  100. ),
  101. subtitle: Text(
  102. getTimeIn12hrFormat(dateTime) + " " + dateTime.timeZoneName,
  103. style: Theme.of(context).textTheme.bodyText2.copyWith(
  104. color: Theme.of(context)
  105. .colorScheme
  106. .defaultTextColor
  107. .withOpacity(0.5),
  108. ),
  109. ),
  110. trailing: (widget.file.ownerID == null ||
  111. widget.file.ownerID ==
  112. Configuration.instance.getUserID()) &&
  113. widget.file.uploadedFileID != null
  114. ? IconButton(
  115. onPressed: () {
  116. _showDateTimePicker(widget.file);
  117. },
  118. icon: const Icon(Icons.edit),
  119. )
  120. : const SizedBox.shrink(),
  121. ),
  122. ListTile(
  123. leading: _isImage
  124. ? const Padding(
  125. padding: EdgeInsets.only(top: 8, left: 6),
  126. child: Icon(
  127. Icons.image,
  128. ),
  129. )
  130. : const Padding(
  131. padding: EdgeInsets.only(top: 8, left: 6),
  132. child: Icon(
  133. Icons.video_camera_back,
  134. size: 27,
  135. ),
  136. ),
  137. title: Text(
  138. file.displayName,
  139. ),
  140. subtitle: Row(
  141. children: [
  142. showDimension
  143. ? Text(
  144. "${_exifData["megaPixels"]}MP "
  145. "${_exifData["resolution"]} ",
  146. )
  147. : const SizedBox.shrink(),
  148. _getFileSize(),
  149. (file.fileType == FileType.video) &&
  150. (file.localID != null || file.duration != 0)
  151. ? Padding(
  152. padding: const EdgeInsets.only(left: 8.0),
  153. child: _getVideoDuration(),
  154. )
  155. : const SizedBox.shrink(),
  156. ],
  157. ),
  158. trailing: file.uploadedFileID == null ||
  159. file.ownerID != Configuration.instance.getUserID()
  160. ? const SizedBox.shrink()
  161. : IconButton(
  162. onPressed: () async {
  163. await editFilename(context, file);
  164. setState(() {});
  165. },
  166. icon: const Icon(Icons.edit),
  167. ),
  168. ),
  169. showExifListTile
  170. ? ListTile(
  171. leading: const Padding(
  172. padding: EdgeInsets.only(left: 6),
  173. child: Icon(Icons.camera_rounded),
  174. ),
  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. leading: const Padding(
  211. padding: EdgeInsets.only(left: 6),
  212. child: Icon(Icons.folder_outlined),
  213. ),
  214. title: fileIsBackedup
  215. ? CollectionsListOfFileWidget(allCollectionIDsOfFile)
  216. : DeviceFoldersListOfFileWidget(allDeviceFoldersOfFile),
  217. ),
  218. ),
  219. (file.uploadedFileID != null && file.updationTime != null)
  220. ? ListTile(
  221. leading: const Padding(
  222. padding: EdgeInsets.only(top: 8, left: 6),
  223. child: Icon(Icons.cloud_upload_outlined),
  224. ),
  225. title: Text(
  226. getFullDate(
  227. DateTime.fromMicrosecondsSinceEpoch(file.updationTime),
  228. ),
  229. ),
  230. subtitle: Text(
  231. getTimeIn12hrFormat(dateTimeForUpdationTime) +
  232. " " +
  233. dateTimeForUpdationTime.timeZoneName,
  234. style: Theme.of(context).textTheme.bodyText2.copyWith(
  235. color: Theme.of(context)
  236. .colorScheme
  237. .defaultTextColor
  238. .withOpacity(0.5),
  239. ),
  240. ),
  241. )
  242. : null,
  243. _isImage ? RawExifListTileWidget(_exif, widget.file) : null,
  244. ];
  245. listTiles.removeWhere(
  246. (element) => element == null,
  247. );
  248. return SafeArea(
  249. top: false,
  250. child: Padding(
  251. padding: const EdgeInsets.all(8.0),
  252. child: CustomScrollView(
  253. shrinkWrap: true,
  254. slivers: <Widget>[
  255. TitleBarWidget(
  256. isFlexibleSpaceDisabled: true,
  257. title: "Details",
  258. isOnTopOfScreen: false,
  259. leading: IconButtonWidget(
  260. icon: Icons.close_outlined,
  261. iconButtonType: IconButtonType.primary,
  262. onTap: () => Navigator.pop(context),
  263. ),
  264. ),
  265. SliverList(
  266. delegate: SliverChildBuilderDelegate(
  267. (context, index) {
  268. if (index.isOdd) {
  269. return const DividerWidget(dividerType: DividerType.menu);
  270. } else {
  271. return listTiles[index ~/ 2];
  272. }
  273. },
  274. childCount: (listTiles.length * 2) - 1,
  275. ),
  276. )
  277. ],
  278. ),
  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. }