file_app_bar.dart 14 KB

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