detail_page.dart 11 KB

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