file_app_bar.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import "package:flutter/foundation.dart";
  4. import 'package:flutter/material.dart';
  5. import 'package:logging/logging.dart';
  6. import 'package:media_extension/media_extension.dart';
  7. import 'package:path/path.dart' as file_path;
  8. import 'package:photo_manager/photo_manager.dart';
  9. import 'package:photos/core/event_bus.dart';
  10. import 'package:photos/db/files_db.dart';
  11. import 'package:photos/events/local_photos_updated_event.dart';
  12. import "package:photos/generated/l10n.dart";
  13. import "package:photos/l10n/l10n.dart";
  14. import "package:photos/models/file/extensions/file_props.dart";
  15. import 'package:photos/models/file/file.dart';
  16. import 'package:photos/models/file/file_type.dart';
  17. import 'package:photos/models/file/trash_file.dart';
  18. import 'package:photos/models/ignored_file.dart';
  19. import "package:photos/models/metadata/common_keys.dart";
  20. import 'package:photos/models/selected_files.dart';
  21. import 'package:photos/services/collections_service.dart';
  22. import 'package:photos/services/hidden_service.dart';
  23. import 'package:photos/services/ignored_files_service.dart';
  24. import 'package:photos/services/local_sync_service.dart';
  25. import 'package:photos/ui/collections/collection_action_sheet.dart';
  26. import 'package:photos/ui/viewer/file/custom_app_bar.dart';
  27. import "package:photos/ui/viewer/file_details/favorite_widget.dart";
  28. import "package:photos/ui/viewer/file_details/upload_icon_widget.dart";
  29. import 'package:photos/utils/dialog_util.dart';
  30. import 'package:photos/utils/file_util.dart';
  31. import "package:photos/utils/magic_util.dart";
  32. import 'package:photos/utils/toast_util.dart';
  33. class FileAppBar extends StatefulWidget {
  34. final EnteFile file;
  35. final Function(EnteFile) onFileRemoved;
  36. final double height;
  37. final bool shouldShowActions;
  38. final ValueNotifier<bool> enableFullScreenNotifier;
  39. const FileAppBar(
  40. this.file,
  41. this.onFileRemoved,
  42. this.height,
  43. this.shouldShowActions, {
  44. required this.enableFullScreenNotifier,
  45. Key? key,
  46. }) : super(key: key);
  47. @override
  48. FileAppBarState createState() => FileAppBarState();
  49. }
  50. class FileAppBarState extends State<FileAppBar> {
  51. final _logger = Logger("FadingAppBar");
  52. @override
  53. Widget build(BuildContext context) {
  54. return CustomAppBar(
  55. ValueListenableBuilder(
  56. valueListenable: widget.enableFullScreenNotifier,
  57. builder: (context, bool isFullScreen, _) {
  58. return IgnorePointer(
  59. ignoring: isFullScreen,
  60. child: AnimatedOpacity(
  61. opacity: isFullScreen ? 0 : 1,
  62. duration: const Duration(milliseconds: 150),
  63. child: Container(
  64. decoration: BoxDecoration(
  65. gradient: LinearGradient(
  66. begin: Alignment.topCenter,
  67. end: Alignment.bottomCenter,
  68. colors: [
  69. Colors.black.withOpacity(0.72),
  70. Colors.black.withOpacity(0.6),
  71. Colors.transparent,
  72. ],
  73. stops: const [0, 0.2, 1],
  74. ),
  75. ),
  76. child: _buildAppBar(),
  77. ),
  78. ),
  79. );
  80. },
  81. ),
  82. Size.fromHeight(Platform.isAndroid ? 80 : 96),
  83. );
  84. }
  85. AppBar _buildAppBar() {
  86. _logger.fine("building app bar ${widget.file.generatedID?.toString()}");
  87. final List<Widget> actions = [];
  88. final isTrashedFile = widget.file is TrashFile;
  89. final shouldShowActions = widget.shouldShowActions && !isTrashedFile;
  90. final bool isOwnedByUser = widget.file.isOwner;
  91. final bool isFileUploaded = widget.file.isUploaded;
  92. bool isFileHidden = false;
  93. if (isOwnedByUser && isFileUploaded) {
  94. isFileHidden = CollectionsService.instance
  95. .getCollectionByID(widget.file.collectionID!)
  96. ?.isHidden() ??
  97. false;
  98. }
  99. if (kDebugMode) {
  100. actions.add(
  101. Text(
  102. widget.file.generatedID?.toString() ?? 'null',
  103. style: const TextStyle(color: Colors.white),
  104. ),
  105. );
  106. }
  107. if (widget.file.isLiveOrMotionPhoto) {
  108. actions.add(
  109. IconButton(
  110. icon: const Icon(Icons.album_outlined),
  111. onPressed: () {
  112. showShortToast(context, S.of(context).pressAndHoldToPlayVideoDetailed);
  113. },
  114. ),
  115. );
  116. }
  117. // only show fav option for files owned by the user
  118. if (isOwnedByUser && !isFileHidden && isFileUploaded) {
  119. actions.add(
  120. Padding(
  121. padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 7.5),
  122. child: FavoriteWidget(widget.file),
  123. ),
  124. );
  125. }
  126. if (!isFileUploaded) {
  127. actions.add(
  128. UploadIconWidget(
  129. file: widget.file,
  130. key: ValueKey(widget.file.tag),
  131. ),
  132. );
  133. }
  134. actions.add(
  135. PopupMenuButton(
  136. itemBuilder: (context) {
  137. final List<PopupMenuItem> items = [];
  138. if (widget.file.isRemoteFile) {
  139. items.add(
  140. PopupMenuItem(
  141. value: 1,
  142. child: Row(
  143. children: [
  144. Icon(
  145. Platform.isAndroid
  146. ? Icons.download
  147. : CupertinoIcons.cloud_download,
  148. color: Theme.of(context).iconTheme.color,
  149. ),
  150. const Padding(
  151. padding: EdgeInsets.all(8),
  152. ),
  153. Text(S.of(context).download),
  154. ],
  155. ),
  156. ),
  157. );
  158. }
  159. // options for files owned by the user
  160. if (isOwnedByUser && !isFileHidden && isFileUploaded) {
  161. final bool isArchived =
  162. widget.file.magicMetadata.visibility == archiveVisibility;
  163. items.add(
  164. PopupMenuItem(
  165. value: 2,
  166. child: Row(
  167. children: [
  168. Icon(
  169. isArchived ? Icons.unarchive : Icons.archive_outlined,
  170. color: Theme.of(context).iconTheme.color,
  171. ),
  172. const Padding(
  173. padding: EdgeInsets.all(8),
  174. ),
  175. Text(
  176. isArchived
  177. ? S.of(context).unarchive
  178. : S.of(context).archive,
  179. ),
  180. ],
  181. ),
  182. ),
  183. );
  184. }
  185. if ((widget.file.fileType == FileType.image ||
  186. widget.file.fileType == FileType.livePhoto) &&
  187. Platform.isAndroid) {
  188. items.add(
  189. PopupMenuItem(
  190. value: 3,
  191. child: Row(
  192. children: [
  193. Icon(
  194. Icons.wallpaper_outlined,
  195. color: Theme.of(context).iconTheme.color,
  196. ),
  197. const Padding(
  198. padding: EdgeInsets.all(8),
  199. ),
  200. Text(S.of(context).setAs),
  201. ],
  202. ),
  203. ),
  204. );
  205. }
  206. if (isOwnedByUser && widget.file.isUploaded) {
  207. if (!isFileHidden) {
  208. items.add(
  209. PopupMenuItem(
  210. value: 4,
  211. child: Row(
  212. children: [
  213. Icon(
  214. Icons.visibility_off,
  215. color: Theme.of(context).iconTheme.color,
  216. ),
  217. const Padding(
  218. padding: EdgeInsets.all(8),
  219. ),
  220. Text(S.of(context).hide),
  221. ],
  222. ),
  223. ),
  224. );
  225. } else {
  226. items.add(
  227. PopupMenuItem(
  228. value: 5,
  229. child: Row(
  230. children: [
  231. Icon(
  232. Icons.visibility,
  233. color: Theme.of(context).iconTheme.color,
  234. ),
  235. const Padding(
  236. padding: EdgeInsets.all(8),
  237. ),
  238. Text(S.of(context).unhide),
  239. ],
  240. ),
  241. ),
  242. );
  243. }
  244. }
  245. return items;
  246. },
  247. onSelected: (dynamic value) async {
  248. if (value == 1) {
  249. _download(widget.file);
  250. } else if (value == 2) {
  251. await _toggleFileArchiveStatus(widget.file);
  252. } else if (value == 3) {
  253. _setAs(widget.file);
  254. } else if (value == 4) {
  255. _handleHideRequest(context);
  256. } else if (value == 5) {
  257. _handleUnHideRequest(context);
  258. }
  259. },
  260. ),
  261. );
  262. return AppBar(
  263. iconTheme:
  264. const IconThemeData(color: Colors.white), //same for both themes
  265. actions: shouldShowActions ? actions : [],
  266. elevation: 0,
  267. backgroundColor: const Color(0x00000000),
  268. );
  269. }
  270. Future<void> _handleHideRequest(BuildContext context) async {
  271. try {
  272. final hideResult =
  273. await CollectionsService.instance.hideFiles(context, [widget.file]);
  274. if (hideResult) {
  275. widget.onFileRemoved(widget.file);
  276. }
  277. } catch (e, s) {
  278. _logger.severe("failed to update file visibility", e, s);
  279. await showGenericErrorDialog(context: context);
  280. }
  281. }
  282. Future<void> _handleUnHideRequest(BuildContext context) async {
  283. final selectedFiles = SelectedFiles();
  284. selectedFiles.files.add(widget.file);
  285. showCollectionActionSheet(
  286. context,
  287. selectedFiles: selectedFiles,
  288. actionType: CollectionActionType.unHide,
  289. );
  290. }
  291. Future<void> _toggleFileArchiveStatus(EnteFile file) async {
  292. final bool isArchived =
  293. widget.file.magicMetadata.visibility == archiveVisibility;
  294. await changeVisibility(
  295. context,
  296. [widget.file],
  297. isArchived ? visibleVisibility : archiveVisibility,
  298. );
  299. if (mounted) {
  300. setState(() {});
  301. }
  302. }
  303. Future<void> _download(EnteFile file) async {
  304. final dialog = createProgressDialog(
  305. context,
  306. context.l10n.downloading,
  307. isDismissible: true,
  308. );
  309. await dialog.show();
  310. try {
  311. final FileType type = file.fileType;
  312. final bool downloadLivePhotoOnDroid =
  313. type == FileType.livePhoto && Platform.isAndroid;
  314. AssetEntity? savedAsset;
  315. final File? fileToSave = await getFile(file);
  316. //Disabling notifications for assets changing to insert the file into
  317. //files db before triggering a sync.
  318. PhotoManager.stopChangeNotify();
  319. if (type == FileType.image) {
  320. savedAsset = await PhotoManager.editor
  321. .saveImageWithPath(fileToSave!.path, title: file.title!);
  322. } else if (type == FileType.video) {
  323. savedAsset = await PhotoManager.editor
  324. .saveVideo(fileToSave!, title: file.title!);
  325. } else if (type == FileType.livePhoto) {
  326. final File? liveVideoFile =
  327. await getFileFromServer(file, liveVideo: true);
  328. if (liveVideoFile == null) {
  329. throw AssertionError("Live video can not be null");
  330. }
  331. if (downloadLivePhotoOnDroid) {
  332. await _saveLivePhotoOnDroid(fileToSave!, liveVideoFile, file);
  333. } else {
  334. savedAsset = await PhotoManager.editor.darwin.saveLivePhoto(
  335. imageFile: fileToSave!,
  336. videoFile: liveVideoFile,
  337. title: file.title!,
  338. );
  339. }
  340. }
  341. if (savedAsset != null) {
  342. file.localID = savedAsset.id;
  343. await FilesDB.instance.insert(file);
  344. Bus.instance.fire(
  345. LocalPhotosUpdatedEvent(
  346. [file],
  347. source: "download",
  348. ),
  349. );
  350. } else if (!downloadLivePhotoOnDroid && savedAsset == null) {
  351. _logger.severe('Failed to save assert of type $type');
  352. }
  353. showToast(context, S.of(context).fileSavedToGallery);
  354. await dialog.hide();
  355. } catch (e) {
  356. _logger.warning("Failed to save file", e);
  357. await dialog.hide();
  358. showGenericErrorDialog(context: context);
  359. } finally {
  360. PhotoManager.startChangeNotify();
  361. LocalSyncService.instance.checkAndSync().ignore();
  362. }
  363. }
  364. Future<void> _saveLivePhotoOnDroid(
  365. File image,
  366. File video,
  367. EnteFile enteFile,
  368. ) async {
  369. debugPrint("Downloading LivePhoto on Droid");
  370. AssetEntity? savedAsset = await (PhotoManager.editor
  371. .saveImageWithPath(image.path, title: enteFile.title!));
  372. if (savedAsset == null) {
  373. throw Exception("Failed to save image of live photo");
  374. }
  375. IgnoredFile ignoreVideoFile = IgnoredFile(
  376. savedAsset.id,
  377. savedAsset.title ?? '',
  378. savedAsset.relativePath ?? 'remoteDownload',
  379. "remoteDownload",
  380. );
  381. await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
  382. final videoTitle = file_path.basenameWithoutExtension(enteFile.title!) +
  383. file_path.extension(video.path);
  384. savedAsset = (await (PhotoManager.editor.saveVideo(
  385. video,
  386. title: videoTitle,
  387. )));
  388. if (savedAsset == null) {
  389. throw Exception("Failed to save video of live photo");
  390. }
  391. ignoreVideoFile = IgnoredFile(
  392. savedAsset.id,
  393. savedAsset.title ?? videoTitle,
  394. savedAsset.relativePath ?? 'remoteDownload',
  395. "remoteDownload",
  396. );
  397. await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
  398. }
  399. Future<void> _setAs(EnteFile file) async {
  400. final dialog = createProgressDialog(context, S.of(context).pleaseWait);
  401. await dialog.show();
  402. try {
  403. final File? fileToSave = await (getFile(file));
  404. if (fileToSave == null) {
  405. throw Exception("Fail to get file for setAs operation");
  406. }
  407. final m = MediaExtension();
  408. final bool result = await m.setAs("file://${fileToSave.path}", "image/*");
  409. if (result == false) {
  410. showShortToast(context, S.of(context).somethingWentWrong);
  411. }
  412. dialog.hide();
  413. } catch (e) {
  414. dialog.hide();
  415. _logger.severe("Failed to use as", e);
  416. showGenericErrorDialog(context: context);
  417. }
  418. }
  419. }