From 77d7d358f380b7fa04b6c53f91ea93ab67c2bb20 Mon Sep 17 00:00:00 2001 From: ashilkn Date: Wed, 23 Jul 2025 20:50:13 +0530 Subject: [PATCH] Minor refactoring and removing unnecessary work on Gallery widget --- .../photos/lib/ui/viewer/gallery/gallery.dart | 57 ++++++++----------- 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart b/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart index 915cbead16..f218fda072 100644 --- a/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart +++ b/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart @@ -123,7 +123,6 @@ class GalleryState extends State { double? groupHeaderExtent; late Logger _logger; - List> currentGroupedFiles = []; bool _hasLoadedFiles = false; StreamSubscription? _reloadEventSubscription; StreamSubscription? _tabDoubleTapEvent; @@ -138,6 +137,7 @@ class GalleryState extends State { final scrollBarInUseNotifier = ValueNotifier(false); late GroupType _groupType; final scrollbarBottomPaddingNotifier = ValueNotifier(0); + late GalleryGroups galleryGroups; @override void initState() { @@ -187,6 +187,7 @@ class GalleryState extends State { ); } if (!hasTriggeredSetState && mounted) { + _updateGalleryGroups(); setState(() {}); } }); @@ -223,8 +224,11 @@ class GalleryState extends State { if (widget.initialFiles != null && !_sortOrderAsc) { _onFilesLoaded(widget.initialFiles!); } + _loadFiles(limit: kInitialLoadLimit).then((result) async { _setFilesAndReload(result.files); + // if hasmore, let load complete and then set scrollcontroller + // else set scrollcontroller. if (result.hasMore) { final result = await _loadFiles(); _setFilesAndReload(result.files); @@ -283,6 +287,19 @@ class GalleryState extends State { } } + void _updateGalleryGroups() { + galleryGroups = GalleryGroups( + allFiles: _allGalleryFiles, + groupType: _groupType, + sortOrderAsc: _sortOrderAsc, + widthAvailable: MediaQuery.sizeOf(context).width, + selectedFiles: widget.selectedFiles, + tagPrefix: widget.tagPrefix, + groupHeaderExtent: groupHeaderExtent, + showSelectAll: widget.showSelectAll, + ); + } + void _selectedFilesListener() { final bottomInset = MediaQuery.paddingOf(context).bottom; final extra = widget.galleryType == GalleryType.homepage ? 76.0 : 0.0; @@ -305,6 +322,7 @@ class GalleryState extends State { void _setFilesAndReload(List files) { final hasReloaded = _onFilesLoaded(files); if (!hasReloaded && mounted) { + _updateGalleryGroups(); setState(() {}); } } @@ -393,29 +411,10 @@ class GalleryState extends State { return shouldReloadFromDB; } - // group files into multiple groups and returns `true` if it resulted in a - // gallery reload bool _onFilesLoaded(List files) { _allGalleryFiles = files; - - final updatedGroupedFiles = - widget.enableFileGrouping && _groupType.timeGrouping() - ? _groupBasedOnTime(files) - : _genericGroupForPerf(files); - if (currentGroupedFiles.length != updatedGroupedFiles.length || - currentGroupedFiles.isEmpty) { - if (mounted) { - setState(() { - _hasLoadedFiles = true; - currentGroupedFiles = updatedGroupedFiles; - }); - return true; - } - return false; - } else { - currentGroupedFiles = updatedGroupedFiles; - return false; - } + _hasLoadedFiles = true; + return false; } Future _loadFiles({int? limit}) async { @@ -516,16 +515,6 @@ class GalleryState extends State { : const SizedBox.shrink(); } - final galleryGroups = GalleryGroups( - allFiles: _allGalleryFiles, - groupType: _groupType, - widthAvailable: widthAvailable, - selectedFiles: widget.selectedFiles, - tagPrefix: widget.tagPrefix, - groupHeaderExtent: groupHeaderExtent!, - showSelectAll: widget.showSelectAll, - limitSelectionToOne: widget.limitSelectionToOne, - ); GalleryFilesState.of(context).setGalleryFiles = _allGalleryFiles; if (!_hasLoadedFiles) { return widget.loadingWidget; @@ -870,8 +859,8 @@ class _PinnedGroupHeaderState extends State { child: ColoredBox( color: getEnteColorScheme(context).backgroundBase, child: GroupHeaderWidget( - title: widget - .galleryGroups.groupIdToGroupTypeMap[currentGroupId!]! + title: widget.galleryGroups + .groupIdToGroupDataMap[currentGroupId!]!.groupType .getTitle( context, widget.galleryGroups.groupIDToFilesMap[currentGroupId]!