fading_app_bar.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // @dart=2.9
  2. import 'dart:io';
  3. import 'dart:io' as io;
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:like_button/like_button.dart';
  7. import 'package:logging/logging.dart';
  8. import 'package:path/path.dart' as file_path;
  9. import 'package:photo_manager/photo_manager.dart';
  10. import 'package:photos/core/event_bus.dart';
  11. import 'package:photos/db/files_db.dart';
  12. import 'package:photos/events/local_photos_updated_event.dart';
  13. import 'package:photos/models/file.dart';
  14. import 'package:photos/models/file_type.dart';
  15. import 'package:photos/models/ignored_file.dart';
  16. import 'package:photos/models/trash_file.dart';
  17. import 'package:photos/services/favorites_service.dart';
  18. import 'package:photos/services/ignored_files_service.dart';
  19. import 'package:photos/services/local_sync_service.dart';
  20. import 'package:photos/ui/common/progress_dialog.dart';
  21. import 'package:photos/ui/viewer/file/custom_app_bar.dart';
  22. import 'package:photos/utils/delete_file_util.dart';
  23. import 'package:photos/utils/dialog_util.dart';
  24. import 'package:photos/utils/file_util.dart';
  25. import 'package:photos/utils/toast_util.dart';
  26. class FadingAppBar extends StatefulWidget implements PreferredSizeWidget {
  27. final File file;
  28. final Function(File) onFileDeleted;
  29. final double height;
  30. final bool shouldShowActions;
  31. final int userID;
  32. const FadingAppBar(
  33. this.file,
  34. this.onFileDeleted,
  35. this.userID,
  36. this.height,
  37. this.shouldShowActions, {
  38. Key key,
  39. }) : super(key: key);
  40. @override
  41. Size get preferredSize => Size.fromHeight(height);
  42. @override
  43. FadingAppBarState createState() => FadingAppBarState();
  44. }
  45. class FadingAppBarState extends State<FadingAppBar> {
  46. final _logger = Logger("FadingAppBar");
  47. bool _shouldHide = false;
  48. @override
  49. Widget build(BuildContext context) {
  50. return CustomAppBar(
  51. IgnorePointer(
  52. ignoring: _shouldHide,
  53. child: AnimatedOpacity(
  54. opacity: _shouldHide ? 0 : 1,
  55. duration: const Duration(milliseconds: 150),
  56. child: Container(
  57. decoration: BoxDecoration(
  58. gradient: LinearGradient(
  59. begin: Alignment.topCenter,
  60. end: Alignment.bottomCenter,
  61. colors: [
  62. Colors.black.withOpacity(0.72),
  63. Colors.black.withOpacity(0.6),
  64. Colors.transparent,
  65. ],
  66. stops: const [0, 0.2, 1],
  67. ),
  68. ),
  69. child: _buildAppBar(),
  70. ),
  71. ),
  72. ),
  73. height: Platform.isAndroid ? 80 : 96,
  74. );
  75. }
  76. void hide() {
  77. setState(() {
  78. _shouldHide = true;
  79. });
  80. }
  81. void show() {
  82. if (mounted) {
  83. setState(() {
  84. _shouldHide = false;
  85. });
  86. }
  87. }
  88. AppBar _buildAppBar() {
  89. debugPrint("building app bar");
  90. final List<Widget> actions = [];
  91. final isTrashedFile = widget.file is TrashFile;
  92. final shouldShowActions = widget.shouldShowActions && !isTrashedFile;
  93. // only show fav option for files owned by the user
  94. if (widget.file.ownerID == null || widget.file.ownerID == widget.userID) {
  95. actions.add(_getFavoriteButton());
  96. }
  97. actions.add(
  98. PopupMenuButton(
  99. itemBuilder: (context) {
  100. final List<PopupMenuItem> items = [];
  101. if (widget.file.isRemoteFile) {
  102. items.add(
  103. PopupMenuItem(
  104. value: 1,
  105. child: Row(
  106. children: [
  107. Icon(
  108. Platform.isAndroid
  109. ? Icons.download
  110. : CupertinoIcons.cloud_download,
  111. color: Theme.of(context).iconTheme.color,
  112. ),
  113. const Padding(
  114. padding: EdgeInsets.all(8),
  115. ),
  116. const Text("Download"),
  117. ],
  118. ),
  119. ),
  120. );
  121. }
  122. // options for files owned by the user
  123. if (widget.file.ownerID == null ||
  124. widget.file.ownerID == widget.userID) {
  125. items.add(
  126. PopupMenuItem(
  127. value: 2,
  128. child: Row(
  129. children: [
  130. Icon(
  131. Platform.isAndroid
  132. ? Icons.delete_outline
  133. : CupertinoIcons.delete,
  134. color: Theme.of(context).iconTheme.color,
  135. ),
  136. const Padding(
  137. padding: EdgeInsets.all(8),
  138. ),
  139. const Text("Delete"),
  140. ],
  141. ),
  142. ),
  143. );
  144. }
  145. return items;
  146. },
  147. onSelected: (value) {
  148. if (value == 1) {
  149. _download(widget.file);
  150. } else if (value == 2) {
  151. _showDeleteSheet(widget.file);
  152. }
  153. },
  154. ),
  155. );
  156. return AppBar(
  157. iconTheme:
  158. const IconThemeData(color: Colors.white), //same for both themes
  159. actions: shouldShowActions ? actions : [],
  160. elevation: 0,
  161. backgroundColor: const Color(0x00000000),
  162. );
  163. }
  164. Widget _getFavoriteButton() {
  165. return FutureBuilder(
  166. future: FavoritesService.instance.isFavorite(widget.file),
  167. builder: (context, snapshot) {
  168. if (snapshot.hasData) {
  169. return _getLikeButton(widget.file, snapshot.data);
  170. } else {
  171. return _getLikeButton(widget.file, false);
  172. }
  173. },
  174. );
  175. }
  176. Widget _getLikeButton(File file, bool isLiked) {
  177. return LikeButton(
  178. isLiked: isLiked,
  179. onTap: (oldValue) async {
  180. final isLiked = !oldValue;
  181. bool hasError = false;
  182. if (isLiked) {
  183. final shouldBlockUser = file.uploadedFileID == null;
  184. ProgressDialog dialog;
  185. if (shouldBlockUser) {
  186. dialog = createProgressDialog(context, "Adding to favorites...");
  187. await dialog.show();
  188. }
  189. try {
  190. await FavoritesService.instance.addToFavorites(file);
  191. } catch (e, s) {
  192. _logger.severe(e, s);
  193. hasError = true;
  194. showToast(context, "Sorry, could not add this to favorites!");
  195. } finally {
  196. if (shouldBlockUser) {
  197. await dialog.hide();
  198. }
  199. }
  200. } else {
  201. try {
  202. await FavoritesService.instance.removeFromFavorites(file);
  203. } catch (e, s) {
  204. _logger.severe(e, s);
  205. hasError = true;
  206. showToast(context, "Sorry, could not remove this from favorites!");
  207. }
  208. }
  209. return hasError ? oldValue : isLiked;
  210. },
  211. likeBuilder: (isLiked) {
  212. return Icon(
  213. isLiked ? Icons.favorite_rounded : Icons.favorite_border_rounded,
  214. color:
  215. isLiked ? Colors.pinkAccent : Colors.white, //same for both themes
  216. size: 24,
  217. );
  218. },
  219. );
  220. }
  221. void _showDeleteSheet(File file) {
  222. final List<Widget> actions = [];
  223. if (file.uploadedFileID == null || file.localID == null) {
  224. actions.add(
  225. CupertinoActionSheetAction(
  226. isDestructiveAction: true,
  227. onPressed: () async {
  228. await deleteFilesFromEverywhere(context, [file]);
  229. Navigator.of(context, rootNavigator: true).pop();
  230. widget.onFileDeleted(file);
  231. },
  232. child: const Text("Everywhere"),
  233. ),
  234. );
  235. } else {
  236. // uploaded file which is present locally too
  237. actions.add(
  238. CupertinoActionSheetAction(
  239. isDestructiveAction: true,
  240. onPressed: () async {
  241. await deleteFilesOnDeviceOnly(context, [file]);
  242. showToast(context, "File deleted from device");
  243. Navigator.of(context, rootNavigator: true).pop();
  244. // TODO: Fix behavior when inside a device folder
  245. },
  246. child: const Text("Device"),
  247. ),
  248. );
  249. actions.add(
  250. CupertinoActionSheetAction(
  251. isDestructiveAction: true,
  252. onPressed: () async {
  253. await deleteFilesFromRemoteOnly(context, [file]);
  254. showShortToast(context, "Moved to trash");
  255. Navigator.of(context, rootNavigator: true).pop();
  256. // TODO: Fix behavior when inside a collection
  257. },
  258. child: const Text("ente"),
  259. ),
  260. );
  261. actions.add(
  262. CupertinoActionSheetAction(
  263. isDestructiveAction: true,
  264. onPressed: () async {
  265. await deleteFilesFromEverywhere(context, [file]);
  266. Navigator.of(context, rootNavigator: true).pop();
  267. widget.onFileDeleted(file);
  268. },
  269. child: const Text("Everywhere"),
  270. ),
  271. );
  272. }
  273. final action = CupertinoActionSheet(
  274. title: const Text("Delete file?"),
  275. actions: actions,
  276. cancelButton: CupertinoActionSheetAction(
  277. child: const Text("Cancel"),
  278. onPressed: () {
  279. Navigator.of(context, rootNavigator: true).pop();
  280. },
  281. ),
  282. );
  283. showCupertinoModalPopup(context: context, builder: (_) => action);
  284. }
  285. Future<void> _download(File file) async {
  286. final dialog = createProgressDialog(context, "Downloading...");
  287. await dialog.show();
  288. final FileType type = file.fileType;
  289. // save and track image for livePhoto/image and video for FileType.video
  290. final io.File fileToSave = await getFile(file);
  291. final savedAsset = type == FileType.video
  292. ? (await PhotoManager.editor.saveVideo(fileToSave, title: file.title))
  293. : (await PhotoManager.editor
  294. .saveImageWithPath(fileToSave.path, title: file.title));
  295. // immediately track assetID to avoid duplicate upload
  296. await LocalSyncService.instance.trackDownloadedFile(savedAsset.id);
  297. file.localID = savedAsset.id;
  298. await FilesDB.instance.insert(file);
  299. if (type == FileType.livePhoto) {
  300. final io.File liveVideo = await getFileFromServer(file, liveVideo: true);
  301. if (liveVideo == null) {
  302. _logger.warning("Failed to find live video" + file.tag);
  303. } else {
  304. final videoTitle = file_path.basenameWithoutExtension(file.title) +
  305. file_path.extension(liveVideo.path);
  306. final savedAsset = (await PhotoManager.editor.saveVideo(
  307. liveVideo,
  308. title: videoTitle,
  309. ));
  310. final ignoreVideoFile = IgnoredFile(
  311. savedAsset.id,
  312. savedAsset.title ?? videoTitle,
  313. savedAsset.relativePath ?? 'remoteDownload',
  314. "remoteDownload",
  315. );
  316. debugPrint("IgnoreFile for auto-upload ${ignoreVideoFile.toString()}");
  317. await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
  318. }
  319. }
  320. Bus.instance.fire(LocalPhotosUpdatedEvent([file]));
  321. await dialog.hide();
  322. if (file.fileType == FileType.livePhoto) {
  323. showToast(context, "Photo and video saved to gallery");
  324. } else {
  325. showToast(context, "File saved to gallery");
  326. }
  327. }
  328. }