file_info_widget.dart 13 KB

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