detail_page.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. onFileRemoved: _onFileRemoved,
  117. userID: Configuration.instance.getUserID(),
  118. key: _bottomBarKey,
  119. ),
  120. ],
  121. ),
  122. ),
  123. // backgroundColor: Theme.of(context).colorScheme.onPrimary,
  124. );
  125. }
  126. Widget _buildPageView() {
  127. _logger.info("Building with " + _selectedIndex.toString());
  128. _pageController = PageController(initialPage: _selectedIndex);
  129. return PageView.builder(
  130. itemBuilder: (context, index) {
  131. final file = _files![index];
  132. final Widget content = FileWidget(
  133. file,
  134. autoPlay: !_hasPageChanged,
  135. tagPrefix: widget.config.tagPrefix,
  136. shouldDisableScroll: (value) {
  137. if (_shouldDisableScroll != value) {
  138. setState(() {
  139. _shouldDisableScroll = value;
  140. });
  141. }
  142. },
  143. playbackCallback: (isPlaying) {
  144. _shouldHideAppBar = 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. galleryLoadStartTime,
  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. Future<void> _onFileRemoved(File file) async {
  239. final totalFiles = _files!.length;
  240. if (totalFiles == 1) {
  241. // Deleted the only file
  242. Navigator.of(context).pop(); // Close pageview
  243. return;
  244. }
  245. if (_selectedIndex == totalFiles - 1) {
  246. // Deleted the last file
  247. await _pageController!.previousPage(
  248. duration: const Duration(milliseconds: 200),
  249. curve: Curves.easeInOut,
  250. );
  251. setState(() {
  252. _files!.remove(file);
  253. });
  254. } else {
  255. await _pageController!.nextPage(
  256. duration: const Duration(milliseconds: 200),
  257. curve: Curves.easeInOut,
  258. );
  259. setState(() {
  260. _selectedIndex--;
  261. _files!.remove(file);
  262. });
  263. }
  264. }
  265. Future<void> _onEditFileRequested(File file) async {
  266. if (file.uploadedFileID != null &&
  267. file.ownerID != Configuration.instance.getUserID()) {
  268. _logger.severe(
  269. "Attempt to edit unowned file",
  270. UnauthorizedEditError(),
  271. StackTrace.current,
  272. );
  273. showErrorDialog(
  274. context,
  275. "Sorry",
  276. "We don't support editing photos and albums that you don't own yet",
  277. );
  278. return;
  279. }
  280. final dialog = createProgressDialog(context, "Please wait...");
  281. await dialog.show();
  282. try {
  283. final ioFile = await getFile(file);
  284. if (ioFile == null) {
  285. showShortToast(context, "Failed to fetch original for edit");
  286. await dialog.hide();
  287. return;
  288. }
  289. final imageProvider =
  290. ExtendedFileImageProvider(ioFile, cacheRawData: true);
  291. await precacheImage(imageProvider, context);
  292. await dialog.hide();
  293. replacePage(
  294. context,
  295. ImageEditorPage(
  296. imageProvider,
  297. file,
  298. widget.config.copyWith(
  299. files: _files,
  300. selectedIndex: _selectedIndex,
  301. ),
  302. ),
  303. );
  304. } catch (e) {
  305. await dialog.hide();
  306. _logger.warning("Failed to initiate edit", e);
  307. }
  308. }
  309. }