fading_app_bar.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/services/favorites_service.dart';
  12. import 'package:photos/services/local_sync_service.dart';
  13. import 'package:photos/ui/custom_app_bar.dart';
  14. import 'package:photos/ui/set_wallpaper_dialog.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.localID == null) {
  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. if (Platform.isAndroid) {
  114. items.add(
  115. PopupMenuItem(
  116. value: 3,
  117. child: Row(
  118. children: [
  119. Icon(Icons.wallpaper_outlined),
  120. Padding(
  121. padding: EdgeInsets.all(8),
  122. ),
  123. Text("set wallpaper"),
  124. ],
  125. ),
  126. ),
  127. );
  128. }
  129. return items;
  130. },
  131. onSelected: (value) {
  132. if (value == 1) {
  133. _download(widget.file);
  134. } else if (value == 2) {
  135. _showDeleteSheet(widget.file);
  136. } else if (value == 3) {
  137. _setWallpaper(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: 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. var 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 actions = List<Widget>();
  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. final savedAsset = await PhotoManager.editor.saveImageWithPath(
  257. (await getFile(file)).path,
  258. title: file.title,
  259. );
  260. file.localID = savedAsset.id;
  261. await FilesDB.instance.insert(file);
  262. await LocalSyncService.instance.trackDownloadedFile(file);
  263. Bus.instance.fire(LocalPhotosUpdatedEvent([file]));
  264. await dialog.hide();
  265. showToast("file saved to gallery");
  266. }
  267. Future<void> _setWallpaper(File file) async {
  268. showDialog(
  269. context: context,
  270. builder: (BuildContext context) {
  271. return SetWallpaperDialog(file);
  272. },
  273. barrierColor: Colors.black87,
  274. );
  275. }
  276. }