detail_page.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/generated/l10n.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:photos/utils/toast_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. resizeToAvoidBottomInset: false,
  110. body: Center(
  111. child: Stack(
  112. children: [
  113. _buildPageView(),
  114. FadingBottomBar(
  115. _files![_selectedIndex],
  116. _onEditFileRequested,
  117. widget.config.mode == DetailPageMode.minimalistic,
  118. onFileRemoved: _onFileRemoved,
  119. userID: Configuration.instance.getUserID(),
  120. key: _bottomBarKey,
  121. ),
  122. ],
  123. ),
  124. ),
  125. // backgroundColor: Theme.of(context).colorScheme.onPrimary,
  126. );
  127. }
  128. Widget _buildPageView() {
  129. _logger.info("Building with " + _selectedIndex.toString());
  130. _pageController = PageController(initialPage: _selectedIndex);
  131. return PageView.builder(
  132. itemBuilder: (context, index) {
  133. final file = _files![index];
  134. final Widget content = FileWidget(
  135. file,
  136. autoPlay: !_hasPageChanged,
  137. tagPrefix: widget.config.tagPrefix,
  138. shouldDisableScroll: (value) {
  139. if (_shouldDisableScroll != value) {
  140. setState(() {
  141. _shouldDisableScroll = value;
  142. });
  143. }
  144. },
  145. playbackCallback: (isPlaying) {
  146. _shouldHideAppBar = isPlaying;
  147. Future.delayed(Duration.zero, () {
  148. _toggleFullScreen();
  149. });
  150. },
  151. backgroundDecoration: const BoxDecoration(color: Colors.black),
  152. );
  153. _preloadFiles(index);
  154. return GestureDetector(
  155. onTap: () {
  156. _shouldHideAppBar = !_shouldHideAppBar;
  157. _toggleFullScreen();
  158. },
  159. child: content,
  160. );
  161. },
  162. onPageChanged: (index) {
  163. setState(() {
  164. _selectedIndex = index;
  165. _hasPageChanged = true;
  166. });
  167. _preloadEntries();
  168. _preloadFiles(index);
  169. },
  170. physics: _shouldDisableScroll
  171. ? const NeverScrollableScrollPhysics()
  172. : const PageScrollPhysics(),
  173. controller: _pageController,
  174. itemCount: _files!.length,
  175. );
  176. }
  177. void _toggleFullScreen() {
  178. if (_shouldHideAppBar) {
  179. _appBarKey!.currentState!.hide();
  180. _bottomBarKey!.currentState!.hide();
  181. } else {
  182. _appBarKey!.currentState!.show();
  183. _bottomBarKey!.currentState!.show();
  184. }
  185. Future.delayed(Duration.zero, () {
  186. SystemChrome.setEnabledSystemUIMode(
  187. //to hide status bar?
  188. SystemUiMode.manual,
  189. overlays: _shouldHideAppBar ? [] : SystemUiOverlay.values,
  190. );
  191. });
  192. }
  193. void _preloadEntries() async {
  194. if (widget.config.asyncLoader == null) {
  195. return;
  196. }
  197. if (_selectedIndex == 0 && !_hasLoadedTillStart) {
  198. final result = await widget.config.asyncLoader!(
  199. _files![_selectedIndex].creationTime! + 1,
  200. DateTime.now().microsecondsSinceEpoch,
  201. limit: kLoadLimit,
  202. asc: true,
  203. );
  204. setState(() {
  205. // Returned result could be a subtype of File
  206. // ignore: unnecessary_cast
  207. final files = result.files.reversed.map((e) => e as File).toList();
  208. if (!result.hasMore) {
  209. _hasLoadedTillStart = true;
  210. }
  211. final length = files.length;
  212. files.addAll(_files!);
  213. _files = files;
  214. _pageController!.jumpToPage(length);
  215. _selectedIndex = length;
  216. });
  217. }
  218. if (_selectedIndex == _files!.length - 1 && !_hasLoadedTillEnd) {
  219. final result = await widget.config.asyncLoader!(
  220. galleryLoadStartTime,
  221. _files![_selectedIndex].creationTime! - 1,
  222. limit: kLoadLimit,
  223. );
  224. setState(() {
  225. if (!result.hasMore) {
  226. _hasLoadedTillEnd = true;
  227. }
  228. _files!.addAll(result.files);
  229. });
  230. }
  231. }
  232. void _preloadFiles(int index) {
  233. if (index > 0) {
  234. preloadFile(_files![index - 1]);
  235. }
  236. if (index < _files!.length - 1) {
  237. preloadFile(_files![index + 1]);
  238. }
  239. }
  240. Future<void> _onFileRemoved(File file) async {
  241. final totalFiles = _files!.length;
  242. if (totalFiles == 1) {
  243. // Deleted the only file
  244. Navigator.of(context).pop(); // Close pageview
  245. return;
  246. }
  247. if (_selectedIndex == totalFiles - 1) {
  248. // Deleted the last file
  249. await _pageController!.previousPage(
  250. duration: const Duration(milliseconds: 200),
  251. curve: Curves.easeInOut,
  252. );
  253. setState(() {
  254. _files!.remove(file);
  255. });
  256. } else {
  257. await _pageController!.nextPage(
  258. duration: const Duration(milliseconds: 200),
  259. curve: Curves.easeInOut,
  260. );
  261. setState(() {
  262. _selectedIndex--;
  263. _files!.remove(file);
  264. });
  265. }
  266. }
  267. Future<void> _onEditFileRequested(File file) async {
  268. if (file.uploadedFileID != null &&
  269. file.ownerID != Configuration.instance.getUserID()) {
  270. _logger.severe(
  271. "Attempt to edit unowned file",
  272. UnauthorizedEditError(),
  273. StackTrace.current,
  274. );
  275. showErrorDialog(
  276. context,
  277. S.of(context).sorry,
  278. S.of(context).weDontSupportEditingPhotosAndAlbumsThatYouDont,
  279. );
  280. return;
  281. }
  282. final dialog = createProgressDialog(context, S.of(context).pleaseWait);
  283. await dialog.show();
  284. try {
  285. final ioFile = await getFile(file);
  286. if (ioFile == null) {
  287. showShortToast(context, S.of(context).failedToFetchOriginalForEdit);
  288. await dialog.hide();
  289. return;
  290. }
  291. final imageProvider =
  292. ExtendedFileImageProvider(ioFile, cacheRawData: true);
  293. await precacheImage(imageProvider, context);
  294. await dialog.hide();
  295. replacePage(
  296. context,
  297. ImageEditorPage(
  298. imageProvider,
  299. file,
  300. widget.config.copyWith(
  301. files: _files,
  302. selectedIndex: _selectedIndex,
  303. ),
  304. ),
  305. );
  306. } catch (e) {
  307. await dialog.hide();
  308. _logger.warning("Failed to initiate edit", e);
  309. }
  310. }
  311. }