detail_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:like_button/like_button.dart';
  4. import 'package:photos/services/favorites_service.dart';
  5. import 'package:photos/models/file_type.dart';
  6. import 'package:photos/models/file.dart';
  7. import 'package:photos/ui/video_widget.dart';
  8. import 'package:photos/ui/zoomable_image.dart';
  9. import 'package:photos/utils/date_time_util.dart';
  10. import 'package:photos/utils/dialog_util.dart';
  11. import 'package:photos/utils/file_util.dart';
  12. import 'package:photos/utils/share_util.dart';
  13. import 'package:logging/logging.dart';
  14. import 'package:photos/utils/toast_util.dart';
  15. class DetailPage extends StatefulWidget {
  16. final List<File> files;
  17. final int selectedIndex;
  18. final String tagPrefix;
  19. DetailPage(this.files, this.selectedIndex, this.tagPrefix, {key})
  20. : super(key: key);
  21. @override
  22. _DetailPageState createState() => _DetailPageState();
  23. }
  24. class _DetailPageState extends State<DetailPage> {
  25. final _logger = Logger("DetailPageState");
  26. bool _shouldDisableScroll = false;
  27. List<File> _files;
  28. PageController _pageController;
  29. int _selectedIndex = 0;
  30. bool _hasPageChanged = false;
  31. @override
  32. void initState() {
  33. _files = widget.files;
  34. _selectedIndex = widget.selectedIndex;
  35. super.initState();
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. _logger.info("Opening " +
  40. _files[_selectedIndex].toString() +
  41. ". " +
  42. _selectedIndex.toString() +
  43. " / " +
  44. _files.length.toString() +
  45. " files .");
  46. return Scaffold(
  47. appBar: _buildAppBar(),
  48. extendBodyBehindAppBar: true,
  49. body: Center(
  50. child: Container(
  51. child: _buildPageView(),
  52. ),
  53. ),
  54. backgroundColor: Colors.black,
  55. );
  56. }
  57. Widget _buildPageView() {
  58. _pageController = PageController(initialPage: _selectedIndex);
  59. return PageView.builder(
  60. itemBuilder: (context, index) {
  61. final file = _files[index];
  62. Widget content;
  63. if (file.fileType == FileType.image) {
  64. content = ZoomableImage(
  65. file,
  66. shouldDisableScroll: (value) {
  67. setState(() {
  68. _shouldDisableScroll = value;
  69. });
  70. },
  71. tagPrefix: widget.tagPrefix,
  72. );
  73. } else if (file.fileType == FileType.video) {
  74. content = VideoWidget(
  75. file,
  76. autoPlay: !_hasPageChanged, // Autoplay if it was opened directly
  77. tagPrefix: widget.tagPrefix,
  78. );
  79. } else {
  80. content = Icon(Icons.error);
  81. }
  82. _preloadFiles(index);
  83. return content;
  84. },
  85. onPageChanged: (index) {
  86. setState(() {
  87. _selectedIndex = index;
  88. _hasPageChanged = true;
  89. });
  90. _preloadFiles(index);
  91. },
  92. physics: _shouldDisableScroll
  93. ? NeverScrollableScrollPhysics()
  94. : PageScrollPhysics(),
  95. controller: _pageController,
  96. itemCount: _files.length,
  97. );
  98. }
  99. void _preloadFiles(int index) {
  100. if (index > 0) {
  101. preloadFile(_files[index - 1]);
  102. }
  103. if (index < _files.length - 1) {
  104. preloadFile(_files[index + 1]);
  105. }
  106. }
  107. AppBar _buildAppBar() {
  108. final actions = List<Widget>();
  109. actions.add(_getFavoriteButton());
  110. actions.add(PopupMenuButton(
  111. itemBuilder: (context) {
  112. return [
  113. PopupMenuItem(
  114. value: 1,
  115. child: Row(
  116. children: [
  117. Icon(Icons.share),
  118. Padding(
  119. padding: EdgeInsets.all(8),
  120. ),
  121. Text("Share"),
  122. ],
  123. ),
  124. ),
  125. PopupMenuItem(
  126. value: 2,
  127. child: Row(
  128. children: [
  129. Icon(Icons.info),
  130. Padding(
  131. padding: EdgeInsets.all(8),
  132. ),
  133. Text("Info"),
  134. ],
  135. ),
  136. ),
  137. PopupMenuItem(
  138. value: 3,
  139. child: Row(
  140. children: [
  141. Icon(Icons.delete),
  142. Padding(
  143. padding: EdgeInsets.all(8),
  144. ),
  145. Text("Delete"),
  146. ],
  147. ),
  148. )
  149. ];
  150. },
  151. onSelected: (value) {
  152. if (value == 1) {
  153. share(context, _files[_selectedIndex]);
  154. } else if (value == 2) {
  155. _displayInfo(_files[_selectedIndex]);
  156. } else if (value == 3) {
  157. _showDeleteSheet();
  158. }
  159. },
  160. ));
  161. return AppBar(
  162. actions: actions,
  163. backgroundColor: Color(0x00000000),
  164. elevation: 0,
  165. );
  166. }
  167. Widget _getFavoriteButton() {
  168. final file = _files[_selectedIndex];
  169. return FutureBuilder(
  170. future: FavoritesService.instance.isFavorite(file),
  171. builder: (context, snapshot) {
  172. if (snapshot.hasData) {
  173. return _getLikeButton(file, snapshot.data);
  174. } else {
  175. return _getLikeButton(file, false);
  176. }
  177. },
  178. );
  179. }
  180. Widget _getLikeButton(File file, bool isLiked) {
  181. return LikeButton(
  182. isLiked: isLiked,
  183. onTap: (oldValue) async {
  184. final isLiked = !oldValue;
  185. bool hasError = false;
  186. if (isLiked) {
  187. final shouldBlockUser = file.uploadedFileID == null;
  188. var dialog;
  189. if (shouldBlockUser) {
  190. dialog = createProgressDialog(context, "Adding to favorites...");
  191. await dialog.show();
  192. }
  193. try {
  194. await FavoritesService.instance.addToFavorites(file);
  195. } catch (e, s) {
  196. _logger.severe(e, s);
  197. hasError = true;
  198. showToast("Sorry, could not add this to favorites!");
  199. } finally {
  200. if (shouldBlockUser) {
  201. await dialog.hide();
  202. }
  203. }
  204. } else {
  205. try {
  206. await FavoritesService.instance.removeFromFavorites(file);
  207. } catch (e, s) {
  208. _logger.severe(e, s);
  209. hasError = true;
  210. showToast("Sorry, could not remove this from favorites!");
  211. }
  212. }
  213. return hasError ? oldValue : isLiked;
  214. },
  215. likeBuilder: (isLiked) {
  216. return Icon(
  217. Icons.favorite_border,
  218. color: isLiked ? Colors.pinkAccent : Colors.white,
  219. size: 30,
  220. );
  221. },
  222. );
  223. }
  224. Future<void> _displayInfo(File file) async {
  225. var asset;
  226. final isLocalFile = file.localID != null;
  227. if (isLocalFile) {
  228. asset = await file.getAsset();
  229. }
  230. return showDialog<void>(
  231. context: context,
  232. builder: (BuildContext context) {
  233. var items = <Widget>[
  234. Row(
  235. children: [
  236. Icon(Icons.timer),
  237. Padding(padding: EdgeInsets.all(4)),
  238. Text(getFormattedTime(
  239. DateTime.fromMicrosecondsSinceEpoch(file.creationTime))),
  240. ],
  241. ),
  242. Padding(padding: EdgeInsets.all(4)),
  243. Row(
  244. children: [
  245. Icon(Icons.folder),
  246. Padding(padding: EdgeInsets.all(4)),
  247. Text(file.deviceFolder),
  248. ],
  249. ),
  250. Padding(padding: EdgeInsets.all(4)),
  251. ];
  252. if (isLocalFile) {
  253. if (file.fileType == FileType.image) {
  254. items.add(Row(
  255. children: [
  256. Icon(Icons.photo_size_select_actual),
  257. Padding(padding: EdgeInsets.all(4)),
  258. Text(asset.width.toString() + " x " + asset.height.toString()),
  259. ],
  260. ));
  261. } else {
  262. items.add(Row(
  263. children: [
  264. Icon(Icons.timer),
  265. Padding(padding: EdgeInsets.all(4)),
  266. Text(asset.videoDuration.toString()),
  267. ],
  268. ));
  269. }
  270. }
  271. if (file.uploadedFileID != null) {
  272. items.add(
  273. Padding(padding: EdgeInsets.all(4)),
  274. );
  275. items.add(Row(
  276. children: [
  277. Icon(Icons.cloud_upload),
  278. Padding(padding: EdgeInsets.all(4)),
  279. Text(getFormattedTime(
  280. DateTime.fromMicrosecondsSinceEpoch(file.updationTime))),
  281. ],
  282. ));
  283. }
  284. return AlertDialog(
  285. title: Text(file.title),
  286. content: SingleChildScrollView(
  287. child: ListBody(
  288. children: items,
  289. ),
  290. ),
  291. actions: <Widget>[
  292. FlatButton(
  293. child: Text('Ok'),
  294. onPressed: () {
  295. Navigator.of(context).pop();
  296. },
  297. ),
  298. ],
  299. );
  300. },
  301. );
  302. }
  303. void _showDeleteSheet() {
  304. final fileToBeDeleted = _files[_selectedIndex];
  305. final actions = List<Widget>();
  306. if (fileToBeDeleted.uploadedFileID == null) {
  307. actions.add(CupertinoActionSheetAction(
  308. child: Text("Everywhere"),
  309. isDestructiveAction: true,
  310. onPressed: () async {
  311. await deleteFilesFromEverywhere(context, [fileToBeDeleted]);
  312. _onFileDeleted();
  313. },
  314. ));
  315. } else {
  316. if (fileToBeDeleted.localID != null) {
  317. actions.add(CupertinoActionSheetAction(
  318. child: Text("On this device"),
  319. isDestructiveAction: true,
  320. onPressed: () async {
  321. await deleteFilesOnDeviceOnly(context, [fileToBeDeleted]);
  322. showToast("File deleted from device");
  323. Navigator.of(context, rootNavigator: true).pop();
  324. },
  325. ));
  326. }
  327. actions.add(CupertinoActionSheetAction(
  328. child: Text("Everywhere"),
  329. isDestructiveAction: true,
  330. onPressed: () async {
  331. await deleteFilesFromEverywhere(context, [fileToBeDeleted]);
  332. _onFileDeleted();
  333. },
  334. ));
  335. }
  336. final action = CupertinoActionSheet(
  337. title: Text("Delete file?"),
  338. actions: actions,
  339. cancelButton: CupertinoActionSheetAction(
  340. child: Text("Cancel"),
  341. onPressed: () {
  342. Navigator.of(context, rootNavigator: true).pop();
  343. },
  344. ),
  345. );
  346. showCupertinoModalPopup(context: context, builder: (_) => action);
  347. }
  348. Future _onFileDeleted() async {
  349. final file = _files[_selectedIndex];
  350. final totalFiles = _files.length;
  351. if (totalFiles == 1) {
  352. // Deleted the only file
  353. Navigator.of(context, rootNavigator: true).pop(); // Close pageview
  354. Navigator.of(context, rootNavigator: true).pop(); // Close gallery
  355. return;
  356. }
  357. if (_selectedIndex == totalFiles - 1) {
  358. // Deleted the last file
  359. await _pageController.previousPage(
  360. duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
  361. setState(() {
  362. _files.remove(file);
  363. });
  364. } else {
  365. await _pageController.nextPage(
  366. duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
  367. setState(() {
  368. _selectedIndex--;
  369. _files.remove(file);
  370. });
  371. }
  372. Navigator.of(context, rootNavigator: true).pop(); // Close dialog
  373. }
  374. }