file_info_widget.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_button.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. ? Padding(
  240. padding: const EdgeInsets.fromLTRB(0, 24, 0, 16),
  241. child: SafeArea(
  242. child: RawExifButton(_exif, widget.file),
  243. ),
  244. )
  245. : const SizedBox(
  246. height: 12,
  247. )
  248. ];
  249. return 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 const DividerWidget(dividerType: DividerType.menu);
  269. } else {
  270. return listTiles[index ~/ 2];
  271. }
  272. },
  273. childCount: (listTiles.length * 2) - 1,
  274. ),
  275. )
  276. ],
  277. ),
  278. );
  279. }
  280. _generateExifForDetails(Map<String, IfdTag> exif) {
  281. if (exif["EXIF FocalLength"] != null) {
  282. _exifData["focalLength"] =
  283. (exif["EXIF FocalLength"].values.toList()[0] as Ratio).numerator /
  284. (exif["EXIF FocalLength"].values.toList()[0] as Ratio)
  285. .denominator;
  286. }
  287. if (exif["EXIF FNumber"] != null) {
  288. _exifData["fNumber"] =
  289. (exif["EXIF FNumber"].values.toList()[0] as Ratio).numerator /
  290. (exif["EXIF FNumber"].values.toList()[0] as Ratio).denominator;
  291. }
  292. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  293. final imageLength = exif["EXIF ExifImageLength"] ??
  294. exif["Image "
  295. "ImageLength"];
  296. if (imageWidth != null && imageLength != null) {
  297. _exifData["resolution"] = '$imageWidth x $imageLength';
  298. _exifData['megaPixels'] =
  299. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  300. 1000000)
  301. .toStringAsFixed(1);
  302. } else {
  303. debugPrint("No image width/height");
  304. }
  305. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  306. _exifData["takenOnDevice"] =
  307. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  308. }
  309. if (exif["EXIF ExposureTime"] != null) {
  310. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  311. }
  312. if (exif["EXIF ISOSpeedRatings"] != null) {
  313. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  314. }
  315. }
  316. Widget _getFileSize() {
  317. return FutureBuilder(
  318. future: getFile(widget.file).then((f) => f.length()),
  319. builder: (context, snapshot) {
  320. if (snapshot.hasData) {
  321. return Text(
  322. (snapshot.data / (1024 * 1024)).toStringAsFixed(2) + " MB",
  323. );
  324. } else {
  325. return Center(
  326. child: SizedBox.fromSize(
  327. size: const Size.square(24),
  328. child: const CupertinoActivityIndicator(
  329. radius: 8,
  330. ),
  331. ),
  332. );
  333. }
  334. },
  335. );
  336. }
  337. Widget _getVideoDuration() {
  338. if (widget.file.duration != 0) {
  339. return Text(
  340. secondsToHHMMSS(widget.file.duration),
  341. );
  342. }
  343. return FutureBuilder(
  344. future: widget.file.getAsset,
  345. builder: (context, snapshot) {
  346. if (snapshot.hasData) {
  347. return Text(
  348. snapshot.data.videoDuration.toString().split(".")[0],
  349. );
  350. } else {
  351. return Center(
  352. child: SizedBox.fromSize(
  353. size: const Size.square(24),
  354. child: const CupertinoActivityIndicator(
  355. radius: 8,
  356. ),
  357. ),
  358. );
  359. }
  360. },
  361. );
  362. }
  363. void _showDateTimePicker(File file) async {
  364. final dateResult = await DatePicker.showDatePicker(
  365. context,
  366. minTime: DateTime(1800, 1, 1),
  367. maxTime: DateTime.now(),
  368. currentTime: DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  369. locale: LocaleType.en,
  370. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  371. );
  372. if (dateResult == null) {
  373. return;
  374. }
  375. final dateWithTimeResult = await DatePicker.showTime12hPicker(
  376. context,
  377. showTitleActions: true,
  378. currentTime: dateResult,
  379. locale: LocaleType.en,
  380. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  381. );
  382. if (dateWithTimeResult != null) {
  383. if (await editTime(
  384. context,
  385. List.of([widget.file]),
  386. dateWithTimeResult.microsecondsSinceEpoch,
  387. )) {
  388. widget.file.creationTime = dateWithTimeResult.microsecondsSinceEpoch;
  389. setState(() {});
  390. }
  391. }
  392. }
  393. }