Browse Source

Fix: selectedFiles use generatedId for tracking selected files

Neeraj Gupta 3 years ago
parent
commit
d2c506f149

+ 1 - 1
ios/Runner.xcodeproj/project.pbxproj

@@ -754,4 +754,4 @@
 /* End XCConfigurationList section */
 	};
 	rootObject = 97C146E61CF9000F007C117D /* Project object */;
-}
+}

+ 26 - 3
lib/models/selected_files.dart

@@ -6,16 +6,39 @@ class SelectedFiles extends ChangeNotifier {
   final lastSelections = <File>{};
 
   void toggleSelection(File file) {
-    if (files.contains(file)) {
-      files.remove(file);
+    // To handle the cases, where the file might have changed due to upload
+    // or any other update, using file.generatedID to track if this file was already
+    // selected or not
+    File alreadySelected = files.firstWhere(
+      (element) => element.generatedID == file.generatedID,
+      orElse: () => null,
+    );
+    if (alreadySelected != null) {
+      files.remove(alreadySelected);
     } else {
       files.add(file);
     }
     lastSelections.clear();
-    lastSelections.add(file);
+    lastSelections.add(alreadySelected ?? file);
     notifyListeners();
   }
 
+  bool isFileSelected(File file) {
+    File alreadySelected = files.firstWhere(
+      (element) => element.generatedID == file.generatedID,
+      orElse: () => null,
+    );
+    return alreadySelected != null;
+  }
+
+  bool isPartOfLastSection(File file) {
+    File alreadySelected = lastSelections.firstWhere(
+      (element) => element.generatedID == file.generatedID,
+      orElse: () => null,
+    );
+    return alreadySelected != null;
+  }
+
   void clearAll() {
     lastSelections.addAll(files);
     files.clear();

+ 3 - 3
lib/ui/huge_listview/lazy_loading_gallery.dart

@@ -217,7 +217,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
     widget.selectedFiles.addListener(() {
       bool shouldRefresh = false;
       for (final file in widget.files) {
-        if (widget.selectedFiles.lastSelections.contains(file)) {
+        if (widget.selectedFiles.isPartOfLastSection(file)) {
           shouldRefresh = true;
         }
       }
@@ -322,7 +322,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
                 child: ColorFiltered(
                   colorFilter: ColorFilter.mode(
                     Colors.black.withOpacity(
-                      widget.selectedFiles.files.contains(file) ? 0.4 : 0,
+                      widget.selectedFiles.isFileSelected(file) ? 0.4 : 0,
                     ),
                     BlendMode.darken,
                   ),
@@ -336,7 +336,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
                 ),
               ),
               Visibility(
-                visible: widget.selectedFiles.files.contains(file),
+                visible: widget.selectedFiles.isFileSelected(file),
                 child: Positioned(
                   right: 4,
                   top: 4,