fading_app_bar.dart 11 KB

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