file_app_bar.dart 14 KB

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