ente/mobile/lib/models/selected_files.dart

96 lines
3.1 KiB
Dart
Raw Normal View History

import 'package:collection/collection.dart' show IterableExtension;
import 'package:flutter/foundation.dart';
import 'package:photos/core/event_bus.dart';
2022-11-19 11:36:16 +00:00
import 'package:photos/events/clear_selections_event.dart';
2023-08-25 04:39:30 +00:00
import 'package:photos/models/file/file.dart';
class SelectedFiles extends ChangeNotifier {
2023-08-24 16:56:24 +00:00
final files = <EnteFile>{};
///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.
2023-08-24 16:56:24 +00:00
final lastSelectionOperationFiles = <EnteFile>{};
2023-08-24 16:56:24 +00:00
void toggleSelection(EnteFile fileToToggle) {
// 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
2023-08-24 16:56:24 +00:00
final EnteFile? alreadySelected = files.firstWhereOrNull(
(element) => _isMatch(fileToToggle, element),
);
if (alreadySelected != null) {
files.remove(alreadySelected);
} else {
files.add(fileToToggle);
}
lastSelectionOperationFiles.clear();
lastSelectionOperationFiles.add(fileToToggle);
2022-11-14 04:34:22 +00:00
notifyListeners();
}
2023-08-24 16:56:24 +00:00
void toggleGroupSelection(Set<EnteFile> filesToToggle) {
2023-07-12 12:29:15 +00:00
if (files.containsAll(filesToToggle)) {
unSelectAll(filesToToggle);
} else {
selectAll(filesToToggle);
}
}
2023-08-24 16:56:24 +00:00
void selectAll(Set<EnteFile> filesToSelect) {
files.addAll(filesToSelect);
lastSelectionOperationFiles.clear();
lastSelectionOperationFiles.addAll(filesToSelect);
2022-11-14 04:34:22 +00:00
notifyListeners();
}
2023-08-24 16:56:24 +00:00
void unSelectAll(Set<EnteFile> filesToUnselect, {bool skipNotify = false}) {
files.removeWhere((file) => filesToUnselect.contains(file));
lastSelectionOperationFiles.clear();
lastSelectionOperationFiles.addAll(filesToUnselect);
2022-12-15 10:02:46 +00:00
if (!skipNotify) {
notifyListeners();
}
}
2023-08-24 16:56:24 +00:00
bool isFileSelected(EnteFile file) {
final EnteFile? alreadySelected = files.firstWhereOrNull(
2022-12-17 10:26:51 +00:00
(element) => _isMatch(file, element),
);
return alreadySelected != null;
}
2023-08-24 16:56:24 +00:00
bool isPartOfLastSelected(EnteFile file) {
final EnteFile? matchedFile = lastSelectionOperationFiles.firstWhereOrNull(
2022-12-17 10:26:51 +00:00
(element) => _isMatch(file, element),
);
2022-11-14 12:53:03 +00:00
return matchedFile != null;
}
2023-08-24 16:56:24 +00:00
bool _isMatch(EnteFile first, EnteFile second) {
2022-12-17 10:26:51 +00:00
if (first.generatedID != null && second.generatedID != null) {
if (first.generatedID == second.generatedID) {
return true;
}
} else if (first.uploadedFileID != null && second.uploadedFileID != null) {
return first.uploadedFileID == second.uploadedFileID;
}
return false;
}
void clearAll() {
2022-11-19 11:36:16 +00:00
Bus.instance.fire(ClearSelectionsEvent());
lastSelectionOperationFiles.addAll(files);
files.clear();
notifyListeners();
}
///Retains only the files that are present in the [filesToRetain] set in
///[files]. Takes the intersection of the two sets.
2023-08-24 16:56:24 +00:00
void retainFiles(Set<EnteFile> filesToRetain) {
files.retainAll(filesToRetain);
notifyListeners();
}
}