Added comment and used better variable name for easier code readability in SelectedFiles

This commit is contained in:
ashilkn 2023-07-13 12:02:33 +05:30
parent a453316e1c
commit e5f49c10ad

View file

@ -6,7 +6,12 @@ import 'package:photos/models/file.dart';
class SelectedFiles extends ChangeNotifier { class SelectedFiles extends ChangeNotifier {
final files = <File>{}; final files = <File>{};
final lastSelections = <File>{};
///This variable is used to track the files that were involved in last selection
///operation (select/unselect). Each [LazyGridView] checks this variable on
///change in selectedFiles to see if any of it's files were involved in last
///select/unselect operation. If yes, then it will rebuild itself.
final lastSelectionOperationFiles = <File>{};
void toggleSelection(File file) { void toggleSelection(File file) {
// To handle the cases, where the file might have changed due to upload // To handle the cases, where the file might have changed due to upload
@ -20,8 +25,8 @@ class SelectedFiles extends ChangeNotifier {
} else { } else {
files.add(file); files.add(file);
} }
lastSelections.clear(); lastSelectionOperationFiles.clear();
lastSelections.add(file); lastSelectionOperationFiles.add(file);
notifyListeners(); notifyListeners();
} }
@ -35,15 +40,15 @@ class SelectedFiles extends ChangeNotifier {
void selectAll(Set<File> selectedFiles) { void selectAll(Set<File> selectedFiles) {
files.addAll(selectedFiles); files.addAll(selectedFiles);
lastSelections.clear(); lastSelectionOperationFiles.clear();
lastSelections.addAll(selectedFiles); lastSelectionOperationFiles.addAll(selectedFiles);
notifyListeners(); notifyListeners();
} }
void unSelectAll(Set<File> filesToUnselect, {bool skipNotify = false}) { void unSelectAll(Set<File> filesToUnselect, {bool skipNotify = false}) {
files.removeWhere((file) => filesToUnselect.contains(file)); files.removeWhere((file) => filesToUnselect.contains(file));
lastSelections.clear(); lastSelectionOperationFiles.clear();
lastSelections.addAll(filesToUnselect); lastSelectionOperationFiles.addAll(filesToUnselect);
if (!skipNotify) { if (!skipNotify) {
notifyListeners(); notifyListeners();
} }
@ -57,7 +62,7 @@ class SelectedFiles extends ChangeNotifier {
} }
bool isPartOfLastSelected(File file) { bool isPartOfLastSelected(File file) {
final File? matchedFile = lastSelections.firstWhereOrNull( final File? matchedFile = lastSelectionOperationFiles.firstWhereOrNull(
(element) => _isMatch(file, element), (element) => _isMatch(file, element),
); );
return matchedFile != null; return matchedFile != null;
@ -76,7 +81,7 @@ class SelectedFiles extends ChangeNotifier {
void clearAll() { void clearAll() {
Bus.instance.fire(ClearSelectionsEvent()); Bus.instance.fire(ClearSelectionsEvent());
lastSelections.addAll(files); lastSelectionOperationFiles.addAll(files);
files.clear(); files.clear();
notifyListeners(); notifyListeners();
} }