file_app_bar.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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: 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(FavoriteWidget(widget.file));
  120. }
  121. if (!isFileUploaded) {
  122. actions.add(
  123. UploadIconWidget(
  124. file: widget.file,
  125. key: ValueKey(widget.file.tag),
  126. ),
  127. );
  128. }
  129. actions.add(
  130. PopupMenuButton(
  131. itemBuilder: (context) {
  132. final List<PopupMenuItem> items = [];
  133. if (widget.file.isRemoteFile) {
  134. items.add(
  135. PopupMenuItem(
  136. value: 1,
  137. child: Row(
  138. children: [
  139. Icon(
  140. Platform.isAndroid
  141. ? Icons.download
  142. : CupertinoIcons.cloud_download,
  143. color: Theme.of(context).iconTheme.color,
  144. ),
  145. const Padding(
  146. padding: EdgeInsets.all(8),
  147. ),
  148. Text(S.of(context).download),
  149. ],
  150. ),
  151. ),
  152. );
  153. }
  154. // options for files owned by the user
  155. if (isOwnedByUser && !isFileHidden && isFileUploaded) {
  156. final bool isArchived =
  157. widget.file.magicMetadata.visibility == archiveVisibility;
  158. items.add(
  159. PopupMenuItem(
  160. value: 2,
  161. child: Row(
  162. children: [
  163. Icon(
  164. isArchived ? Icons.unarchive : Icons.archive_outlined,
  165. color: Theme.of(context).iconTheme.color,
  166. ),
  167. const Padding(
  168. padding: EdgeInsets.all(8),
  169. ),
  170. Text(
  171. isArchived
  172. ? S.of(context).unarchive
  173. : S.of(context).archive,
  174. ),
  175. ],
  176. ),
  177. ),
  178. );
  179. }
  180. if ((widget.file.fileType == FileType.image ||
  181. widget.file.fileType == FileType.livePhoto) &&
  182. Platform.isAndroid) {
  183. items.add(
  184. PopupMenuItem(
  185. value: 3,
  186. child: Row(
  187. children: [
  188. Icon(
  189. Icons.wallpaper_outlined,
  190. color: Theme.of(context).iconTheme.color,
  191. ),
  192. const Padding(
  193. padding: EdgeInsets.all(8),
  194. ),
  195. Text(S.of(context).setAs),
  196. ],
  197. ),
  198. ),
  199. );
  200. }
  201. if (isOwnedByUser && widget.file.isUploaded) {
  202. if (!isFileHidden) {
  203. items.add(
  204. PopupMenuItem(
  205. value: 4,
  206. child: Row(
  207. children: [
  208. Icon(
  209. Icons.visibility_off,
  210. color: Theme.of(context).iconTheme.color,
  211. ),
  212. const Padding(
  213. padding: EdgeInsets.all(8),
  214. ),
  215. Text(S.of(context).hide),
  216. ],
  217. ),
  218. ),
  219. );
  220. } else {
  221. items.add(
  222. PopupMenuItem(
  223. value: 5,
  224. child: Row(
  225. children: [
  226. Icon(
  227. Icons.visibility,
  228. color: Theme.of(context).iconTheme.color,
  229. ),
  230. const Padding(
  231. padding: EdgeInsets.all(8),
  232. ),
  233. Text(S.of(context).unhide),
  234. ],
  235. ),
  236. ),
  237. );
  238. }
  239. }
  240. return items;
  241. },
  242. onSelected: (dynamic value) async {
  243. if (value == 1) {
  244. _download(widget.file);
  245. } else if (value == 2) {
  246. await _toggleFileArchiveStatus(widget.file);
  247. } else if (value == 3) {
  248. _setAs(widget.file);
  249. } else if (value == 4) {
  250. _handleHideRequest(context);
  251. } else if (value == 5) {
  252. _handleUnHideRequest(context);
  253. }
  254. },
  255. ),
  256. );
  257. return AppBar(
  258. iconTheme:
  259. const IconThemeData(color: Colors.white), //same for both themes
  260. actions: shouldShowActions ? actions : [],
  261. elevation: 0,
  262. backgroundColor: const Color(0x00000000),
  263. );
  264. }
  265. Future<void> _handleHideRequest(BuildContext context) async {
  266. try {
  267. final hideResult =
  268. await CollectionsService.instance.hideFiles(context, [widget.file]);
  269. if (hideResult) {
  270. widget.onFileRemoved(widget.file);
  271. }
  272. } catch (e, s) {
  273. _logger.severe("failed to update file visibility", e, s);
  274. await showGenericErrorDialog(context: context);
  275. }
  276. }
  277. Future<void> _handleUnHideRequest(BuildContext context) async {
  278. final selectedFiles = SelectedFiles();
  279. selectedFiles.files.add(widget.file);
  280. showCollectionActionSheet(
  281. context,
  282. selectedFiles: selectedFiles,
  283. actionType: CollectionActionType.unHide,
  284. );
  285. }
  286. Future<void> _toggleFileArchiveStatus(EnteFile file) async {
  287. final bool isArchived =
  288. widget.file.magicMetadata.visibility == archiveVisibility;
  289. await changeVisibility(
  290. context,
  291. [widget.file],
  292. isArchived ? visibleVisibility : archiveVisibility,
  293. );
  294. if (mounted) {
  295. setState(() {});
  296. }
  297. }
  298. Future<void> _download(EnteFile file) async {
  299. final dialog = createProgressDialog(
  300. context,
  301. context.l10n.downloading,
  302. isDismissible: true,
  303. );
  304. await dialog.show();
  305. try {
  306. final FileType type = file.fileType;
  307. final bool downloadLivePhotoOnDroid =
  308. type == FileType.livePhoto && Platform.isAndroid;
  309. AssetEntity? savedAsset;
  310. final File? fileToSave = await getFile(file);
  311. //Disabling notifications for assets changing to insert the file into
  312. //files db before triggering a sync.
  313. PhotoManager.stopChangeNotify();
  314. if (type == FileType.image) {
  315. savedAsset = await PhotoManager.editor
  316. .saveImageWithPath(fileToSave!.path, title: file.title!);
  317. } else if (type == FileType.video) {
  318. savedAsset = await PhotoManager.editor
  319. .saveVideo(fileToSave!, title: file.title!);
  320. } else if (type == FileType.livePhoto) {
  321. final File? liveVideoFile =
  322. await getFileFromServer(file, liveVideo: true);
  323. if (liveVideoFile == null) {
  324. throw AssertionError("Live video can not be null");
  325. }
  326. if (downloadLivePhotoOnDroid) {
  327. await _saveLivePhotoOnDroid(fileToSave!, liveVideoFile, file);
  328. } else {
  329. savedAsset = await PhotoManager.editor.darwin.saveLivePhoto(
  330. imageFile: fileToSave!,
  331. videoFile: liveVideoFile,
  332. title: file.title!,
  333. );
  334. }
  335. }
  336. if (savedAsset != null) {
  337. file.localID = savedAsset.id;
  338. await FilesDB.instance.insert(file);
  339. Bus.instance.fire(
  340. LocalPhotosUpdatedEvent(
  341. [file],
  342. source: "download",
  343. ),
  344. );
  345. } else if (!downloadLivePhotoOnDroid && savedAsset == null) {
  346. _logger.severe('Failed to save assert of type $type');
  347. }
  348. showToast(context, S.of(context).fileSavedToGallery);
  349. await dialog.hide();
  350. } catch (e) {
  351. _logger.warning("Failed to save file", e);
  352. await dialog.hide();
  353. showGenericErrorDialog(context: context);
  354. } finally {
  355. PhotoManager.startChangeNotify();
  356. LocalSyncService.instance.checkAndSync().ignore();
  357. }
  358. }
  359. Future<void> _saveLivePhotoOnDroid(
  360. File image,
  361. File video,
  362. EnteFile enteFile,
  363. ) async {
  364. debugPrint("Downloading LivePhoto on Droid");
  365. AssetEntity? savedAsset = await (PhotoManager.editor
  366. .saveImageWithPath(image.path, title: enteFile.title!));
  367. if (savedAsset == null) {
  368. throw Exception("Failed to save image of live photo");
  369. }
  370. IgnoredFile ignoreVideoFile = IgnoredFile(
  371. savedAsset.id,
  372. savedAsset.title ?? '',
  373. savedAsset.relativePath ?? 'remoteDownload',
  374. "remoteDownload",
  375. );
  376. await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
  377. final videoTitle = file_path.basenameWithoutExtension(enteFile.title!) +
  378. file_path.extension(video.path);
  379. savedAsset = (await (PhotoManager.editor.saveVideo(
  380. video,
  381. title: videoTitle,
  382. )));
  383. if (savedAsset == null) {
  384. throw Exception("Failed to save video of live photo");
  385. }
  386. ignoreVideoFile = IgnoredFile(
  387. savedAsset.id,
  388. savedAsset.title ?? videoTitle,
  389. savedAsset.relativePath ?? 'remoteDownload',
  390. "remoteDownload",
  391. );
  392. await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
  393. }
  394. Future<void> _setAs(EnteFile file) async {
  395. final dialog = createProgressDialog(context, S.of(context).pleaseWait);
  396. await dialog.show();
  397. try {
  398. final File? fileToSave = await (getFile(file));
  399. if (fileToSave == null) {
  400. throw Exception("Fail to get file for setAs operation");
  401. }
  402. final m = MediaExtension();
  403. final bool result = await m.setAs("file://${fileToSave.path}", "image/*");
  404. if (result == false) {
  405. showShortToast(context, S.of(context).somethingWentWrong);
  406. }
  407. dialog.hide();
  408. } catch (e) {
  409. dialog.hide();
  410. _logger.severe("Failed to use as", e);
  411. showGenericErrorDialog(context: context);
  412. }
  413. }
  414. }