file_app_bar.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:logging/logging.dart';
  4. import 'package:media_extension/media_extension.dart';
  5. import "package:photos/generated/l10n.dart";
  6. import "package:photos/l10n/l10n.dart";
  7. import "package:photos/models/file/extensions/file_props.dart";
  8. import 'package:photos/models/file/file.dart';
  9. import 'package:photos/models/file/file_type.dart';
  10. import 'package:photos/models/file/trash_file.dart';
  11. import "package:photos/models/metadata/common_keys.dart";
  12. import 'package:photos/models/selected_files.dart';
  13. import 'package:photos/services/collections_service.dart';
  14. import 'package:photos/services/hidden_service.dart';
  15. import 'package:photos/ui/collections/collection_action_sheet.dart';
  16. import 'package:photos/ui/viewer/file/custom_app_bar.dart';
  17. import "package:photos/ui/viewer/file_details/favorite_widget.dart";
  18. import "package:photos/ui/viewer/file_details/upload_icon_widget.dart";
  19. import 'package:photos/utils/dialog_util.dart';
  20. import "package:photos/utils/file_download_util.dart";
  21. import 'package:photos/utils/file_util.dart';
  22. import "package:photos/utils/magic_util.dart";
  23. import 'package:photos/utils/toast_util.dart';
  24. class FileAppBar extends StatefulWidget {
  25. final EnteFile file;
  26. final Function(EnteFile) onFileRemoved;
  27. final double height;
  28. final bool shouldShowActions;
  29. final ValueNotifier<bool> enableFullScreenNotifier;
  30. const FileAppBar(
  31. this.file,
  32. this.onFileRemoved,
  33. this.height,
  34. this.shouldShowActions, {
  35. required this.enableFullScreenNotifier,
  36. Key? key,
  37. }) : super(key: key);
  38. @override
  39. FileAppBarState createState() => FileAppBarState();
  40. }
  41. class FileAppBarState extends State<FileAppBar> {
  42. final _logger = Logger("FadingAppBar");
  43. final List<Widget> _actions = [];
  44. @override
  45. void didUpdateWidget(FileAppBar oldWidget) {
  46. super.didUpdateWidget(oldWidget);
  47. if (oldWidget.file.generatedID != widget.file.generatedID) {
  48. _getActions();
  49. }
  50. }
  51. @override
  52. Widget build(BuildContext context) {
  53. _logger.fine("building app bar ${widget.file.generatedID?.toString()}");
  54. //When the widget is initialized, the actions are not available.
  55. //Cannot call _getActions() in initState.
  56. if (_actions.isEmpty) {
  57. _getActions();
  58. }
  59. final isTrashedFile = widget.file is TrashFile;
  60. final shouldShowActions = widget.shouldShowActions && !isTrashedFile;
  61. return CustomAppBar(
  62. ValueListenableBuilder(
  63. valueListenable: widget.enableFullScreenNotifier,
  64. builder: (context, bool isFullScreen, child) {
  65. return IgnorePointer(
  66. ignoring: isFullScreen,
  67. child: AnimatedOpacity(
  68. opacity: isFullScreen ? 0 : 1,
  69. duration: const Duration(milliseconds: 150),
  70. child: child,
  71. ),
  72. );
  73. },
  74. child: Container(
  75. decoration: BoxDecoration(
  76. gradient: LinearGradient(
  77. begin: Alignment.topCenter,
  78. end: Alignment.bottomCenter,
  79. colors: [
  80. Colors.black.withOpacity(0.72),
  81. Colors.black.withOpacity(0.6),
  82. Colors.transparent,
  83. ],
  84. stops: const [0, 0.2, 1],
  85. ),
  86. ),
  87. child: AppBar(
  88. iconTheme: const IconThemeData(
  89. color: Colors.white,
  90. ), //same for both themes
  91. actions: shouldShowActions ? _actions : [],
  92. elevation: 0,
  93. backgroundColor: const Color(0x00000000),
  94. ),
  95. ),
  96. ),
  97. Size.fromHeight(Platform.isAndroid ? 84 : 96),
  98. );
  99. }
  100. List<Widget> _getActions() {
  101. _actions.clear();
  102. final bool isOwnedByUser = widget.file.isOwner;
  103. final bool isFileUploaded = widget.file.isUploaded;
  104. bool isFileHidden = false;
  105. if (isOwnedByUser && isFileUploaded) {
  106. isFileHidden = CollectionsService.instance
  107. .getCollectionByID(widget.file.collectionID!)
  108. ?.isHidden() ??
  109. false;
  110. }
  111. if (widget.file.isLiveOrMotionPhoto) {
  112. _actions.add(
  113. IconButton(
  114. icon: const Icon(Icons.album_outlined),
  115. onPressed: () {
  116. showShortToast(
  117. context,
  118. S.of(context).pressAndHoldToPlayVideoDetailed,
  119. );
  120. },
  121. ),
  122. );
  123. }
  124. // only show fav option for files owned by the user
  125. if (!isFileHidden && isFileUploaded) {
  126. _actions.add(FavoriteWidget(widget.file));
  127. }
  128. if (!isFileUploaded) {
  129. _actions.add(
  130. UploadIconWidget(
  131. file: widget.file,
  132. key: ValueKey(widget.file.tag),
  133. ),
  134. );
  135. }
  136. final List<PopupMenuItem> items = [];
  137. if (widget.file.isRemoteFile) {
  138. items.add(
  139. PopupMenuItem(
  140. value: 1,
  141. child: Row(
  142. children: [
  143. Icon(
  144. Platform.isAndroid
  145. ? Icons.download
  146. : Icons.cloud_download_outlined,
  147. color: Theme.of(context).iconTheme.color,
  148. ),
  149. const Padding(
  150. padding: EdgeInsets.all(8),
  151. ),
  152. Text(S.of(context).download),
  153. ],
  154. ),
  155. ),
  156. );
  157. }
  158. // options for files owned by the user
  159. if (isOwnedByUser && !isFileHidden && isFileUploaded) {
  160. final bool isArchived =
  161. widget.file.magicMetadata.visibility == archiveVisibility;
  162. items.add(
  163. PopupMenuItem(
  164. value: 2,
  165. child: Row(
  166. children: [
  167. Icon(
  168. isArchived ? Icons.unarchive : Icons.archive_outlined,
  169. color: Theme.of(context).iconTheme.color,
  170. ),
  171. const Padding(
  172. padding: EdgeInsets.all(8),
  173. ),
  174. Text(
  175. isArchived ? S.of(context).unarchive : S.of(context).archive,
  176. ),
  177. ],
  178. ),
  179. ),
  180. );
  181. }
  182. if ((widget.file.fileType == FileType.image ||
  183. widget.file.fileType == FileType.livePhoto) &&
  184. Platform.isAndroid) {
  185. items.add(
  186. PopupMenuItem(
  187. value: 3,
  188. child: Row(
  189. children: [
  190. Icon(
  191. Icons.wallpaper_outlined,
  192. color: Theme.of(context).iconTheme.color,
  193. ),
  194. const Padding(
  195. padding: EdgeInsets.all(8),
  196. ),
  197. Text(S.of(context).setAs),
  198. ],
  199. ),
  200. ),
  201. );
  202. }
  203. if (isOwnedByUser && widget.file.isUploaded) {
  204. if (!isFileHidden) {
  205. items.add(
  206. PopupMenuItem(
  207. value: 4,
  208. child: Row(
  209. children: [
  210. Icon(
  211. Icons.visibility_off,
  212. color: Theme.of(context).iconTheme.color,
  213. ),
  214. const Padding(
  215. padding: EdgeInsets.all(8),
  216. ),
  217. Text(S.of(context).hide),
  218. ],
  219. ),
  220. ),
  221. );
  222. } else {
  223. items.add(
  224. PopupMenuItem(
  225. value: 5,
  226. child: Row(
  227. children: [
  228. Icon(
  229. Icons.visibility,
  230. color: Theme.of(context).iconTheme.color,
  231. ),
  232. const Padding(
  233. padding: EdgeInsets.all(8),
  234. ),
  235. Text(S.of(context).unhide),
  236. ],
  237. ),
  238. ),
  239. );
  240. }
  241. }
  242. if (items.isNotEmpty) {
  243. _actions.add(
  244. PopupMenuButton(
  245. itemBuilder: (context) {
  246. return items;
  247. },
  248. onSelected: (dynamic value) async {
  249. if (value == 1) {
  250. await _download(widget.file);
  251. } else if (value == 2) {
  252. await _toggleFileArchiveStatus(widget.file);
  253. } else if (value == 3) {
  254. await _setAs(widget.file);
  255. } else if (value == 4) {
  256. await _handleHideRequest(context);
  257. } else if (value == 5) {
  258. await _handleUnHideRequest(context);
  259. }
  260. },
  261. ),
  262. );
  263. }
  264. return _actions;
  265. }
  266. Future<void> _handleHideRequest(BuildContext context) async {
  267. try {
  268. final hideResult =
  269. await CollectionsService.instance.hideFiles(context, [widget.file]);
  270. if (hideResult) {
  271. widget.onFileRemoved(widget.file);
  272. }
  273. } catch (e, s) {
  274. _logger.severe("failed to update file visibility", e, s);
  275. await showGenericErrorDialog(context: context, error: e);
  276. }
  277. }
  278. Future<void> _handleUnHideRequest(BuildContext context) async {
  279. final selectedFiles = SelectedFiles();
  280. selectedFiles.files.add(widget.file);
  281. showCollectionActionSheet(
  282. context,
  283. selectedFiles: selectedFiles,
  284. actionType: CollectionActionType.unHide,
  285. );
  286. }
  287. Future<void> _toggleFileArchiveStatus(EnteFile file) async {
  288. final bool isArchived =
  289. widget.file.magicMetadata.visibility == archiveVisibility;
  290. await changeVisibility(
  291. context,
  292. [widget.file],
  293. isArchived ? visibleVisibility : archiveVisibility,
  294. );
  295. if (mounted) {
  296. setState(() {});
  297. }
  298. }
  299. Future<void> _download(EnteFile file) async {
  300. final dialog = createProgressDialog(
  301. context,
  302. context.l10n.downloading,
  303. isDismissible: true,
  304. );
  305. await dialog.show();
  306. try {
  307. await downloadToGallery(file);
  308. showToast(context, S.of(context).fileSavedToGallery);
  309. await dialog.hide();
  310. } catch (e) {
  311. _logger.warning("Failed to save file", e);
  312. await dialog.hide();
  313. await showGenericErrorDialog(context: context, error: e);
  314. }
  315. }
  316. Future<void> _setAs(EnteFile file) async {
  317. final dialog = createProgressDialog(context, S.of(context).pleaseWait);
  318. await dialog.show();
  319. try {
  320. final File? fileToSave = await (getFile(file));
  321. if (fileToSave == null) {
  322. throw Exception("Fail to get file for setAs operation");
  323. }
  324. final m = MediaExtension();
  325. final bool result = await m.setAs("file://${fileToSave.path}", "image/*");
  326. if (result == false) {
  327. showShortToast(context, S.of(context).somethingWentWrong);
  328. }
  329. await dialog.hide();
  330. } catch (e) {
  331. await dialog.hide();
  332. _logger.severe("Failed to use as", e);
  333. await showGenericErrorDialog(context: context, error: e);
  334. }
  335. }
  336. }