fading_app_bar.dart 7.8 KB

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