💚 End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Use optimized row search for internal Flex
ashil-pilot0176b6b295fb
diff --git a/mobile/apps/photos/changes/optimize-internal-flex.md b/mobile/apps/photos/changes/optimize-internal-flex.mdnew file mode 100644index 0000000000..edb404c764--- /dev/null+++ b/mobile/apps/photos/changes/optimize-internal-flex.md@@ -0,0 +1 @@+- Improved gallery layout calculation speed for the internal Justified · Flex option, especially on wider screens.diff --git a/mobile/apps/photos/lib/models/gallery/flex_layout.dart b/mobile/apps/photos/lib/models/gallery/flex_layout.dartindex 9e2a233280..169b029f2d 100644--- a/mobile/apps/photos/lib/models/gallery/flex_layout.dart+++ b/mobile/apps/photos/lib/models/gallery/flex_layout.dart@@ -63,11 +63,16 @@ class FlexLayoutCalculator {targetRowHeight * maximumRowHeightFactor,);final preferredTileWidth = targetRowHeight * _preferredTileWidthFactor;+ final maximumCandidateRowLength =+ JustifiedLayoutCalculator.responsiveMaximumItemsPerRowFor(+ availableWidth,+ );for (var start = count - 1; start >= 0; start--) {var ratioSum = 0.0;var minimumRatio = double.infinity;- for (var end = start; end < count; end++) {+ final candidateEnd = math.min(count, start + maximumCandidateRowLength);+ for (var end = start; end < candidateEnd; end++) {ratioSum += ratios[end];minimumRatio = math.min(minimumRatio, ratios[end]);final itemCount = end - start + 1;diff --git a/mobile/apps/photos/lib/models/gallery/justified_layout.dart b/mobile/apps/photos/lib/models/gallery/justified_layout.dartindex b26301e2b1..009c11b095 100644--- a/mobile/apps/photos/lib/models/gallery/justified_layout.dart+++ b/mobile/apps/photos/lib/models/gallery/justified_layout.dart@@ -118,7 +118,7 @@ class JustifiedLayoutCalculator {.toDouble();}- static int _maximumItemsPerRowFor(double availableWidth) {+ static int responsiveMaximumItemsPerRowFor(double availableWidth) {if (availableWidth < _mediumWidthBreakpoint) {return _compactMaximumItemsPerRow;}@@ -171,7 +171,7 @@ class JustifiedLayoutCalculator {final rows = <JustifiedRowLayout>[];final pendingRatios = <double>[];final maximumRowHeight = targetRowHeight * maximumRowHeightFactor;- final maximumItemsPerRow = _maximumItemsPerRowFor(availableWidth);+ final maximumItemsPerRow = responsiveMaximumItemsPerRowFor(availableWidth);// On wider galleries, prefer a partial final row once its configured// growth limit is reached. Compact layouts retain their tail treatment.final limitsFinalRowGrowth = availableWidth >= _mediumWidthBreakpoint;diff --git a/mobile/apps/photos/lib/settings/local_settings.dart b/mobile/apps/photos/lib/settings/local_settings.dartindex 13278acc3b..ce56ac9848 100644--- a/mobile/apps/photos/lib/settings/local_settings.dart+++ b/mobile/apps/photos/lib/settings/local_settings.dart@@ -304,7 +304,9 @@ class LocalSettings {JustifiedLayoutStrategy getJustifiedLayoutStrategy() {return switch (_prefs.getString(kJustifiedLayoutStrategy)) {- "flex" || "flexFullRows" => JustifiedLayoutStrategy.flex,+ "flex" ||+ "flexFullRows" ||+ "flexOptimized" => JustifiedLayoutStrategy.flex,_ => JustifiedLayoutStrategy.comfortLarge,};}diff --git a/mobile/apps/photos/test/models/flex_layout_test.dart b/mobile/apps/photos/test/models/flex_layout_test.dartindex e20ababeac..81ea8b3fcb 100644--- a/mobile/apps/photos/test/models/flex_layout_test.dart+++ b/mobile/apps/photos/test/models/flex_layout_test.dart@@ -57,15 +57,40 @@ void main() {expect(_occupiedWidth(row), closeTo(404, 1e-9));});- test("can exceed Comfort's item cap to avoid a portrait orphan", () {- final rows = _rows(List.filled(4, 9 / 16));-- expect(rows, hasLength(1));- expect(rows.single.itemWidths, hasLength(4));- expect(_occupiedWidth(rows.single), closeTo(402, 1e-9));+ test("bounds candidate rows at the responsive width breakpoints", () {+ for (final (width, targetHeight, expectedMaximum) in [+ (599.9, 320.0, 3),+ (600.0, 320.0, 4),+ (1007.9, 320.0, 4),+ (1008.0, 320.0, 5),+ ]) {+ final atLimitRatio =+ (width - 2 * (expectedMaximum - 1)) /+ (expectedMaximum * targetHeight);+ final itemCount = expectedMaximum + 1;+ final ratio = (width - 2 * (itemCount - 1)) / (itemCount * targetHeight);+ expect(+ _rows(+ List.filled(expectedMaximum, atLimitRatio),+ width: width,+ targetHeight: targetHeight,+ ).single.itemWidths,+ hasLength(expectedMaximum),+ );++ final rows = _rows(+ List.filled(itemCount, ratio),+ width: width,+ targetHeight: targetHeight,+ );+ expect(+ rows.map((row) => row.itemWidths.length),+ everyElement(lessThanOrEqualTo(expectedMaximum)),+ );+ }});- test("avoids cramped landscape rows without imposing a count cap", () {+ test("avoids cramped landscape rows", () {const ratios = [0.75, 0.75, 2.0, 2.0, 2.0, 2.0, 2.0, 0.4, 0.4, 0.4];final compactRows = _rows(ratios, targetHeight: 224);diff --git a/mobile/apps/photos/test/models/gallery_groups_justified_test.dart b/mobile/apps/photos/test/models/gallery_groups_justified_test.dartindex e7d7bac2ae..b4ba15ae1a 100644--- a/mobile/apps/photos/test/models/gallery_groups_justified_test.dart+++ b/mobile/apps/photos/test/models/gallery_groups_justified_test.dart@@ -167,7 +167,8 @@ void main() {1,);final flex = groups().groupLayouts.single as JustifiedSectionLayout;- expect(flex.rows.single.itemWidths, hasLength(4));+ expect(flex.rows.map((row) => row.itemWidths.length), [3, 1]);+ _expectRowsCoverFiles(flex, files.length);});test("routes Flex with its non-final singleton tuning", () async {diff --git a/mobile/apps/photos/test/settings/local_settings_gallery_layout_test.dart b/mobile/apps/photos/test/settings/local_settings_gallery_layout_test.dartindex 03b8887b37..38f52a2a01 100644--- a/mobile/apps/photos/test/settings/local_settings_gallery_layout_test.dart+++ b/mobile/apps/photos/test/settings/local_settings_gallery_layout_test.dart@@ -46,13 +46,18 @@ void main() {},);- test("renamed Flex reads the former Full Rows selection", () async {- SharedPreferences.setMockInitialValues({- LocalSettings.kJustifiedLayoutStrategy: "flexFullRows",- });- final settings = LocalSettings(await SharedPreferences.getInstance());+ test("Flex reads former Full Rows and optimized selections", () async {+ for (final storedStrategy in ["flexFullRows", "flexOptimized"]) {+ SharedPreferences.setMockInitialValues({+ LocalSettings.kJustifiedLayoutStrategy: storedStrategy,+ });+ final settings = LocalSettings(await SharedPreferences.getInstance());- expect(settings.getJustifiedLayoutStrategy(), JustifiedLayoutStrategy.flex);+ expect(+ settings.getJustifiedLayoutStrategy(),+ JustifiedLayoutStrategy.flex,+ );+ }});group("justified layout tuning", () {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 8d31c94257..d5a3a3630f 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@@ -12,6 +12,7 @@ import "package:photos/models/file/dummy_file.dart";import "package:photos/models/file/file.dart";import "package:photos/models/file/file_type.dart";import "package:photos/models/file_load_result.dart";+import "package:photos/models/gallery/gallery_layout_config.dart";import "package:photos/models/gallery/justified_layout_strategy.dart";import "package:photos/models/gallery/justified_layout_tuning.dart";import "package:photos/models/metadata/file_magic.dart";@@ -71,6 +72,38 @@ void main() {await localSettings.setInternalUserDisabled(false);});+ testWidgets("Flex remains unavailable when internal features are disabled", (+ tester,+ ) async {+ await localSettings.setGalleryLayoutType(GalleryLayoutType.justified);+ await localSettings.setJustifiedLayoutStrategy(+ JustifiedLayoutStrategy.flex,+ );+ await localSettings.setInternalUserDisabled(true);++ expect(isJustifiedLayoutAvailable, isFalse);+ expect(+ resolveGalleryLayoutType(localSettings.getGalleryLayoutType()),+ GalleryLayoutType.grid,+ );+ for (final settings in [+ const Scaffold(body: GalleryLayoutSettings()),+ const GallerySettingsScreen(fromGalleryLayoutSettingsCTA: true),+ ]) {+ await tester.pumpWidget(+ MaterialApp(+ theme: lightThemeData,+ localizationsDelegates: StringsLocalizations.localizationsDelegates,+ supportedLocales: StringsLocalizations.supportedLocales,+ home: settings,+ ),+ );+ await tester.pumpAndSettle();+ expect(find.text("Justified · Flex (i)"), findsNothing);+ expect(find.text("Layout (i)"), findsNothing);+ }+ });+testWidgets("both layout menus notify galleries when only the strategy changes",(tester) async {