From f44013349f713c96670a72fdbc90e94c5959bd74 Mon Sep 17 00:00:00 2001 From: ashilkn Date: Thu, 5 Oct 2023 18:53:43 +0530 Subject: [PATCH] fix(gallery): groups in gallery show old state when scrolling back to them after updation --- .../component/group/lazy_group_gallery.dart | 16 ++++++++++++---- lib/ui/viewer/gallery/gallery.dart | 16 ++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/ui/viewer/gallery/component/group/lazy_group_gallery.dart b/lib/ui/viewer/gallery/component/group/lazy_group_gallery.dart index dde5200d2..8ddd21e5d 100644 --- a/lib/ui/viewer/gallery/component/group/lazy_group_gallery.dart +++ b/lib/ui/viewer/gallery/component/group/lazy_group_gallery.dart @@ -137,10 +137,18 @@ class _LazyGroupGalleryState extends State { dayStartTime.microsecondsSinceEpoch + microSecondsInDay - 1, asc: GalleryContextState.of(context)!.sortOrderAsc, ); - if (mounted) { - setState(() { - _files = result.files; - }); + + //When items are updated in a LazyGroupGallery, only it rebuilds with the + //new state of _files which is a state variable in it's state object. + //widget.files does not change as the Widget itself is immutable. So + //to create a new Widget of LazyLoadingGallery with the updated + //widget.files, we have to call setState from an ancestor state object. + //[galleryState] will never be null except when LazyLoadingGallery is + //used without Gallery as an ancestor. + final galleryState = context.findAncestorStateOfType(); + if (galleryState?.mounted ?? false) { + galleryState!.setState(() {}); + _files = result.files; } } else if (kDebugMode) { debugPrint("Unexpected event ${event.type.name}"); diff --git a/lib/ui/viewer/gallery/gallery.dart b/lib/ui/viewer/gallery/gallery.dart index 21a7696ec..6d7f4b6cd 100644 --- a/lib/ui/viewer/gallery/gallery.dart +++ b/lib/ui/viewer/gallery/gallery.dart @@ -83,15 +83,15 @@ class Gallery extends StatefulWidget { @override State createState() { - return _GalleryState(); + return GalleryState(); } } -class _GalleryState extends State { +class GalleryState extends State { static const int kInitialLoadLimit = 100; late Logger _logger; - List> _currentGroupedFiles = []; + List> currentGroupedFiles = []; bool _hasLoadedFiles = false; late ItemScrollController _itemScroller; StreamSubscription? _reloadEventSubscription; @@ -197,17 +197,17 @@ class _GalleryState extends State { bool _onFilesLoaded(List files) { final updatedGroupedFiles = widget.enableFileGrouping ? _groupFiles(files) : [files]; - if (_currentGroupedFiles.length != updatedGroupedFiles.length || - _currentGroupedFiles.isEmpty) { + if (currentGroupedFiles.length != updatedGroupedFiles.length || + currentGroupedFiles.isEmpty) { if (mounted) { setState(() { _hasLoadedFiles = true; - _currentGroupedFiles = updatedGroupedFiles; + currentGroupedFiles = updatedGroupedFiles; }); } return true; } else { - _currentGroupedFiles = updatedGroupedFiles; + currentGroupedFiles = updatedGroupedFiles; return false; } } @@ -233,7 +233,7 @@ class _GalleryState extends State { inSelectionMode: widget.inSelectionMode, child: MultipleGroupsGalleryView( itemScroller: _itemScroller, - groupedFiles: _currentGroupedFiles, + groupedFiles: currentGroupedFiles, disableScroll: widget.disableScroll, emptyState: widget.emptyState, asyncLoader: widget.asyncLoader,