file_app_bar.dart 13 KB

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