detail_page.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:like_button/like_button.dart';
  4. import 'package:photos/core/cache/image_cache.dart';
  5. import 'package:photos/favorite_files_repository.dart';
  6. import 'package:photos/file_repository.dart';
  7. import 'package:photos/models/file_type.dart';
  8. import 'package:photos/models/file.dart';
  9. import 'package:photos/ui/video_widget.dart';
  10. import 'package:photos/ui/zoomable_image.dart';
  11. import 'package:photos/utils/date_time_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. 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. void _preloadFile(File file) {
  108. if (file.fileType == FileType.video) {
  109. return;
  110. }
  111. if (file.localId == null) {
  112. file.getBytes().then((data) {
  113. BytesLruCache.put(file, data);
  114. });
  115. } else {
  116. final cachedFile = FileLruCache.get(file);
  117. if (cachedFile == null) {
  118. file.getAsset().then((asset) {
  119. asset.file.then((assetFile) {
  120. FileLruCache.put(file, assetFile);
  121. });
  122. });
  123. }
  124. }
  125. }
  126. AppBar _buildAppBar() {
  127. final actions = List<Widget>();
  128. if (_files[_selectedIndex].localId != null) {
  129. actions.add(_getFavoriteButton());
  130. actions.add(_getDeleteButton());
  131. }
  132. actions.add(PopupMenuButton(
  133. itemBuilder: (context) {
  134. return [
  135. PopupMenuItem(
  136. value: 1,
  137. child: Row(
  138. children: [
  139. Icon(Icons.share),
  140. Padding(
  141. padding: EdgeInsets.all(8),
  142. ),
  143. Text("Share"),
  144. ],
  145. ),
  146. ),
  147. PopupMenuItem(
  148. value: 2,
  149. child: Row(
  150. children: [
  151. Icon(Icons.info),
  152. Padding(
  153. padding: EdgeInsets.all(8),
  154. ),
  155. Text("Info"),
  156. ],
  157. ),
  158. )
  159. ];
  160. },
  161. onSelected: (value) {
  162. if (value == 1) {
  163. share(context, _files[_selectedIndex]);
  164. } else if (value == 2) {
  165. _displayInfo(_files[_selectedIndex]);
  166. }
  167. },
  168. ));
  169. return AppBar(
  170. actions: actions,
  171. backgroundColor: Color(0x00000000),
  172. elevation: 0,
  173. );
  174. }
  175. Widget _getFavoriteButton() {
  176. final file = _files[_selectedIndex];
  177. return LikeButton(
  178. isLiked: FavoriteFilesRepository.instance.isLiked(file),
  179. onTap: (oldValue) {
  180. return FavoriteFilesRepository.instance.setLiked(file, !oldValue);
  181. },
  182. likeBuilder: (isLiked) {
  183. return Icon(
  184. Icons.favorite_border,
  185. color: isLiked ? Colors.pinkAccent : Colors.white,
  186. size: 30,
  187. );
  188. },
  189. );
  190. }
  191. Widget _getDeleteButton() {
  192. return IconButton(
  193. icon: Icon(Icons.delete_outline),
  194. iconSize: 30,
  195. onPressed: () {
  196. _showDeleteSheet();
  197. },
  198. );
  199. }
  200. Future<void> _displayInfo(File file) async {
  201. final asset = await file.getAsset();
  202. return showDialog<void>(
  203. context: context,
  204. builder: (BuildContext context) {
  205. var items = <Widget>[
  206. Row(
  207. children: [
  208. Icon(Icons.timer),
  209. Padding(padding: EdgeInsets.all(4)),
  210. Text(getFormattedTime(
  211. DateTime.fromMicrosecondsSinceEpoch(file.creationTime))),
  212. ],
  213. ),
  214. Padding(padding: EdgeInsets.all(4)),
  215. Row(
  216. children: [
  217. Icon(Icons.folder),
  218. Padding(padding: EdgeInsets.all(4)),
  219. Text(file.deviceFolder),
  220. ],
  221. ),
  222. Padding(padding: EdgeInsets.all(4)),
  223. ];
  224. if (file.fileType == FileType.image) {
  225. items.add(Row(
  226. children: [
  227. Icon(Icons.photo_size_select_actual),
  228. Padding(padding: EdgeInsets.all(4)),
  229. Text(asset.width.toString() + " x " + asset.height.toString()),
  230. ],
  231. ));
  232. } else {
  233. items.add(Row(
  234. children: [
  235. Icon(Icons.timer),
  236. Padding(padding: EdgeInsets.all(4)),
  237. Text(asset.videoDuration.toString()),
  238. ],
  239. ));
  240. }
  241. return AlertDialog(
  242. title: Text(file.title),
  243. content: SingleChildScrollView(
  244. child: ListBody(
  245. children: items,
  246. ),
  247. ),
  248. actions: <Widget>[
  249. FlatButton(
  250. child: Text('Ok'),
  251. onPressed: () {
  252. Navigator.of(context).pop();
  253. },
  254. ),
  255. ],
  256. );
  257. },
  258. );
  259. }
  260. void _showDeleteSheet() {
  261. final action = CupertinoActionSheet(
  262. actions: <Widget>[
  263. CupertinoActionSheetAction(
  264. child: Text("Delete on device"),
  265. isDestructiveAction: true,
  266. onPressed: () async {
  267. await _delete(false);
  268. },
  269. ),
  270. CupertinoActionSheetAction(
  271. child: Text("Delete everywhere [WiP]"),
  272. isDestructiveAction: true,
  273. onPressed: () async {
  274. await _delete(true);
  275. },
  276. )
  277. ],
  278. cancelButton: CupertinoActionSheetAction(
  279. child: Text("Cancel"),
  280. onPressed: () {
  281. Navigator.of(context, rootNavigator: true).pop();
  282. },
  283. ),
  284. );
  285. showCupertinoModalPopup(context: context, builder: (_) => action);
  286. }
  287. Future _delete(bool deleteEveryWhere) async {
  288. final file = _files[_selectedIndex];
  289. final totalFiles = _files.length;
  290. if (_selectedIndex == totalFiles - 1) {
  291. // Deleted the last file
  292. await _pageController.previousPage(
  293. duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
  294. } else {
  295. await _pageController.nextPage(
  296. duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
  297. setState(() {
  298. _files.remove(file);
  299. });
  300. Future.delayed(Duration(milliseconds: 200), () {
  301. _pageController.jumpToPage(_selectedIndex - 1);
  302. });
  303. }
  304. Navigator.of(context, rootNavigator: true).pop(); // Close dialog
  305. if (_files.length == 0) {
  306. // Deleted the last file in gallery
  307. Navigator.of(context, rootNavigator: true).pop(); // Close pageview
  308. Navigator.of(context, rootNavigator: true).pop(); // Close gallery
  309. }
  310. await deleteFiles([file], deleteEveryWhere: deleteEveryWhere);
  311. FileRepository.instance.reloadFiles();
  312. }
  313. }