Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Reject stale detail page selections

Neeraj Guptacc56f2fc22e2
diff --git a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart
index b1999092f8..502de3dccb 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart
@@ -159,8 +159,9 @@ class _BodyState extends State<_Body> {
super.initState();
_files = widget.config.files;
- _selectedIndexNotifier.value = widget.config.selectedIndex;
- _pageController = PageController(initialPage: _selectedIndexNotifier.value);
+ final selectedIndex = _initialSelectedIndex();
+ _selectedIndexNotifier.value = selectedIndex ?? -1;
+ _pageController = PageController(initialPage: selectedIndex ?? 0);
_guestViewEventSubscription = Bus.instance.on<GuestViewEvent>().listen((
event,
) {
@@ -177,8 +178,13 @@ class _BodyState extends State<_Body> {
// Update shared collection state after first frame
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
- _updateSharedCollectionState(_files![_selectedIndexNotifier.value]);
- _evaluateQrIfEligible(_files![_selectedIndexNotifier.value]);
+ final selectedFile = _selectedFile;
+ if (selectedFile == null) {
+ Navigator.maybePop(context).ignore();
+ return;
+ }
+ _updateSharedCollectionState(selectedFile);
+ _evaluateQrIfEligible(selectedFile);
widget.config.onPageReady?.call(context);
});
}
@@ -200,15 +206,19 @@ class _BodyState extends State<_Body> {
@override
Widget build(BuildContext context) {
- try {
- _files![_selectedIndexNotifier.value];
- } catch (e) {
- _logger.severe(e);
- Navigator.pop(context);
+ final selectedFile = _selectedFile;
+ if (selectedFile == null) {
+ _logger.warning("Closing detail page without a selected file");
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ if (mounted) {
+ Navigator.maybePop(context).ignore();
+ }
+ });
+ return const Scaffold(backgroundColor: Colors.black);
}
_logger.info(
"Opening " +
- _files![_selectedIndexNotifier.value].toString() +
+ selectedFile.toString() +
". " +
(_selectedIndexNotifier.value + 1).toString() +
" / " +
@@ -419,6 +429,10 @@ class _BodyState extends State<_Body> {
);
},
onPageChanged: (index) {
+ final file = _fileAt(index);
+ if (file == null) {
+ return;
+ }
if (_selectedIndexNotifier.value == index) {
if (kDebugMode) {
debugPrint("onPageChanged called with same index $index");
@@ -431,8 +445,8 @@ class _BodyState extends State<_Body> {
_selectedIndexNotifier.value = index;
}
Bus.instance.fire(GuestViewEvent(isGuestView, swipeLocked));
- _updateSharedCollectionState(_files![index]);
- _evaluateQrIfEligible(_files![index]);
+ _updateSharedCollectionState(file);
+ _evaluateQrIfEligible(file);
},
physics: _shouldDisableScroll || swipeLocked
? const NeverScrollableScrollPhysics()
@@ -594,8 +608,30 @@ class _BodyState extends State<_Body> {
// Guard: Only update if still showing the same file
// (user may have swiped to a different file while awaiting)
- if (_files![_selectedIndexNotifier.value].uploadedFileID == fileID) {
+ if (_selectedFile?.uploadedFileID == fileID) {
notifier.value = isShared;
}
}
+
+ int? _initialSelectedIndex() {
+ final files = _files;
+ if (files == null || files.isEmpty) {
+ return null;
+ }
+ final selectedIndex = widget.config.selectedIndex;
+ if (selectedIndex < 0 || selectedIndex >= files.length) {
+ return null;
+ }
+ return selectedIndex;
+ }
+
+ EnteFile? get _selectedFile => _fileAt(_selectedIndexNotifier.value);
+
+ EnteFile? _fileAt(int index) {
+ final files = _files;
+ if (files == null || index < 0 || index >= files.length) {
+ return null;
+ }
+ return files[index];
+ }
}
diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/component/gallery_file_widget.dart b/mobile/apps/photos/lib/ui/viewer/gallery/component/gallery_file_widget.dart
index 512d35bcd4..759369bc41 100644
--- a/mobile/apps/photos/lib/ui/viewer/gallery/component/gallery_file_widget.dart
+++ b/mobile/apps/photos/lib/ui/viewer/gallery/component/gallery_file_widget.dart
@@ -286,6 +286,10 @@ class _GalleryFileWidgetState extends State<GalleryFileWidget> {
void _routeToDetailPage(EnteFile file, BuildContext context) {
final galleryFiles = GalleryFilesState.of(context).galleryFiles;
+ final selectedIndex = galleryFiles.indexOf(file);
+ if (selectedIndex < 0) {
+ return;
+ }
// Device folders (local-only contexts) should keep files visible
// even after deleting from Ente (remote) since they still exist locally
final galleryType = GalleryContextState.of(context)?.galleryType;
@@ -293,7 +297,7 @@ class _GalleryFileWidgetState extends State<GalleryFileWidget> {
final page = DetailPage(
DetailPageConfiguration(
galleryFiles,
- galleryFiles.indexOf(file),
+ selectedIndex,
widget.tag,
isLocalOnlyContext: isLocalOnlyContext,
galleryType: galleryType,