detail_page.dart 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import 'package:extended_image/extended_image.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:logging/logging.dart';
  5. import 'package:photos/core/configuration.dart';
  6. import 'package:photos/core/constants.dart';
  7. import 'package:photos/core/errors.dart';
  8. import 'package:photos/models/file.dart';
  9. import 'package:photos/ui/tools/editor/image_editor_page.dart';
  10. import 'package:photos/ui/viewer/file/fading_app_bar.dart';
  11. import 'package:photos/ui/viewer/file/fading_bottom_bar.dart';
  12. import 'package:photos/ui/viewer/file/file_widget.dart';
  13. import 'package:photos/ui/viewer/gallery/gallery.dart';
  14. import 'package:photos/utils/dialog_util.dart';
  15. import 'package:photos/utils/file_util.dart';
  16. import 'package:photos/utils/navigation_util.dart';
  17. enum DetailPageMode {
  18. minimalistic,
  19. full,
  20. }
  21. class DetailPageConfiguration {
  22. final List<File> files;
  23. final GalleryLoader asyncLoader;
  24. final int selectedIndex;
  25. final String tagPrefix;
  26. final DetailPageMode mode;
  27. DetailPageConfiguration(
  28. this.files,
  29. this.asyncLoader,
  30. this.selectedIndex,
  31. this.tagPrefix, {
  32. this.mode = DetailPageMode.full,
  33. });
  34. DetailPageConfiguration copyWith({
  35. List<File> files,
  36. GalleryLoader asyncLoader,
  37. int selectedIndex,
  38. String tagPrefix,
  39. }) {
  40. return DetailPageConfiguration(
  41. files ?? this.files,
  42. asyncLoader ?? this.asyncLoader,
  43. selectedIndex ?? this.selectedIndex,
  44. tagPrefix ?? this.tagPrefix,
  45. );
  46. }
  47. }
  48. class DetailPage extends StatefulWidget {
  49. final DetailPageConfiguration config;
  50. const DetailPage(this.config, {key}) : super(key: key);
  51. @override
  52. State<DetailPage> createState() => _DetailPageState();
  53. }
  54. class _DetailPageState extends State<DetailPage> {
  55. static const kLoadLimit = 100;
  56. final _logger = Logger("DetailPageState");
  57. bool _shouldDisableScroll = false;
  58. List<File> _files;
  59. PageController _pageController;
  60. int _selectedIndex = 0;
  61. bool _hasPageChanged = false;
  62. bool _hasLoadedTillStart = false;
  63. bool _hasLoadedTillEnd = false;
  64. bool _shouldHideAppBar = false;
  65. GlobalKey<FadingAppBarState> _appBarKey;
  66. GlobalKey<FadingBottomBarState> _bottomBarKey;
  67. @override
  68. void initState() {
  69. _files = [
  70. ...widget.config.files
  71. ]; // Make a copy since we append preceding and succeeding entries to this
  72. _selectedIndex = widget.config.selectedIndex;
  73. _preloadEntries();
  74. super.initState();
  75. }
  76. @override
  77. void dispose() {
  78. SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
  79. super.dispose();
  80. }
  81. @override
  82. Widget build(BuildContext context) {
  83. _logger.info(
  84. "Opening " +
  85. _files[_selectedIndex].toString() +
  86. ". " +
  87. (_selectedIndex + 1).toString() +
  88. " / " +
  89. _files.length.toString() +
  90. " files .",
  91. );
  92. _appBarKey = GlobalKey<FadingAppBarState>();
  93. _bottomBarKey = GlobalKey<FadingBottomBarState>();
  94. return Scaffold(
  95. appBar: FadingAppBar(
  96. _files[_selectedIndex],
  97. _onFileDeleted,
  98. Configuration.instance.getUserID(),
  99. 100,
  100. widget.config.mode == DetailPageMode.full,
  101. key: _appBarKey,
  102. ),
  103. extendBodyBehindAppBar: true,
  104. body: Center(
  105. child: Stack(
  106. children: [
  107. _buildPageView(),
  108. FadingBottomBar(
  109. _files[_selectedIndex],
  110. _onEditFileRequested,
  111. widget.config.mode == DetailPageMode.minimalistic,
  112. key: _bottomBarKey,
  113. ),
  114. ],
  115. ),
  116. ),
  117. // backgroundColor: Theme.of(context).colorScheme.onPrimary,
  118. );
  119. }
  120. Widget _buildPageView() {
  121. _logger.info("Building with " + _selectedIndex.toString());
  122. _pageController = PageController(initialPage: _selectedIndex);
  123. return PageView.builder(
  124. itemBuilder: (context, index) {
  125. final file = _files[index];
  126. Widget content = FileWidget(
  127. file,
  128. autoPlay: !_hasPageChanged,
  129. tagPrefix: widget.config.tagPrefix,
  130. shouldDisableScroll: (value) {
  131. if (_shouldDisableScroll != value) {
  132. setState(() {
  133. _shouldDisableScroll = value;
  134. });
  135. }
  136. },
  137. playbackCallback: (isPlaying) {
  138. _shouldHideAppBar = isPlaying;
  139. Future.delayed(Duration.zero, () {
  140. _toggleFullScreen();
  141. });
  142. },
  143. backgroundDecoration: const BoxDecoration(color: Colors.black),
  144. );
  145. _preloadFiles(index);
  146. return GestureDetector(
  147. onTap: () {
  148. _shouldHideAppBar = !_shouldHideAppBar;
  149. _toggleFullScreen();
  150. },
  151. child: content,
  152. );
  153. },
  154. onPageChanged: (index) {
  155. setState(() {
  156. _selectedIndex = index;
  157. _hasPageChanged = true;
  158. });
  159. _preloadEntries();
  160. _preloadFiles(index);
  161. },
  162. physics: _shouldDisableScroll
  163. ? const NeverScrollableScrollPhysics()
  164. : const PageScrollPhysics(),
  165. controller: _pageController,
  166. itemCount: _files.length,
  167. );
  168. }
  169. void _toggleFullScreen() {
  170. if (_shouldHideAppBar) {
  171. _appBarKey.currentState.hide();
  172. _bottomBarKey.currentState.hide();
  173. } else {
  174. _appBarKey.currentState.show();
  175. _bottomBarKey.currentState.show();
  176. }
  177. Future.delayed(Duration.zero, () {
  178. SystemChrome.setEnabledSystemUIOverlays(
  179. //to hide status bar?
  180. _shouldHideAppBar ? [] : SystemUiOverlay.values,
  181. );
  182. });
  183. }
  184. void _preloadEntries() async {
  185. if (widget.config.asyncLoader == null) {
  186. return;
  187. }
  188. if (_selectedIndex == 0 && !_hasLoadedTillStart) {
  189. final result = await widget.config.asyncLoader(
  190. _files[_selectedIndex].creationTime + 1,
  191. DateTime.now().microsecondsSinceEpoch,
  192. limit: kLoadLimit,
  193. asc: true,
  194. );
  195. setState(() {
  196. // Returned result could be a subtype of File
  197. // ignore: unnecessary_cast
  198. final files = result.files.reversed.map((e) => e as File).toList();
  199. if (!result.hasMore) {
  200. _hasLoadedTillStart = true;
  201. }
  202. final length = files.length;
  203. files.addAll(_files);
  204. _files = files;
  205. _pageController.jumpToPage(length);
  206. _selectedIndex = length;
  207. });
  208. }
  209. if (_selectedIndex == _files.length - 1 && !_hasLoadedTillEnd) {
  210. final result = await widget.config.asyncLoader(
  211. kGalleryLoadStartTime,
  212. _files[_selectedIndex].creationTime - 1,
  213. limit: kLoadLimit,
  214. );
  215. setState(() {
  216. if (!result.hasMore) {
  217. _hasLoadedTillEnd = true;
  218. }
  219. _files.addAll(result.files);
  220. });
  221. }
  222. }
  223. void _preloadFiles(int index) {
  224. if (index > 0) {
  225. preloadFile(_files[index - 1]);
  226. }
  227. if (index < _files.length - 1) {
  228. preloadFile(_files[index + 1]);
  229. }
  230. }
  231. Future<void> _onFileDeleted(File file) async {
  232. final totalFiles = _files.length;
  233. if (totalFiles == 1) {
  234. // Deleted the only file
  235. Navigator.of(context).pop(); // Close pageview
  236. return;
  237. }
  238. if (_selectedIndex == totalFiles - 1) {
  239. // Deleted the last file
  240. await _pageController.previousPage(
  241. duration: const Duration(milliseconds: 200),
  242. curve: Curves.easeInOut,
  243. );
  244. setState(() {
  245. _files.remove(file);
  246. });
  247. } else {
  248. await _pageController.nextPage(
  249. duration: const Duration(milliseconds: 200),
  250. curve: Curves.easeInOut,
  251. );
  252. setState(() {
  253. _selectedIndex--;
  254. _files.remove(file);
  255. });
  256. }
  257. }
  258. Future<void> _onEditFileRequested(File file) async {
  259. if (file.uploadedFileID != null &&
  260. file.ownerID != Configuration.instance.getUserID()) {
  261. _logger.severe(
  262. "Attempt to edit unowned file",
  263. UnauthorizedEditError(),
  264. StackTrace.current,
  265. );
  266. showErrorDialog(
  267. context,
  268. "Sorry",
  269. "We don't support editing photos and albums that you don't own yet",
  270. );
  271. return;
  272. }
  273. final dialog = createProgressDialog(context, "Please wait...");
  274. await dialog.show();
  275. final imageProvider =
  276. ExtendedFileImageProvider(await getFile(file), cacheRawData: true);
  277. await precacheImage(imageProvider, context);
  278. await dialog.hide();
  279. replacePage(
  280. context,
  281. ImageEditorPage(
  282. imageProvider,
  283. file,
  284. widget.config.copyWith(
  285. files: _files,
  286. selectedIndex: _selectedIndex,
  287. ),
  288. ),
  289. );
  290. }
  291. }