detail_page.dart 8.6 KB

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