fading_app_bar.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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:photo_manager/photo_manager.dart';
  8. import 'package:photos/core/event_bus.dart';
  9. import 'package:photos/db/files_db.dart';
  10. import 'package:photos/events/local_photos_updated_event.dart';
  11. import 'package:photos/models/file.dart';
  12. import 'package:photos/models/file_type.dart';
  13. import 'package:photos/models/trash_file.dart';
  14. import 'package:photos/services/favorites_service.dart';
  15. import 'package:photos/services/local_sync_service.dart';
  16. import 'package:photos/ui/custom_app_bar.dart';
  17. import 'package:photos/ui/progress_dialog.dart';
  18. import 'package:photos/utils/date_time_util.dart';
  19. import 'package:photos/utils/delete_file_util.dart';
  20. import 'package:photos/utils/dialog_util.dart';
  21. import 'package:photos/utils/file_util.dart';
  22. import 'package:photos/utils/toast_util.dart';
  23. class FadingAppBar extends StatefulWidget implements PreferredSizeWidget {
  24. final File file;
  25. final Function(File) onFileDeleted;
  26. final double height;
  27. final bool shouldShowActions;
  28. final int userID;
  29. FadingAppBar(
  30. this.file,
  31. this.onFileDeleted,
  32. this.userID,
  33. this.height,
  34. this.shouldShowActions, {
  35. Key key,
  36. }) : super(key: key);
  37. @override
  38. Size get preferredSize => Size.fromHeight(height);
  39. @override
  40. FadingAppBarState createState() => FadingAppBarState();
  41. }
  42. class FadingAppBarState extends State<FadingAppBar> {
  43. final _logger = Logger("FadingAppBar");
  44. bool _shouldHide = false;
  45. @override
  46. Widget build(BuildContext context) {
  47. return CustomAppBar(
  48. AnimatedOpacity(
  49. child: Container(
  50. decoration: BoxDecoration(
  51. gradient: LinearGradient(
  52. begin: Alignment.topCenter,
  53. end: Alignment.bottomCenter,
  54. colors: [
  55. Colors.black.withOpacity(0.64),
  56. Colors.black.withOpacity(0.5),
  57. Colors.transparent,
  58. ],
  59. stops: const [0, 0.2, 1],
  60. ),
  61. ),
  62. child: _buildAppBar(),
  63. ),
  64. opacity: _shouldHide ? 0 : 1,
  65. duration: Duration(milliseconds: 150),
  66. ),
  67. height: 100,
  68. );
  69. }
  70. void hide() {
  71. setState(() {
  72. _shouldHide = true;
  73. });
  74. }
  75. void show() {
  76. if (mounted) {
  77. setState(() {
  78. _shouldHide = false;
  79. });
  80. }
  81. }
  82. AppBar _buildAppBar() {
  83. final List<Widget> actions = [];
  84. final shouldShowActions =
  85. widget.shouldShowActions && widget.file is! TrashFile;
  86. // only show fav option for files owned by the user
  87. if (widget.file.ownerID == null || widget.file.ownerID == widget.userID) {
  88. actions.add(_getFavoriteButton());
  89. }
  90. actions.add(PopupMenuButton(
  91. itemBuilder: (context) {
  92. final List<PopupMenuItem> items = [];
  93. if (widget.file.isRemoteFile()) {
  94. items.add(
  95. PopupMenuItem(
  96. value: 1,
  97. child: Row(
  98. children: [
  99. Icon(Platform.isAndroid
  100. ? Icons.download
  101. : CupertinoIcons.cloud_download),
  102. Padding(
  103. padding: EdgeInsets.all(8),
  104. ),
  105. Text("download"),
  106. ],
  107. ),
  108. ),
  109. );
  110. }
  111. // only show delete option for files owned by the user
  112. if (widget.file.ownerID == null ||
  113. widget.file.ownerID == widget.userID) {
  114. items.add(
  115. PopupMenuItem(
  116. value: 2,
  117. child: Row(
  118. children: [
  119. Icon(Platform.isAndroid
  120. ? Icons.delete_outline
  121. : CupertinoIcons.delete),
  122. Padding(
  123. padding: EdgeInsets.all(8),
  124. ),
  125. Text("delete"),
  126. ],
  127. ),
  128. ),
  129. );
  130. }
  131. return items;
  132. },
  133. onSelected: (value) {
  134. if (value == 1) {
  135. _download(widget.file);
  136. } else if (value == 2) {
  137. _showDeleteSheet(widget.file);
  138. }
  139. },
  140. ));
  141. return AppBar(
  142. title: Text(
  143. getDayTitle(widget.file.creationTime),
  144. style: TextStyle(
  145. fontSize: 14,
  146. ),
  147. ),
  148. actions: shouldShowActions ? actions : [],
  149. backgroundColor: Color(0x00000000),
  150. elevation: 0,
  151. );
  152. }
  153. Widget _getFavoriteButton() {
  154. return FutureBuilder(
  155. future: FavoritesService.instance.isFavorite(widget.file),
  156. builder: (context, snapshot) {
  157. if (snapshot.hasData) {
  158. return _getLikeButton(widget.file, snapshot.data);
  159. } else {
  160. return _getLikeButton(widget.file, false);
  161. }
  162. },
  163. );
  164. }
  165. Widget _getLikeButton(File file, bool isLiked) {
  166. return LikeButton(
  167. isLiked: isLiked,
  168. onTap: (oldValue) async {
  169. final isLiked = !oldValue;
  170. bool hasError = false;
  171. if (isLiked) {
  172. final shouldBlockUser = file.uploadedFileID == null;
  173. ProgressDialog dialog;
  174. if (shouldBlockUser) {
  175. dialog = createProgressDialog(context, "adding to favorites...");
  176. await dialog.show();
  177. }
  178. try {
  179. await FavoritesService.instance.addToFavorites(file);
  180. } catch (e, s) {
  181. _logger.severe(e, s);
  182. hasError = true;
  183. showToast("sorry, could not add this to favorites!");
  184. } finally {
  185. if (shouldBlockUser) {
  186. await dialog.hide();
  187. }
  188. }
  189. } else {
  190. try {
  191. await FavoritesService.instance.removeFromFavorites(file);
  192. } catch (e, s) {
  193. _logger.severe(e, s);
  194. hasError = true;
  195. showToast("sorry, could not remove this from favorites!");
  196. }
  197. }
  198. return hasError ? oldValue : isLiked;
  199. },
  200. likeBuilder: (isLiked) {
  201. return Icon(
  202. Icons.favorite_border,
  203. color: isLiked ? Colors.pinkAccent : Colors.white,
  204. size: 24,
  205. );
  206. },
  207. );
  208. }
  209. void _showDeleteSheet(File file) {
  210. final List<Widget> actions = [];
  211. if (file.uploadedFileID == null) {
  212. actions.add(CupertinoActionSheetAction(
  213. child: Text("everywhere"),
  214. isDestructiveAction: true,
  215. onPressed: () async {
  216. await deleteFilesFromEverywhere(context, [file]);
  217. widget.onFileDeleted(file);
  218. },
  219. ));
  220. } else {
  221. if (file.localID != null) {
  222. actions.add(CupertinoActionSheetAction(
  223. child: Text("on this device"),
  224. isDestructiveAction: true,
  225. onPressed: () async {
  226. await deleteFilesOnDeviceOnly(context, [file]);
  227. showToast("file deleted from device");
  228. Navigator.of(context, rootNavigator: true).pop();
  229. },
  230. ));
  231. }
  232. actions.add(CupertinoActionSheetAction(
  233. child: Text("everywhere"),
  234. isDestructiveAction: true,
  235. onPressed: () async {
  236. await deleteFilesFromEverywhere(context, [file]);
  237. widget.onFileDeleted(file);
  238. },
  239. ));
  240. }
  241. final action = CupertinoActionSheet(
  242. title: Text("delete file?"),
  243. actions: actions,
  244. cancelButton: CupertinoActionSheetAction(
  245. child: Text("cancel"),
  246. onPressed: () {
  247. Navigator.of(context, rootNavigator: true).pop();
  248. },
  249. ),
  250. );
  251. showCupertinoModalPopup(context: context, builder: (_) => action);
  252. }
  253. Future<void> _download(File file) async {
  254. final dialog = createProgressDialog(context, "downloading...");
  255. await dialog.show();
  256. FileType type = file.fileType;
  257. // save and track image for livePhoto/image and video for FileType.video
  258. io.File fileToSave = await getFile(file);
  259. final savedAsset = type == FileType.video
  260. ? (await PhotoManager.editor.saveVideo(fileToSave, title: file.title))
  261. : (await PhotoManager.editor
  262. .saveImageWithPath(fileToSave.path, title: file.title));
  263. // immediately track assetID to avoid duplicate upload
  264. await LocalSyncService.instance.trackDownloadedFile(savedAsset.id);
  265. file.localID = savedAsset.id;
  266. await FilesDB.instance.insert(file);
  267. if (type == FileType.livePhoto) {
  268. io.File liveVideo = await getFileFromServer(file, liveVideo: true);
  269. if (liveVideo == null) {
  270. _logger.warning("Failed to find live video" + file.tag());
  271. } else {
  272. final savedAsset = (await PhotoManager.editor.saveVideo(liveVideo));
  273. // in case of livePhoto, file.localID only points the image asset.
  274. // ignore the saved video asset for live photo from future downloads
  275. await LocalSyncService.instance.trackDownloadedFile(savedAsset.id);
  276. }
  277. }
  278. Bus.instance.fire(LocalPhotosUpdatedEvent([file]));
  279. await dialog.hide();
  280. if (file.fileType == FileType.livePhoto) {
  281. showToast("photo and video saved to gallery");
  282. } else {
  283. showToast("file saved to gallery");
  284. }
  285. }
  286. }