detail_page.dart 9.1 KB

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