Skip to content

armgit / ente

Public Imported from GitHub
0

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

Improve performance of deciding what title to show on the PinnedGroupHeader based on group

ashilkn2426b7405cb8
diff --git a/mobile/apps/photos/lib/models/gallery/gallery_sections.dart b/mobile/apps/photos/lib/models/gallery/gallery_sections.dart
index 7f6d53c12a..365acbeea2 100644
--- a/mobile/apps/photos/lib/models/gallery/gallery_sections.dart
+++ b/mobile/apps/photos/lib/models/gallery/gallery_sections.dart
@@ -47,6 +47,7 @@ class GalleryGroups {
final Map<String, List<EnteFile>> _groupIdToFilesMap = {};
final Map<String, GroupHeaderData> _groupIdToHeaderDataMap = {};
final Map<double, String> _scrollOffsetToGroupIdMap = {};
+ final List<double> _groupScrollOffsets = [];
final currentUserID = Configuration.instance.getUserID();
final _uuid = const Uuid();
@@ -56,11 +57,18 @@ class GalleryGroups {
_groupIdToHeaderDataMap;
Map<double, String> get scrollOffsetToGroupIdMap => _scrollOffsetToGroupIdMap;
List<FixedExtentSectionLayout> get groupLayouts => _groupLayouts;
+ List<double> get groupScrollOffsets => _groupScrollOffsets;
void init() {
_buildGroups();
crossAxisCount = localSettings.getPhotoGridSize();
_groupLayouts = _computeGroupLayouts();
+ assert(groupIDs.length == _groupIdToFilesMap.length);
+ assert(groupIDs.length == _groupIdToHeaderDataMap.length);
+ assert(
+ groupIDs.length == _scrollOffsetToGroupIdMap.length,
+ );
+ assert(groupIDs.length == _groupScrollOffsets.length);
}
List<FixedExtentSectionLayout> _computeGroupLayouts() {
@@ -166,6 +174,7 @@ class GalleryGroups {
);
_scrollOffsetToGroupIdMap[currentOffset] = groupID;
+ _groupScrollOffsets.add(currentOffset);
currentIndex = lastIndex;
currentOffset = maxOffset;
diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart b/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart
index 0ef6346fad..5cf15b5b98 100644
--- a/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart
+++ b/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart
@@ -613,15 +613,40 @@ class _PinnedGroupHeaderState extends State<PinnedGroupHeader> {
if (normalizedScrollOffset < 0) {
currentGroupID = null;
} else {
- //TODO: make this more efficient. This is very less thought out.
- final entry =
- widget.galleryGroups.scrollOffsetToGroupIdMap.entries.firstWhere(
- (element) => element.key >= normalizedScrollOffset,
- orElse: () =>
- widget.galleryGroups.scrollOffsetToGroupIdMap.entries.last,
- );
- currentGroupID = entry.value;
+ final groupScrollOffsets = widget.galleryGroups.groupScrollOffsets;
+
+ // Binary search to find the index of the largest scrollOffset in
+ // groupScrollOffsets which is <= scrollPosition
+ int low = 0;
+ int high = groupScrollOffsets.length - 1;
+ int floorIndex = 0;
+
+ // Handle the case where scrollPosition is smaller than the first key.
+ // In this scenario, we associate it with the first heading.
+ if (normalizedScrollOffset < groupScrollOffsets.first) {
+ return;
+ }
+
+ while (low <= high) {
+ final mid = low + (high - low) ~/ 2;
+ final midValue = groupScrollOffsets[mid];
+
+ if (midValue <= normalizedScrollOffset) {
+ // This key is less than or equal to the target scrollPosition.
+ // It's a potential floor. Store its index and try searching higher
+ // for a potentially closer floor value.
+ floorIndex = mid;
+ low = mid + 1;
+ } else {
+ // This key is greater than the target scrollPosition.
+ // The floor must be in the lower half.
+ high = mid - 1;
+ }
+ }
+ currentGroupID = widget.galleryGroups
+ .scrollOffsetToGroupIdMap[groupScrollOffsets[floorIndex]];
}
+
setState(() {});
}
@@ -629,12 +654,13 @@ class _PinnedGroupHeaderState extends State<PinnedGroupHeader> {
Widget build(BuildContext context) {
return currentGroupID != null
? GroupHeaderWidget(
- title: widget.galleryGroups.groupIdToheaderDataMap[currentGroupID!]!
- .groupType
- .getTitle(
- context,
- widget.galleryGroups.groupIDToFilesMap[currentGroupID]!.first,
- ),
+ title: widget.galleryGroups.groupIdToheaderDataMap[currentGroupID!]
+ ?.groupType
+ .getTitle(
+ context,
+ widget.galleryGroups.groupIDToFilesMap[currentGroupID]!.first,
+ ) ??
+ '',
gridSize: localSettings.getPhotoGridSize(),
height: widget.galleryGroups.headerExtent,
)