detail_page.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. @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. _onFileDeleted,
  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. _keepScreenAliveOnPlaying(isPlaying);
  145. Future.delayed(Duration.zero, () {
  146. _toggleFullScreen();
  147. });
  148. },
  149. backgroundDecoration: const BoxDecoration(color: Colors.black),
  150. );
  151. _preloadFiles(index);
  152. return GestureDetector(
  153. onTap: () {
  154. _shouldHideAppBar = !_shouldHideAppBar;
  155. _toggleFullScreen();
  156. },
  157. child: content,
  158. );
  159. },
  160. onPageChanged: (index) {
  161. setState(() {
  162. _selectedIndex = index;
  163. _hasPageChanged = true;
  164. });
  165. _preloadEntries();
  166. _preloadFiles(index);
  167. },
  168. physics: _shouldDisableScroll
  169. ? const NeverScrollableScrollPhysics()
  170. : const PageScrollPhysics(),
  171. controller: _pageController,
  172. itemCount: _files.length,
  173. );
  174. }
  175. void _toggleFullScreen() {
  176. if (_shouldHideAppBar) {
  177. _appBarKey.currentState.hide();
  178. _bottomBarKey.currentState.hide();
  179. } else {
  180. _appBarKey.currentState.show();
  181. _bottomBarKey.currentState.show();
  182. }
  183. Future.delayed(Duration.zero, () {
  184. SystemChrome.setEnabledSystemUIMode(
  185. //to hide status bar?
  186. SystemUiMode.manual,
  187. overlays: _shouldHideAppBar ? [] : SystemUiOverlay.values,
  188. );
  189. });
  190. }
  191. void _preloadEntries() async {
  192. if (widget.config.asyncLoader == null) {
  193. return;
  194. }
  195. if (_selectedIndex == 0 && !_hasLoadedTillStart) {
  196. final result = await widget.config.asyncLoader(
  197. _files[_selectedIndex].creationTime + 1,
  198. DateTime.now().microsecondsSinceEpoch,
  199. limit: kLoadLimit,
  200. asc: true,
  201. );
  202. setState(() {
  203. // Returned result could be a subtype of File
  204. // ignore: unnecessary_cast
  205. final files = result.files.reversed.map((e) => e as File).toList();
  206. if (!result.hasMore) {
  207. _hasLoadedTillStart = true;
  208. }
  209. final length = files.length;
  210. files.addAll(_files);
  211. _files = files;
  212. _pageController.jumpToPage(length);
  213. _selectedIndex = length;
  214. });
  215. }
  216. if (_selectedIndex == _files.length - 1 && !_hasLoadedTillEnd) {
  217. final result = await widget.config.asyncLoader(
  218. kGalleryLoadStartTime,
  219. _files[_selectedIndex].creationTime - 1,
  220. limit: kLoadLimit,
  221. );
  222. setState(() {
  223. if (!result.hasMore) {
  224. _hasLoadedTillEnd = true;
  225. }
  226. _files.addAll(result.files);
  227. });
  228. }
  229. }
  230. void _preloadFiles(int index) {
  231. if (index > 0) {
  232. preloadFile(_files[index - 1]);
  233. }
  234. if (index < _files.length - 1) {
  235. preloadFile(_files[index + 1]);
  236. }
  237. }
  238. void _keepScreenAliveOnPlaying(bool isPlaying) {
  239. bool wakeLockEnabledHere = false;
  240. if (isPlaying) {
  241. Wakelock.enabled.then((value) {
  242. if (value == false) {
  243. Wakelock.enable();
  244. wakeLockEnabledHere = true;
  245. //wakeLockEnabledHere will not be set to true if wakeLock is already enabled from settings on iOS.
  246. //We shouldn't disable when video is not playing if it was enabled manually by the user from ente settings by user.
  247. }
  248. });
  249. }
  250. if (wakeLockEnabledHere && !isPlaying) {
  251. Wakelock.disable();
  252. }
  253. }
  254. Future<void> _onFileDeleted(File file) async {
  255. final totalFiles = _files.length;
  256. if (totalFiles == 1) {
  257. // Deleted the only file
  258. Navigator.of(context).pop(); // Close pageview
  259. return;
  260. }
  261. if (_selectedIndex == totalFiles - 1) {
  262. // Deleted the last file
  263. await _pageController.previousPage(
  264. duration: const Duration(milliseconds: 200),
  265. curve: Curves.easeInOut,
  266. );
  267. setState(() {
  268. _files.remove(file);
  269. });
  270. } else {
  271. await _pageController.nextPage(
  272. duration: const Duration(milliseconds: 200),
  273. curve: Curves.easeInOut,
  274. );
  275. setState(() {
  276. _selectedIndex--;
  277. _files.remove(file);
  278. });
  279. }
  280. }
  281. Future<void> _onEditFileRequested(File file) async {
  282. if (file.uploadedFileID != null &&
  283. file.ownerID != Configuration.instance.getUserID()) {
  284. _logger.severe(
  285. "Attempt to edit unowned file",
  286. UnauthorizedEditError(),
  287. StackTrace.current,
  288. );
  289. showErrorDialog(
  290. context,
  291. "Sorry",
  292. "We don't support editing photos and albums that you don't own yet",
  293. );
  294. return;
  295. }
  296. final dialog = createProgressDialog(context, "Please wait...");
  297. await dialog.show();
  298. final imageProvider =
  299. ExtendedFileImageProvider(await getFile(file), cacheRawData: true);
  300. await precacheImage(imageProvider, context);
  301. await dialog.hide();
  302. replacePage(
  303. context,
  304. ImageEditorPage(
  305. imageProvider,
  306. file,
  307. widget.config.copyWith(
  308. files: _files,
  309. selectedIndex: _selectedIndex,
  310. ),
  311. ),
  312. );
  313. }
  314. }