detail_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 dialog =
  188. createProgressDialog(context, "Adding to favorites...");
  189. await dialog.show();
  190. try {
  191. await FavoritesService.instance.addToFavorites(file);
  192. showToast("Added to favorites.");
  193. } catch (e, s) {
  194. _logger.severe(e, s);
  195. await dialog.hide();
  196. hasError = true;
  197. showGenericErrorDialog(context);
  198. } finally {
  199. await dialog.hide();
  200. }
  201. } else {
  202. final dialog =
  203. createProgressDialog(context, "Removing from favorites...");
  204. await dialog.show();
  205. try {
  206. await FavoritesService.instance.removeFromFavorites(file);
  207. showToast("Removed from favorites.");
  208. } catch (e, s) {
  209. _logger.severe(e, s);
  210. await dialog.hide();
  211. hasError = true;
  212. showGenericErrorDialog(context);
  213. } finally {
  214. await dialog.hide();
  215. }
  216. }
  217. return hasError ? oldValue : isLiked;
  218. },
  219. likeBuilder: (isLiked) {
  220. return Icon(
  221. Icons.favorite_border,
  222. color: isLiked ? Colors.pinkAccent : Colors.white,
  223. size: 30,
  224. );
  225. },
  226. );
  227. }
  228. Future<void> _displayInfo(File file) async {
  229. var asset;
  230. final isLocalFile = file.localID != null;
  231. if (isLocalFile) {
  232. asset = await file.getAsset();
  233. }
  234. return showDialog<void>(
  235. context: context,
  236. builder: (BuildContext context) {
  237. var items = <Widget>[
  238. Row(
  239. children: [
  240. Icon(Icons.timer),
  241. Padding(padding: EdgeInsets.all(4)),
  242. Text(getFormattedTime(
  243. DateTime.fromMicrosecondsSinceEpoch(file.creationTime))),
  244. ],
  245. ),
  246. Padding(padding: EdgeInsets.all(4)),
  247. Row(
  248. children: [
  249. Icon(Icons.folder),
  250. Padding(padding: EdgeInsets.all(4)),
  251. Text(file.deviceFolder),
  252. ],
  253. ),
  254. Padding(padding: EdgeInsets.all(4)),
  255. ];
  256. if (isLocalFile) {
  257. if (file.fileType == FileType.image) {
  258. items.add(Row(
  259. children: [
  260. Icon(Icons.photo_size_select_actual),
  261. Padding(padding: EdgeInsets.all(4)),
  262. Text(asset.width.toString() + " x " + asset.height.toString()),
  263. ],
  264. ));
  265. } else {
  266. items.add(Row(
  267. children: [
  268. Icon(Icons.timer),
  269. Padding(padding: EdgeInsets.all(4)),
  270. Text(asset.videoDuration.toString()),
  271. ],
  272. ));
  273. }
  274. }
  275. if (file.uploadedFileID != null) {
  276. items.add(
  277. Padding(padding: EdgeInsets.all(4)),
  278. );
  279. items.add(Row(
  280. children: [
  281. Icon(Icons.cloud_upload),
  282. Padding(padding: EdgeInsets.all(4)),
  283. Text(getFormattedTime(
  284. DateTime.fromMicrosecondsSinceEpoch(file.updationTime))),
  285. ],
  286. ));
  287. }
  288. return AlertDialog(
  289. title: Text(file.title),
  290. content: SingleChildScrollView(
  291. child: ListBody(
  292. children: items,
  293. ),
  294. ),
  295. actions: <Widget>[
  296. FlatButton(
  297. child: Text('Ok'),
  298. onPressed: () {
  299. Navigator.of(context).pop();
  300. },
  301. ),
  302. ],
  303. );
  304. },
  305. );
  306. }
  307. void _showDeleteSheet() {
  308. final fileToBeDeleted = _files[_selectedIndex];
  309. final actions = List<Widget>();
  310. if (fileToBeDeleted.uploadedFileID == null) {
  311. actions.add(CupertinoActionSheetAction(
  312. child: Text("Everywhere"),
  313. isDestructiveAction: true,
  314. onPressed: () async {
  315. await deleteFilesFromEverywhere(context, [fileToBeDeleted]);
  316. _onFileDeleted();
  317. },
  318. ));
  319. } else {
  320. if (fileToBeDeleted.localID != null) {
  321. actions.add(CupertinoActionSheetAction(
  322. child: Text("On this device"),
  323. isDestructiveAction: true,
  324. onPressed: () async {
  325. await deleteFilesOnDeviceOnly(context, [fileToBeDeleted]);
  326. showToast("File deleted from device");
  327. Navigator.of(context, rootNavigator: true).pop();
  328. },
  329. ));
  330. }
  331. actions.add(CupertinoActionSheetAction(
  332. child: Text("Everywhere"),
  333. isDestructiveAction: true,
  334. onPressed: () async {
  335. await deleteFilesFromEverywhere(context, [fileToBeDeleted]);
  336. _onFileDeleted();
  337. },
  338. ));
  339. }
  340. final action = CupertinoActionSheet(
  341. title: Text("Delete file?"),
  342. actions: actions,
  343. cancelButton: CupertinoActionSheetAction(
  344. child: Text("Cancel"),
  345. onPressed: () {
  346. Navigator.of(context, rootNavigator: true).pop();
  347. },
  348. ),
  349. );
  350. showCupertinoModalPopup(context: context, builder: (_) => action);
  351. }
  352. Future _onFileDeleted() async {
  353. final file = _files[_selectedIndex];
  354. final totalFiles = _files.length;
  355. if (totalFiles == 1) {
  356. // Deleted the only file
  357. Navigator.of(context, rootNavigator: true).pop(); // Close pageview
  358. Navigator.of(context, rootNavigator: true).pop(); // Close gallery
  359. return;
  360. }
  361. if (_selectedIndex == totalFiles - 1) {
  362. // Deleted the last file
  363. await _pageController.previousPage(
  364. duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
  365. setState(() {
  366. _files.remove(file);
  367. });
  368. } else {
  369. await _pageController.nextPage(
  370. duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
  371. setState(() {
  372. _selectedIndex--;
  373. _files.remove(file);
  374. });
  375. }
  376. Navigator.of(context, rootNavigator: true).pop(); // Close dialog
  377. }
  378. }