ente/lib/ui/detail_page.dart

291 lines
8.2 KiB
Dart
Raw Normal View History

2021-06-02 13:54:31 +00:00
import 'package:extended_image/extended_image.dart';
2020-04-25 22:57:43 +00:00
import 'package:flutter/cupertino.dart';
2020-03-24 19:59:36 +00:00
import 'package:flutter/material.dart';
2021-07-05 18:28:57 +00:00
import 'package:flutter/services.dart';
2021-06-09 13:38:27 +00:00
import 'package:logging/logging.dart';
2021-08-09 13:26:06 +00:00
import 'package:photos/core/configuration.dart';
import 'package:photos/core/constants.dart';
2021-08-09 13:26:06 +00:00
import 'package:photos/core/errors.dart';
2021-06-09 13:38:27 +00:00
import 'package:photos/models/file.dart';
import 'package:photos/ui/fading_app_bar.dart';
import 'package:photos/ui/fading_bottom_bar.dart';
import 'package:photos/ui/file_widget.dart';
import 'package:photos/ui/gallery.dart';
2021-06-02 13:54:31 +00:00
import 'package:photos/ui/image_editor_page.dart';
2020-10-23 15:20:51 +00:00
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/file_util.dart';
2021-06-02 13:54:31 +00:00
import 'package:photos/utils/navigation_util.dart';
2020-03-24 19:59:36 +00:00
2021-09-15 20:40:08 +00:00
enum DetailPageMode {
minimalistic,
full,
}
2021-06-09 13:38:27 +00:00
class DetailPageConfiguration {
2020-06-19 23:03:26 +00:00
final List<File> files;
final GalleryLoader asyncLoader;
final int selectedIndex;
final String tagPrefix;
2021-09-15 20:40:08 +00:00
final DetailPageMode mode;
2020-03-24 19:59:36 +00:00
2021-06-09 13:38:27 +00:00
DetailPageConfiguration(
this.files,
this.asyncLoader,
this.selectedIndex,
2021-09-15 20:40:08 +00:00
this.tagPrefix, {
this.mode = DetailPageMode.full,
});
2021-06-09 13:38:27 +00:00
DetailPageConfiguration copyWith({
List<File> files,
GalleryLoader asyncLoader,
int selectedIndex,
String tagPrefix,
}) {
return DetailPageConfiguration(
files ?? this.files,
asyncLoader ?? this.asyncLoader,
selectedIndex ?? this.selectedIndex,
tagPrefix ?? this.tagPrefix,
);
}
}
class DetailPage extends StatefulWidget {
final DetailPageConfiguration config;
DetailPage(this.config, {key}) : super(key: key);
2020-04-17 08:17:37 +00:00
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
static const kLoadLimit = 100;
2020-05-06 16:43:03 +00:00
final _logger = Logger("DetailPageState");
2020-04-17 08:17:37 +00:00
bool _shouldDisableScroll = false;
2020-06-19 23:03:26 +00:00
List<File> _files;
PageController _pageController;
int _selectedIndex = 0;
bool _hasPageChanged = false;
bool _hasLoadedTillStart = false;
bool _hasLoadedTillEnd = false;
2021-07-05 18:09:44 +00:00
bool _shouldHideAppBar = false;
GlobalKey<FadingAppBarState> _appBarKey;
GlobalKey<FadingBottomBarState> _bottomBarKey;
2020-04-23 20:00:20 +00:00
2020-03-24 19:59:36 +00:00
@override
2020-04-25 22:57:43 +00:00
void initState() {
_files = [
...widget.config.files
]; // Make a copy since we append preceding and succeeding entries to this
2021-06-09 13:38:27 +00:00
_selectedIndex = widget.config.selectedIndex;
2021-07-07 01:10:02 +00:00
_preloadEntries();
2020-04-25 22:57:43 +00:00
super.initState();
}
2020-04-23 20:00:20 +00:00
@override
void dispose() {
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
super.dispose();
}
2020-04-25 22:57:43 +00:00
@override
Widget build(BuildContext context) {
2020-05-06 16:43:03 +00:00
_logger.info("Opening " +
2020-06-19 23:03:26 +00:00
_files[_selectedIndex].toString() +
2020-04-27 15:28:03 +00:00
". " +
(_selectedIndex + 1).toString() +
2020-04-25 22:57:43 +00:00
" / " +
2020-06-19 23:03:26 +00:00
_files.length.toString() +
" files .");
_appBarKey = GlobalKey<FadingAppBarState>();
_bottomBarKey = GlobalKey<FadingBottomBarState>();
2020-03-24 19:59:36 +00:00
return Scaffold(
appBar: FadingAppBar(
_files[_selectedIndex],
_onFileDeleted,
Configuration.instance.getUserID(),
100,
2021-09-15 20:40:08 +00:00
widget.config.mode == DetailPageMode.full,
key: _appBarKey,
),
2020-06-20 21:37:44 +00:00
extendBodyBehindAppBar: true,
2020-03-24 19:59:36 +00:00
body: Center(
2021-08-09 13:26:06 +00:00
child: Stack(
children: [
_buildPageView(),
FadingBottomBar(
_files[_selectedIndex],
_onEditFileRequested,
2021-09-15 20:40:08 +00:00
widget.config.mode == DetailPageMode.minimalistic,
2021-08-09 13:26:06 +00:00
key: _bottomBarKey,
),
],
2020-03-24 19:59:36 +00:00
),
),
backgroundColor: Colors.black,
2020-03-24 19:59:36 +00:00
);
}
2020-04-27 11:57:40 +00:00
Widget _buildPageView() {
_logger.info("Building with " + _selectedIndex.toString());
_pageController = PageController(initialPage: _selectedIndex);
2020-06-17 15:09:47 +00:00
return PageView.builder(
2020-04-25 22:57:43 +00:00
itemBuilder: (context, index) {
2020-06-19 23:03:26 +00:00
final file = _files[index];
Widget content = FileWidget(
file,
autoPlay: !_hasPageChanged,
tagPrefix: widget.config.tagPrefix,
shouldDisableScroll: (value) {
setState(() {
_shouldDisableScroll = value;
});
},
playbackCallback: (isPlaying) {
_shouldHideAppBar = isPlaying;
Future.delayed(Duration.zero, () {
_toggleFullScreen();
});
},
);
2020-06-19 23:03:26 +00:00
_preloadFiles(index);
2021-07-05 17:34:45 +00:00
return GestureDetector(
onTap: () {
_shouldHideAppBar = !_shouldHideAppBar;
2021-07-06 23:24:19 +00:00
_toggleFullScreen();
2021-07-05 17:34:45 +00:00
},
child: content,
);
2020-04-25 22:57:43 +00:00
},
2020-06-17 15:09:47 +00:00
onPageChanged: (index) {
setState(() {
_selectedIndex = index;
_hasPageChanged = true;
});
2021-07-07 01:10:02 +00:00
_preloadEntries();
2020-06-19 23:03:26 +00:00
_preloadFiles(index);
2020-04-25 22:57:43 +00:00
},
physics: _shouldDisableScroll
? NeverScrollableScrollPhysics()
: PageScrollPhysics(),
controller: _pageController,
2020-06-19 23:03:26 +00:00
itemCount: _files.length,
2020-04-25 22:57:43 +00:00
);
}
2021-07-06 23:24:19 +00:00
void _toggleFullScreen() {
if (_shouldHideAppBar) {
_appBarKey.currentState.hide();
_bottomBarKey.currentState.hide();
} else {
_appBarKey.currentState.show();
_bottomBarKey.currentState.show();
}
Future.delayed(Duration.zero, () {
SystemChrome.setEnabledSystemUIOverlays(
_shouldHideAppBar ? [] : SystemUiOverlay.values,
);
});
}
2021-07-07 01:10:02 +00:00
void _preloadEntries() async {
2021-09-15 19:09:55 +00:00
if (widget.config.asyncLoader == null) {
return;
}
2021-07-07 01:10:02 +00:00
if (_selectedIndex == 0 && !_hasLoadedTillStart) {
final result = await widget.config.asyncLoader(
2021-07-07 01:10:02 +00:00
_files[_selectedIndex].creationTime + 1,
DateTime.now().microsecondsSinceEpoch,
limit: kLoadLimit,
asc: true);
setState(() {
final files = result.files.reversed.toList();
if (!result.hasMore) {
_hasLoadedTillStart = true;
}
final length = files.length;
files.addAll(_files);
_files = files;
_pageController.jumpToPage(length);
_selectedIndex = length;
});
}
2021-07-07 01:10:02 +00:00
if (_selectedIndex == _files.length - 1 && !_hasLoadedTillEnd) {
final result = await widget.config.asyncLoader(
kGalleryLoadStartTime, _files[_selectedIndex].creationTime - 1,
2021-07-07 01:10:02 +00:00
limit: kLoadLimit);
setState(() {
if (!result.hasMore) {
_hasLoadedTillEnd = true;
}
_files.addAll(result.files);
});
}
}
2020-06-19 23:03:26 +00:00
void _preloadFiles(int index) {
2020-06-17 15:09:47 +00:00
if (index > 0) {
2020-07-29 15:48:13 +00:00
preloadFile(_files[index - 1]);
2020-06-17 15:09:47 +00:00
}
2020-06-19 23:03:26 +00:00
if (index < _files.length - 1) {
2020-07-29 15:48:13 +00:00
preloadFile(_files[index + 1]);
2020-06-17 15:09:47 +00:00
}
}
Future<void> _onFileDeleted(File file) async {
final totalFiles = _files.length;
if (totalFiles == 1) {
// Deleted the only file
Navigator.of(context, rootNavigator: true).pop(); // Close pageview
Navigator.of(context, rootNavigator: true).pop(); // Close gallery
return;
}
if (_selectedIndex == totalFiles - 1) {
// Deleted the last file
await _pageController.previousPage(
duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
setState(() {
_files.remove(file);
});
} else {
await _pageController.nextPage(
duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
setState(() {
_selectedIndex--;
_files.remove(file);
});
}
Navigator.of(context, rootNavigator: true).pop(); // Close dialog
}
2021-06-12 21:29:04 +00:00
Future<void> _onEditFileRequested(File file) async {
2021-08-09 13:26:06 +00:00
if (file.uploadedFileID != null &&
file.ownerID != Configuration.instance.getUserID()) {
_logger.severe("Attempt to edit unowned file", UnauthorizedEditError(),
StackTrace.current);
showErrorDialog(context, "sorry",
"we don't support editing photos and albums that you don't own yet");
return;
}
final dialog = createProgressDialog(context, "please wait...");
2021-06-12 21:29:04 +00:00
await dialog.show();
final imageProvider =
ExtendedFileImageProvider(await getFile(file), cacheRawData: true);
await precacheImage(imageProvider, context);
2021-06-12 21:29:04 +00:00
await dialog.hide();
replacePage(
context,
ImageEditorPage(
imageProvider,
file,
widget.config.copyWith(
files: _files,
selectedIndex: _selectedIndex,
),
),
2021-07-06 09:57:30 +00:00
);
}
2020-04-23 20:00:20 +00:00
}