Browse Source

fix(gallery): groups in gallery show old state when scrolling back to them after updation

ashilkn 1 year ago
parent
commit
f44013349f

+ 12 - 4
lib/ui/viewer/gallery/component/group/lazy_group_gallery.dart

@@ -137,10 +137,18 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
           dayStartTime.microsecondsSinceEpoch + microSecondsInDay - 1,
           dayStartTime.microsecondsSinceEpoch + microSecondsInDay - 1,
           asc: GalleryContextState.of(context)!.sortOrderAsc,
           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<GalleryState>();
+        if (galleryState?.mounted ?? false) {
+          galleryState!.setState(() {});
+          _files = result.files;
         }
         }
       } else if (kDebugMode) {
       } else if (kDebugMode) {
         debugPrint("Unexpected event ${event.type.name}");
         debugPrint("Unexpected event ${event.type.name}");

+ 8 - 8
lib/ui/viewer/gallery/gallery.dart

@@ -83,15 +83,15 @@ class Gallery extends StatefulWidget {
 
 
   @override
   @override
   State<Gallery> createState() {
   State<Gallery> createState() {
-    return _GalleryState();
+    return GalleryState();
   }
   }
 }
 }
 
 
-class _GalleryState extends State<Gallery> {
+class GalleryState extends State<Gallery> {
   static const int kInitialLoadLimit = 100;
   static const int kInitialLoadLimit = 100;
 
 
   late Logger _logger;
   late Logger _logger;
-  List<List<EnteFile>> _currentGroupedFiles = [];
+  List<List<EnteFile>> currentGroupedFiles = [];
   bool _hasLoadedFiles = false;
   bool _hasLoadedFiles = false;
   late ItemScrollController _itemScroller;
   late ItemScrollController _itemScroller;
   StreamSubscription<FilesUpdatedEvent>? _reloadEventSubscription;
   StreamSubscription<FilesUpdatedEvent>? _reloadEventSubscription;
@@ -197,17 +197,17 @@ class _GalleryState extends State<Gallery> {
   bool _onFilesLoaded(List<EnteFile> files) {
   bool _onFilesLoaded(List<EnteFile> files) {
     final updatedGroupedFiles =
     final updatedGroupedFiles =
         widget.enableFileGrouping ? _groupFiles(files) : [files];
         widget.enableFileGrouping ? _groupFiles(files) : [files];
-    if (_currentGroupedFiles.length != updatedGroupedFiles.length ||
-        _currentGroupedFiles.isEmpty) {
+    if (currentGroupedFiles.length != updatedGroupedFiles.length ||
+        currentGroupedFiles.isEmpty) {
       if (mounted) {
       if (mounted) {
         setState(() {
         setState(() {
           _hasLoadedFiles = true;
           _hasLoadedFiles = true;
-          _currentGroupedFiles = updatedGroupedFiles;
+          currentGroupedFiles = updatedGroupedFiles;
         });
         });
       }
       }
       return true;
       return true;
     } else {
     } else {
-      _currentGroupedFiles = updatedGroupedFiles;
+      currentGroupedFiles = updatedGroupedFiles;
       return false;
       return false;
     }
     }
   }
   }
@@ -233,7 +233,7 @@ class _GalleryState extends State<Gallery> {
       inSelectionMode: widget.inSelectionMode,
       inSelectionMode: widget.inSelectionMode,
       child: MultipleGroupsGalleryView(
       child: MultipleGroupsGalleryView(
         itemScroller: _itemScroller,
         itemScroller: _itemScroller,
-        groupedFiles: _currentGroupedFiles,
+        groupedFiles: currentGroupedFiles,
         disableScroll: widget.disableScroll,
         disableScroll: widget.disableScroll,
         emptyState: widget.emptyState,
         emptyState: widget.emptyState,
         asyncLoader: widget.asyncLoader,
         asyncLoader: widget.asyncLoader,