π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Skip gallery layout at invalid width
ashilkn15a91609d576
diff --git a/mobile/apps/photos/changes/gallery-layout-recovery.md b/mobile/apps/photos/changes/gallery-layout-recovery.mdnew file mode 100644index 0000000000..dec2e9ac30--- /dev/null+++ b/mobile/apps/photos/changes/gallery-layout-recovery.md@@ -0,0 +1 @@+- Avoided gallery layout errors when screen dimensions are temporarily unavailable.diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart b/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dartindex b617b401e3..33c476b251 100644--- a/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart+++ b/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart@@ -386,11 +386,17 @@ class GalleryState extends State<Gallery> {void _updateGalleryGroups({bool callSetState = true}) {if (groupHeaderExtent == null) return;+ final widthAvailable = MediaQuery.sizeOf(context).width;+ // View metrics can be zero during background startup. Skip invalid layouts+ // without replacing an already displayed gallery.+ if (!widthAvailable.isFinite || widthAvailable <= 0) {+ return;+ }final groups = GalleryGroups(allFiles: _allGalleryFiles,groupType: _groupType,sortOrderAsc: _sortOrderAsc,- widthAvailable: MediaQuery.sizeOf(context).width,+ widthAvailable: widthAvailable,selectedFiles: widget.selectedFiles,tagPrefix: widget.tagPrefix,groupHeaderExtent: groupHeaderExtent!,@@ -1061,7 +1067,7 @@ class GalleryState extends State<Gallery> {disableSelection: widget.disableSelection,type: _groupType,galleryType: widget.galleryType,- child: _allGalleryFiles.isEmpty+ child: groups.allFiles.isEmpty? _GalleryAppBarScrollBody(appBar: widget.appBar,physics: _scrollPhysics,diff --git a/mobile/apps/photos/test/ui/viewer/gallery/gallery_layout_changed_event_test.dart b/mobile/apps/photos/test/ui/viewer/gallery/gallery_layout_changed_event_test.dartindex 7a24d8b70b..8fd57bd983 100644--- a/mobile/apps/photos/test/ui/viewer/gallery/gallery_layout_changed_event_test.dart+++ b/mobile/apps/photos/test/ui/viewer/gallery/gallery_layout_changed_event_test.dart@@ -9,6 +9,7 @@ import "package:package_info_plus/package_info_plus.dart";import "package:photos/core/configuration.dart";import "package:photos/core/event_bus.dart";import "package:photos/ente_theme_data.dart";+import "package:photos/events/event.dart";import "package:photos/events/gallery_layout_changed_event.dart";import "package:photos/models/file/dummy_file.dart";import "package:photos/models/file/file.dart";@@ -23,6 +24,7 @@ import "package:photos/ui/viewer/gallery/component/group/group_header_widget.darimport "package:photos/ui/viewer/gallery/component/group/type.dart";import "package:photos/ui/viewer/gallery/gallery.dart";import "package:photos/ui/viewer/gallery/gallery_app_bar_config.dart";+import "package:photos/ui/viewer/gallery/gallery_grid_zoom.dart";import "package:photos/ui/viewer/gallery/layout_settings.dart";import "package:photos/ui/viewer/gallery/state/gallery_boundaries_provider.dart";import "package:photos/ui/viewer/gallery/state/gallery_files_inherited_widget.dart";@@ -58,6 +60,182 @@ void main() {await localSettings.setGalleryLayoutType(GalleryLayoutType.grid);});+ testWidgets(+ "zero-width startup keeps loading and lays out all files on recovery",+ (tester) async {+ for (final layout in [+ GalleryLayoutType.grid,+ GalleryLayoutType.justified,+ ]) {+ await localSettings.setGalleryLayoutType(layout);+ final width = ValueNotifier<double>(0);+ final galleryKey = GlobalKey<GalleryState>();+ final files = List<EnteFile>.generate(200, _justifiedTestFile);+ final firstPage = Completer<FileLoadResult>();+ final loadLimits = <int?>[];+ await tester.pumpWidget(+ _resizableGalleryHost(+ width,+ Gallery(+ key: galleryKey,+ tagPrefix: "zero_width_startup",+ groupType: GroupType.month,+ loadingWidget: const SizedBox.shrink(),+ asyncLoader: (start, end, {limit, asc}) {+ loadLimits.add(limit);+ return loadLimits.length == 1+ ? firstPage.future+ : Future.value(FileLoadResult(files, false));+ },+ ),+ ),+ );+ await tester.pumpAndSettle();++ firstPage.complete(FileLoadResult(files.take(100).toList(), true));+ await tester.pumpAndSettle();+ await tester.pump(const Duration(seconds: 1));+ expect(tester.takeException(), isNull);+ expect(loadLimits, [100, null]);+ expect(galleryKey.currentState!.galleryGroups, isNull);+ expect(find.byType(GalleryGridZoom), findsNothing);++ width.value = 400;+ await tester.pumpAndSettle();+ final groups = galleryKey.currentState!.galleryGroups!;+ expect(groups.widthAvailable, 400);+ expect(groups.allFiles, orderedEquals(files));+ expect(groups.getGeometryOfFile(files.last), isNotNull);+ expect(loadLimits, [100, null]);+ expect(find.byType(GalleryGridZoom), findsOneWidget);+ expect(tester.takeException(), isNull);++ await tester.pumpWidget(const SizedBox.shrink());+ await tester.pump(const Duration(milliseconds: 100));+ width.dispose();+ }+ },+ );++ testWidgets("zero-width refresh keeps the existing gallery mounted", (+ tester,+ ) async {+ await localSettings.setGalleryLayoutType(GalleryLayoutType.justified);+ final width = ValueNotifier<double>(400);+ final galleryKey = GlobalKey<GalleryState>();+ final reload = StreamController<Event>.broadcast();+ var files = List<EnteFile>.generate(12, _justifiedTestFile);+ var loads = 0;+ await tester.pumpWidget(+ _resizableGalleryHost(+ width,+ Gallery(+ key: galleryKey,+ tagPrefix: "zero_width_refresh",+ reloadDebounceTime: Duration.zero,+ enableFileGrouping: false,+ forceReloadEvents: [reload.stream],+ asyncLoader: (start, end, {limit, asc}) async {+ loads++;+ return FileLoadResult(files, false);+ },+ ),+ ),+ );+ await tester.pumpAndSettle();+ final originalGroups = galleryKey.currentState!.galleryGroups!;+ expect(originalGroups.allFiles, hasLength(12));+ final scrollableFinder = find.descendant(+ of: find.byType(CustomScrollView),+ matching: find.byType(Scrollable),+ );+ final scrollable = tester.state<ScrollableState>(scrollableFinder);+ scrollable.position.jumpTo(150);+ await tester.pumpAndSettle();+ expect(+ tester.state<ScrollableState>(scrollableFinder).position.pixels,+ 150,+ );++ width.value = 0;+ await tester.pump();+ files = List<EnteFile>.generate(24, _justifiedTestFile);+ reload.add(Event());+ await tester.pumpAndSettle();+ expect(loads, 2);+ expect(galleryKey.currentState!.galleryGroups, same(originalGroups));+ expect(tester.state<ScrollableState>(scrollableFinder), same(scrollable));+ expect(tester.takeException(), isNull);++ width.value = 400;+ await tester.pumpAndSettle();+ reload.add(Event());+ await tester.pumpAndSettle();+ final groups = galleryKey.currentState!.galleryGroups!;+ expect(groups.widthAvailable, originalGroups.widthAvailable);+ expect(groups.allFiles, orderedEquals(files));+ expect(groups.getGeometryOfFile(files.last), isNotNull);+ expect(loads, 3);+ expect(+ tester.state<ScrollableState>(scrollableFinder).position.pixels,+ 150,+ );+ expect(tester.state<ScrollableState>(scrollableFinder), same(scrollable));+ expect(tester.takeException(), isNull);++ await tester.pumpWidget(const SizedBox.shrink());+ await tester.pump(const Duration(milliseconds: 100));+ await reload.close();+ width.dispose();+ });++ testWidgets("zero-width refresh retains the displayed empty state", (+ tester,+ ) async {+ final width = ValueNotifier<double>(400);+ final galleryKey = GlobalKey<GalleryState>();+ final reload = StreamController<Event>.broadcast();+ var files = <EnteFile>[];+ const emptyStateKey = ValueKey("zero_width_empty_state");+ await tester.pumpWidget(+ _resizableGalleryHost(+ width,+ Gallery(+ key: galleryKey,+ tagPrefix: "zero_width_empty_refresh",+ groupType: GroupType.month,+ emptyState: const SizedBox(key: emptyStateKey),+ forceReloadEvents: [reload.stream],+ asyncLoader: (start, end, {limit, asc}) async =>+ FileLoadResult(files, false),+ ),+ ),+ );+ await tester.pumpAndSettle();+ final originalGroups = galleryKey.currentState!.galleryGroups!;+ expect(originalGroups.allFiles, isEmpty);+ expect(find.byKey(emptyStateKey), findsOneWidget);++ width.value = 0;+ await tester.pump();+ files = List<EnteFile>.generate(12, _justifiedTestFile);+ reload.add(Event());+ await tester.pumpAndSettle();+ width.value = 400;+ await tester.pumpAndSettle();+ await tester.pump(const Duration(seconds: 1));+ await tester.pump(const Duration(seconds: 1));+ expect(tester.takeException(), isNull);+ expect(galleryKey.currentState!.galleryGroups, same(originalGroups));+ expect(find.byKey(emptyStateKey), findsOneWidget);+ expect(find.byType(PinnedGroupHeader), findsNothing);++ await tester.pumpWidget(const SizedBox.shrink());+ await tester.pump(const Duration(milliseconds: 100));+ await reload.close();+ width.dispose();+ });+testWidgets("pinching selection galleries changes density without changing selection",(tester) async {@@ -667,6 +845,25 @@ Widget _galleryHost(Gallery gallery) {return GalleryBoundariesProvider(child: GalleryFilesState(child: gallery));}+Widget _resizableGalleryHost(ValueNotifier<double> width, Gallery gallery) {+ return MaterialApp(+ theme: lightThemeData,+ localizationsDelegates: StringsLocalizations.localizationsDelegates,+ supportedLocales: StringsLocalizations.supportedLocales,+ home: ValueListenableBuilder<double>(+ valueListenable: width,+ child: _galleryHost(gallery),+ builder: (context, value, child) => MediaQuery(+ data: MediaQuery.of(context).copyWith(size: Size(value, 600)),+ child: Align(+ alignment: Alignment.topLeft,+ child: SizedBox(width: value, height: 600, child: child),+ ),+ ),+ ),+ );+}+Future<void> _mountPinchGallery(WidgetTester tester, Gallery gallery) async {await tester.pumpWidget(MaterialApp(