detail_page.dart 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/tools/editor/image_editor_page.dart';
  11. import 'package:photos/ui/viewer/file/fading_app_bar.dart';
  12. import 'package:photos/ui/viewer/file/fading_bottom_bar.dart';
  13. import 'package:photos/ui/viewer/file/file_widget.dart';
  14. import 'package:photos/ui/viewer/gallery/gallery.dart';
  15. import 'package:photos/utils/dialog_util.dart';
  16. import 'package:photos/utils/file_util.dart';
  17. import 'package:photos/utils/navigation_util.dart';
  18. import 'package:wakelock/wakelock.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. bool wakeLockEnabledHere;
  70. @override
  71. void initState() {
  72. wakeLockEnabledHere = false;
  73. _files = [
  74. ...widget.config.files
  75. ]; // Make a copy since we append preceding and succeeding entries to this
  76. _selectedIndex = widget.config.selectedIndex;
  77. _preloadEntries();
  78. super.initState();
  79. }
  80. @override
  81. void dispose() {
  82. SystemChrome.setEnabledSystemUIMode(
  83. SystemUiMode.manual,
  84. overlays: SystemUiOverlay.values,
  85. );
  86. if (wakeLockEnabledHere) {
  87. Wakelock.enabled.then((isEnabled) {
  88. isEnabled ? Wakelock.disable() : null;
  89. });
  90. }
  91. super.dispose();
  92. }
  93. @override
  94. Widget build(BuildContext context) {
  95. _logger.info(
  96. "Opening " +
  97. _files[_selectedIndex].toString() +
  98. ". " +
  99. (_selectedIndex + 1).toString() +
  100. " / " +
  101. _files.length.toString() +
  102. " files .",
  103. );
  104. _appBarKey = GlobalKey<FadingAppBarState>();
  105. _bottomBarKey = GlobalKey<FadingBottomBarState>();
  106. return Scaffold(
  107. appBar: FadingAppBar(
  108. _files[_selectedIndex],
  109. _onFileDeleted,
  110. Configuration.instance.getUserID(),
  111. 100,
  112. widget.config.mode == DetailPageMode.full,
  113. key: _appBarKey,
  114. ),
  115. extendBodyBehindAppBar: true,
  116. body: Center(
  117. child: Stack(
  118. children: [
  119. _buildPageView(),
  120. FadingBottomBar(
  121. _files[_selectedIndex],
  122. _onEditFileRequested,
  123. widget.config.mode == DetailPageMode.minimalistic,
  124. key: _bottomBarKey,
  125. ),
  126. ],
  127. ),
  128. ),
  129. // backgroundColor: Theme.of(context).colorScheme.onPrimary,
  130. );
  131. }
  132. Widget _buildPageView() {
  133. _logger.info("Building with " + _selectedIndex.toString());
  134. _pageController = PageController(initialPage: _selectedIndex);
  135. return PageView.builder(
  136. itemBuilder: (context, index) {
  137. final file = _files[index];
  138. final Widget content = FileWidget(
  139. file,
  140. autoPlay: !_hasPageChanged,
  141. tagPrefix: widget.config.tagPrefix,
  142. shouldDisableScroll: (value) {
  143. if (_shouldDisableScroll != value) {
  144. setState(() {
  145. _shouldDisableScroll = value;
  146. });
  147. }
  148. },
  149. playbackCallback: (isPlaying) {
  150. _shouldHideAppBar = isPlaying;
  151. _keepScreenAliveOnPlaying(isPlaying);
  152. Future.delayed(Duration.zero, () {
  153. _toggleFullScreen();
  154. });
  155. },
  156. backgroundDecoration: const BoxDecoration(color: Colors.black),
  157. );
  158. _preloadFiles(index);
  159. return GestureDetector(
  160. onTap: () {
  161. _shouldHideAppBar = !_shouldHideAppBar;
  162. _toggleFullScreen();
  163. },
  164. child: content,
  165. );
  166. },
  167. onPageChanged: (index) {
  168. setState(() {
  169. _selectedIndex = index;
  170. _hasPageChanged = true;
  171. });
  172. _preloadEntries();
  173. _preloadFiles(index);
  174. },
  175. physics: _shouldDisableScroll
  176. ? const NeverScrollableScrollPhysics()
  177. : const PageScrollPhysics(),
  178. controller: _pageController,
  179. itemCount: _files.length,
  180. );
  181. }
  182. void _toggleFullScreen() {
  183. if (_shouldHideAppBar) {
  184. _appBarKey.currentState.hide();
  185. _bottomBarKey.currentState.hide();
  186. } else {
  187. _appBarKey.currentState.show();
  188. _bottomBarKey.currentState.show();
  189. }
  190. Future.delayed(Duration.zero, () {
  191. SystemChrome.setEnabledSystemUIMode(
  192. //to hide status bar?
  193. SystemUiMode.manual,
  194. overlays: _shouldHideAppBar ? [] : SystemUiOverlay.values,
  195. );
  196. });
  197. }
  198. void _preloadEntries() async {
  199. if (widget.config.asyncLoader == null) {
  200. return;
  201. }
  202. if (_selectedIndex == 0 && !_hasLoadedTillStart) {
  203. final result = await widget.config.asyncLoader(
  204. _files[_selectedIndex].creationTime + 1,
  205. DateTime.now().microsecondsSinceEpoch,
  206. limit: kLoadLimit,
  207. asc: true,
  208. );
  209. setState(() {
  210. // Returned result could be a subtype of File
  211. // ignore: unnecessary_cast
  212. final files = result.files.reversed.map((e) => e as File).toList();
  213. if (!result.hasMore) {
  214. _hasLoadedTillStart = true;
  215. }
  216. final length = files.length;
  217. files.addAll(_files);
  218. _files = files;
  219. _pageController.jumpToPage(length);
  220. _selectedIndex = length;
  221. });
  222. }
  223. if (_selectedIndex == _files.length - 1 && !_hasLoadedTillEnd) {
  224. final result = await widget.config.asyncLoader(
  225. galleryLoadStartTime,
  226. _files[_selectedIndex].creationTime - 1,
  227. limit: kLoadLimit,
  228. );
  229. setState(() {
  230. if (!result.hasMore) {
  231. _hasLoadedTillEnd = true;
  232. }
  233. _files.addAll(result.files);
  234. });
  235. }
  236. }
  237. void _preloadFiles(int index) {
  238. if (index > 0) {
  239. preloadFile(_files[index - 1]);
  240. }
  241. if (index < _files.length - 1) {
  242. preloadFile(_files[index + 1]);
  243. }
  244. }
  245. void _keepScreenAliveOnPlaying(bool isPlaying) {
  246. if (isPlaying) {
  247. Wakelock.enabled.then((value) {
  248. if (value == false) {
  249. Wakelock.enable();
  250. wakeLockEnabledHere = true;
  251. //wakeLockEnabledHere will not be set to true if wakeLock is already enabled from settings on iOS.
  252. //We shouldn't disable when video is not playing if it was enabled manually by the user from ente settings by user.
  253. }
  254. });
  255. }
  256. if (wakeLockEnabledHere && !isPlaying) {
  257. Wakelock.disable();
  258. }
  259. }
  260. Future<void> _onFileDeleted(File file) async {
  261. final totalFiles = _files.length;
  262. if (totalFiles == 1) {
  263. // Deleted the only file
  264. Navigator.of(context).pop(); // Close pageview
  265. return;
  266. }
  267. if (_selectedIndex == totalFiles - 1) {
  268. // Deleted the last file
  269. await _pageController.previousPage(
  270. duration: const Duration(milliseconds: 200),
  271. curve: Curves.easeInOut,
  272. );
  273. setState(() {
  274. _files.remove(file);
  275. });
  276. } else {
  277. await _pageController.nextPage(
  278. duration: const Duration(milliseconds: 200),
  279. curve: Curves.easeInOut,
  280. );
  281. setState(() {
  282. _selectedIndex--;
  283. _files.remove(file);
  284. });
  285. }
  286. }
  287. Future<void> _onEditFileRequested(File file) async {
  288. if (file.uploadedFileID != null &&
  289. file.ownerID != Configuration.instance.getUserID()) {
  290. _logger.severe(
  291. "Attempt to edit unowned file",
  292. UnauthorizedEditError(),
  293. StackTrace.current,
  294. );
  295. showErrorDialog(
  296. context,
  297. "Sorry",
  298. "We don't support editing photos and albums that you don't own yet",
  299. );
  300. return;
  301. }
  302. final dialog = createProgressDialog(context, "Please wait...");
  303. await dialog.show();
  304. final imageProvider =
  305. ExtendedFileImageProvider(await getFile(file), cacheRawData: true);
  306. await precacheImage(imageProvider, context);
  307. await dialog.hide();
  308. replacePage(
  309. context,
  310. ImageEditorPage(
  311. imageProvider,
  312. file,
  313. widget.config.copyWith(
  314. files: _files,
  315. selectedIndex: _selectedIndex,
  316. ),
  317. ),
  318. );
  319. }
  320. }