file_info_widget.dart 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. import "dart:async";
  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:path/path.dart' as path;
  7. import 'package:photo_manager/photo_manager.dart';
  8. import "package:photos/core/configuration.dart";
  9. import 'package:photos/db/files_db.dart';
  10. import "package:photos/ente_theme_data.dart";
  11. import "package:photos/models/file.dart";
  12. import "package:photos/models/file_type.dart";
  13. import 'package:photos/services/collections_service.dart';
  14. import "package:photos/services/feature_flag_service.dart";
  15. import "package:photos/services/location_service.dart";
  16. import 'package:photos/theme/ente_theme.dart';
  17. import 'package:photos/ui/components/divider_widget.dart';
  18. import 'package:photos/ui/components/icon_button_widget.dart';
  19. import 'package:photos/ui/components/title_bar_widget.dart';
  20. import 'package:photos/ui/viewer/file/collections_list_of_file_widget.dart';
  21. import 'package:photos/ui/viewer/file/device_folders_list_of_file_widget.dart';
  22. import 'package:photos/ui/viewer/file/file_caption_widget.dart';
  23. import "package:photos/ui/viewer/file/location_chip.dart";
  24. import "package:photos/ui/viewer/file/locations_list.dart";
  25. import "package:photos/ui/viewer/file/object_tags_widget.dart";
  26. import 'package:photos/ui/viewer/file/raw_exif_list_tile_widget.dart';
  27. import "package:photos/utils/date_time_util.dart";
  28. import "package:photos/utils/exif_util.dart";
  29. import "package:photos/utils/file_util.dart";
  30. import "package:photos/utils/magic_util.dart";
  31. class FileInfoWidget extends StatefulWidget {
  32. final File file;
  33. const FileInfoWidget(
  34. this.file, {
  35. Key? key,
  36. }) : super(key: key);
  37. @override
  38. State<FileInfoWidget> createState() => _FileInfoWidgetState();
  39. }
  40. class _FileInfoWidgetState extends State<FileInfoWidget> {
  41. Map<String, IfdTag>? _exif;
  42. late LocationService locationService = LocationService.instance;
  43. final Map<String, dynamic> _exifData = {
  44. "focalLength": null,
  45. "fNumber": null,
  46. "resolution": null,
  47. "takenOnDevice": null,
  48. "exposureTime": null,
  49. "ISO": null,
  50. "megaPixels": null
  51. };
  52. bool _isImage = false;
  53. int? _currentUserID;
  54. @override
  55. void initState() {
  56. debugPrint('file_info_dialog initState');
  57. _currentUserID = Configuration.instance.getUserID();
  58. _isImage = widget.file.fileType == FileType.image ||
  59. widget.file.fileType == FileType.livePhoto;
  60. if (_isImage) {
  61. getExif(widget.file).then((exif) {
  62. if (mounted) {
  63. setState(() {
  64. _exif = exif;
  65. });
  66. }
  67. });
  68. }
  69. super.initState();
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. final file = widget.file;
  74. final fileIsBackedup = file.uploadedFileID == null ? false : true;
  75. final bool isFileOwner =
  76. file.ownerID == null || file.ownerID == _currentUserID;
  77. late Future<Set<int>> allCollectionIDsOfFile;
  78. //Typing this as Future<Set<T>> as it would be easier to implement showing multiple device folders for a file in the future
  79. final Future<Set<String>> allDeviceFoldersOfFile =
  80. Future.sync(() => {file.deviceFolder ?? ''});
  81. if (fileIsBackedup) {
  82. allCollectionIDsOfFile = FilesDB.instance.getAllCollectionIDsOfFile(
  83. file.uploadedFileID!,
  84. );
  85. }
  86. final dateTime = DateTime.fromMicrosecondsSinceEpoch(file.creationTime!);
  87. final dateTimeForUpdationTime =
  88. DateTime.fromMicrosecondsSinceEpoch(file.updationTime!);
  89. if (_isImage && _exif != null) {
  90. _generateExifForDetails(_exif!);
  91. }
  92. final bool showExifListTile = _exifData["focalLength"] != null ||
  93. _exifData["fNumber"] != null ||
  94. _exifData["takenOnDevice"] != null ||
  95. _exifData["exposureTime"] != null ||
  96. _exifData["ISO"] != null;
  97. final bool showDimension =
  98. _exifData["resolution"] != null && _exifData["megaPixels"] != null;
  99. final listTiles = <Widget?>[
  100. !widget.file.isUploaded ||
  101. (!isFileOwner && (widget.file.caption?.isEmpty ?? true))
  102. ? const SizedBox.shrink()
  103. : Padding(
  104. padding: const EdgeInsets.only(top: 8, bottom: 4),
  105. child: isFileOwner
  106. ? FileCaptionWidget(file: widget.file)
  107. : FileCaptionReadyOnly(caption: widget.file.caption!),
  108. ),
  109. ListTile(
  110. horizontalTitleGap: 2,
  111. leading: const Padding(
  112. padding: EdgeInsets.only(top: 8),
  113. child: Icon(Icons.calendar_today_rounded),
  114. ),
  115. title: Text(
  116. getFullDate(
  117. DateTime.fromMicrosecondsSinceEpoch(file.creationTime!),
  118. ),
  119. ),
  120. subtitle: Text(
  121. getTimeIn12hrFormat(dateTime) + " " + dateTime.timeZoneName,
  122. style: Theme.of(context).textTheme.bodyText2!.copyWith(
  123. color: Theme.of(context)
  124. .colorScheme
  125. .defaultTextColor
  126. .withOpacity(0.5),
  127. ),
  128. ),
  129. trailing: (widget.file.ownerID == null ||
  130. widget.file.ownerID == _currentUserID) &&
  131. widget.file.uploadedFileID != null
  132. ? IconButton(
  133. onPressed: () {
  134. _showDateTimePicker(widget.file);
  135. },
  136. icon: const Icon(Icons.edit),
  137. )
  138. : const SizedBox.shrink(),
  139. ),
  140. ListTile(
  141. horizontalTitleGap: 2,
  142. leading: _isImage
  143. ? const Padding(
  144. padding: EdgeInsets.only(top: 8),
  145. child: Icon(
  146. Icons.image,
  147. ),
  148. )
  149. : const Padding(
  150. padding: EdgeInsets.only(top: 8),
  151. child: Icon(
  152. Icons.video_camera_back,
  153. size: 27,
  154. ),
  155. ),
  156. title: Text(
  157. path.basenameWithoutExtension(file.displayName) +
  158. path.extension(file.displayName).toUpperCase(),
  159. ),
  160. subtitle: Wrap(
  161. children: [
  162. showDimension
  163. ? Text(
  164. "${_exifData["megaPixels"]}MP "
  165. "${_exifData["resolution"]} ",
  166. )
  167. : const SizedBox.shrink(),
  168. _getFileSize(),
  169. (file.fileType == FileType.video) &&
  170. (file.localID != null || file.duration != 0)
  171. ? Padding(
  172. padding: const EdgeInsets.only(left: 8.0),
  173. child: _getVideoDuration(),
  174. )
  175. : const SizedBox.shrink(),
  176. ],
  177. ),
  178. trailing: file.uploadedFileID == null || file.ownerID != _currentUserID
  179. ? const SizedBox.shrink()
  180. : IconButton(
  181. onPressed: () async {
  182. await editFilename(context, file);
  183. setState(() {});
  184. },
  185. icon: const Icon(Icons.edit),
  186. ),
  187. ),
  188. showExifListTile
  189. ? ListTile(
  190. horizontalTitleGap: 2,
  191. leading: const Icon(Icons.camera_rounded),
  192. title: Text(_exifData["takenOnDevice"] ?? "--"),
  193. subtitle: Wrap(
  194. children: [
  195. _exifData["fNumber"] != null
  196. ? Padding(
  197. padding: const EdgeInsets.only(right: 10),
  198. child: Text('ƒ/' + _exifData["fNumber"].toString()),
  199. )
  200. : const SizedBox.shrink(),
  201. _exifData["exposureTime"] != null
  202. ? Padding(
  203. padding: const EdgeInsets.only(right: 10),
  204. child: Text(_exifData["exposureTime"]),
  205. )
  206. : const SizedBox.shrink(),
  207. _exifData["focalLength"] != null
  208. ? Padding(
  209. padding: const EdgeInsets.only(right: 10),
  210. child:
  211. Text(_exifData["focalLength"].toString() + "mm"),
  212. )
  213. : const SizedBox.shrink(),
  214. _exifData["ISO"] != null
  215. ? Padding(
  216. padding: const EdgeInsets.only(right: 10),
  217. child: Text("ISO" + _exifData["ISO"].toString()),
  218. )
  219. : const SizedBox.shrink(),
  220. ],
  221. ),
  222. )
  223. : null,
  224. SizedBox(
  225. height: 62,
  226. child: ListTile(
  227. horizontalTitleGap: 0,
  228. leading: const Icon(Icons.folder_outlined),
  229. title: fileIsBackedup
  230. ? CollectionsListOfFileWidget(
  231. allCollectionIDsOfFile,
  232. _currentUserID!,
  233. )
  234. : DeviceFoldersListOfFileWidget(allDeviceFoldersOfFile),
  235. ),
  236. ),
  237. FeatureFlagService.instance.isInternalUserOrDebugBuild()
  238. ? SizedBox(
  239. height: 62,
  240. child: ListTile(
  241. horizontalTitleGap: 0,
  242. leading: const Icon(Icons.image_search),
  243. title: ObjectTagsWidget(file),
  244. ),
  245. )
  246. : null,
  247. (file.uploadedFileID != null && file.updationTime != null)
  248. ? ListTile(
  249. horizontalTitleGap: 2,
  250. leading: const Padding(
  251. padding: EdgeInsets.only(top: 8),
  252. child: Icon(Icons.cloud_upload_outlined),
  253. ),
  254. title: Text(
  255. getFullDate(
  256. DateTime.fromMicrosecondsSinceEpoch(file.updationTime!),
  257. ),
  258. ),
  259. subtitle: Text(
  260. getTimeIn12hrFormat(dateTimeForUpdationTime) +
  261. " " +
  262. dateTimeForUpdationTime.timeZoneName,
  263. style: Theme.of(context).textTheme.bodyText2!.copyWith(
  264. color: Theme.of(context)
  265. .colorScheme
  266. .defaultTextColor
  267. .withOpacity(0.5),
  268. ),
  269. ),
  270. )
  271. : null,
  272. ListTile(
  273. horizontalTitleGap: 2,
  274. leading: Padding(
  275. padding: const EdgeInsets.only(top: 8),
  276. child: locationService.getLocationsByFileID(file.generatedID!).isEmpty
  277. ? const Icon(Icons.add_location_alt_rounded)
  278. : const Icon(Icons.location_on_rounded),
  279. ),
  280. title: Text(
  281. locationService.getLocationsByFileID(file.generatedID!).isEmpty
  282. ? "Add Location"
  283. : "Locations",
  284. ),
  285. subtitle:
  286. locationService.getLocationsByFileID(file.generatedID!).isEmpty
  287. ? Text(
  288. "group nearby photos",
  289. style: Theme.of(context).textTheme.bodyMedium!.copyWith(
  290. color: Theme.of(context)
  291. .colorScheme
  292. .defaultTextColor
  293. .withOpacity(0.5),
  294. ),
  295. )
  296. : locationChipList(
  297. file.generatedID!,
  298. context,
  299. ),
  300. trailing:
  301. locationService.getLocationsByFileID(file.generatedID!).isEmpty
  302. ? IconButton(
  303. onPressed: () async {
  304. unawaited(
  305. Navigator.of(context).push(
  306. MaterialPageRoute(
  307. builder: (BuildContext context) {
  308. return LocationsList(
  309. state: 1,
  310. fileId: file.generatedID,
  311. );
  312. },
  313. ),
  314. ),
  315. );
  316. },
  317. icon: const Icon(Icons.arrow_forward_ios),
  318. )
  319. : const SizedBox.shrink(),
  320. ),
  321. _isImage ? RawExifListTileWidget(_exif, widget.file) : null,
  322. ];
  323. listTiles.removeWhere(
  324. (element) => element == null,
  325. );
  326. return SafeArea(
  327. top: false,
  328. child: Scrollbar(
  329. thickness: 4,
  330. radius: const Radius.circular(2),
  331. thumbVisibility: true,
  332. child: Padding(
  333. padding: const EdgeInsets.all(8.0),
  334. child: CustomScrollView(
  335. physics: const ClampingScrollPhysics(),
  336. shrinkWrap: true,
  337. slivers: <Widget>[
  338. TitleBarWidget(
  339. isFlexibleSpaceDisabled: true,
  340. title: "Details",
  341. isOnTopOfScreen: false,
  342. backgroundColor: getEnteColorScheme(context).backgroundElevated,
  343. leading: IconButtonWidget(
  344. icon: Icons.close_outlined,
  345. iconButtonType: IconButtonType.primary,
  346. onTap: () => Navigator.pop(context),
  347. ),
  348. ),
  349. SliverToBoxAdapter(child: addedBy(widget.file)),
  350. SliverList(
  351. delegate: SliverChildBuilderDelegate(
  352. (context, index) {
  353. if (index.isOdd) {
  354. return index == 1
  355. ? const SizedBox.shrink()
  356. : const DividerWidget(
  357. dividerType: DividerType.menu,
  358. );
  359. } else {
  360. return listTiles[index ~/ 2];
  361. }
  362. },
  363. childCount: (listTiles.length * 2) - 1,
  364. ),
  365. )
  366. ],
  367. ),
  368. ),
  369. ),
  370. );
  371. }
  372. Widget addedBy(File file) {
  373. if (file.uploadedFileID == null) {
  374. return const SizedBox.shrink();
  375. }
  376. String? addedBy;
  377. if (file.ownerID == _currentUserID) {
  378. if (file.pubMagicMetadata!.uploaderName != null) {
  379. addedBy = file.pubMagicMetadata!.uploaderName;
  380. }
  381. } else {
  382. final fileOwner = CollectionsService.instance
  383. .getFileOwner(file.ownerID!, file.collectionID);
  384. addedBy = fileOwner.email;
  385. }
  386. if (addedBy == null || addedBy.isEmpty) {
  387. return const SizedBox.shrink();
  388. }
  389. final enteTheme = Theme.of(context).colorScheme.enteTheme;
  390. return Padding(
  391. padding: const EdgeInsets.only(top: 4.0, bottom: 4.0, left: 16),
  392. child: Text(
  393. "Added by $addedBy",
  394. style: enteTheme.textTheme.mini
  395. .copyWith(color: enteTheme.colorScheme.textMuted),
  396. ),
  397. );
  398. }
  399. _generateExifForDetails(Map<String, IfdTag> exif) {
  400. if (exif["EXIF FocalLength"] != null) {
  401. _exifData["focalLength"] =
  402. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio).numerator /
  403. (exif["EXIF FocalLength"]!.values.toList()[0] as Ratio)
  404. .denominator;
  405. }
  406. if (exif["EXIF FNumber"] != null) {
  407. _exifData["fNumber"] =
  408. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).numerator /
  409. (exif["EXIF FNumber"]!.values.toList()[0] as Ratio).denominator;
  410. }
  411. final imageWidth = exif["EXIF ExifImageWidth"] ?? exif["Image ImageWidth"];
  412. final imageLength = exif["EXIF ExifImageLength"] ??
  413. exif["Image "
  414. "ImageLength"];
  415. if (imageWidth != null && imageLength != null) {
  416. _exifData["resolution"] = '$imageWidth x $imageLength';
  417. _exifData['megaPixels'] =
  418. ((imageWidth.values.firstAsInt() * imageLength.values.firstAsInt()) /
  419. 1000000)
  420. .toStringAsFixed(1);
  421. } else {
  422. debugPrint("No image width/height");
  423. }
  424. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  425. _exifData["takenOnDevice"] =
  426. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  427. }
  428. if (exif["EXIF ExposureTime"] != null) {
  429. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  430. }
  431. if (exif["EXIF ISOSpeedRatings"] != null) {
  432. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  433. }
  434. }
  435. Widget _getFileSize() {
  436. Future<int> fileSizeFuture;
  437. if (widget.file.fileSize != null) {
  438. fileSizeFuture = Future.value(widget.file.fileSize);
  439. } else {
  440. fileSizeFuture = getFile(widget.file).then((f) => f!.length());
  441. }
  442. return FutureBuilder<int>(
  443. future: fileSizeFuture,
  444. builder: (context, snapshot) {
  445. if (snapshot.hasData) {
  446. return Text(
  447. (snapshot.data! / (1024 * 1024)).toStringAsFixed(2) + " MB",
  448. );
  449. } else {
  450. return Center(
  451. child: SizedBox.fromSize(
  452. size: const Size.square(24),
  453. child: const CupertinoActivityIndicator(
  454. radius: 8,
  455. ),
  456. ),
  457. );
  458. }
  459. },
  460. );
  461. }
  462. Widget _getVideoDuration() {
  463. if (widget.file.duration != 0) {
  464. return Text(
  465. secondsToHHMMSS(widget.file.duration!),
  466. );
  467. }
  468. return FutureBuilder<AssetEntity?>(
  469. future: widget.file.getAsset,
  470. builder: (context, snapshot) {
  471. if (snapshot.hasData) {
  472. return Text(
  473. snapshot.data!.videoDuration.toString().split(".")[0],
  474. );
  475. } else {
  476. return Center(
  477. child: SizedBox.fromSize(
  478. size: const Size.square(24),
  479. child: const CupertinoActivityIndicator(
  480. radius: 8,
  481. ),
  482. ),
  483. );
  484. }
  485. },
  486. );
  487. }
  488. void _showDateTimePicker(File file) async {
  489. final dateResult = await DatePicker.showDatePicker(
  490. context,
  491. minTime: DateTime(1800, 1, 1),
  492. maxTime: DateTime.now(),
  493. currentTime: DateTime.fromMicrosecondsSinceEpoch(file.creationTime!),
  494. locale: LocaleType.en,
  495. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  496. );
  497. if (dateResult == null) {
  498. return;
  499. }
  500. final dateWithTimeResult = await DatePicker.showTime12hPicker(
  501. context,
  502. showTitleActions: true,
  503. currentTime: dateResult,
  504. locale: LocaleType.en,
  505. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  506. );
  507. if (dateWithTimeResult != null) {
  508. if (await editTime(
  509. context,
  510. List.of([widget.file]),
  511. dateWithTimeResult.microsecondsSinceEpoch,
  512. )) {
  513. widget.file.creationTime = dateWithTimeResult.microsecondsSinceEpoch;
  514. setState(() {});
  515. }
  516. }
  517. }
  518. }