detail_page.dart 9.0 KB

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