2022-09-23 06:16:16 +00:00
|
|
|
import 'package:collection/collection.dart' show IterableExtension;
|
2020-07-15 20:29:29 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-11-18 11:56:39 +00:00
|
|
|
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';
|
2020-07-15 20:29:29 +00:00
|
|
|
|
|
|
|
class SelectedFiles extends ChangeNotifier {
|
2023-08-24 16:56:24 +00:00
|
|
|
final files = <EnteFile>{};
|
2023-07-13 06:32:33 +00:00
|
|
|
|
|
|
|
///This variable is used to track the files that were involved in last selection
|
|
|
|
///operation (select/unselect). Each [LazyGridView] checks this variable on
|
2023-07-13 06:52:20 +00:00
|
|
|
///change in [SelectedFiles] to see if any of it's files were involved in last
|
2023-07-13 06:32:33 +00:00
|
|
|
///select/unselect operation. If yes, then it will rebuild itself.
|
2023-08-24 16:56:24 +00:00
|
|
|
final lastSelectionOperationFiles = <EnteFile>{};
|
2020-07-15 20:29:29 +00:00
|
|
|
|
2023-08-24 16:56:24 +00:00
|
|
|
void toggleSelection(EnteFile fileToToggle) {
|
2022-06-29 09:41:20 +00:00
|
|
|
// 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(
|
2023-07-13 06:52:20 +00:00
|
|
|
(element) => _isMatch(fileToToggle, element),
|
2022-06-29 09:41:20 +00:00
|
|
|
);
|
|
|
|
if (alreadySelected != null) {
|
|
|
|
files.remove(alreadySelected);
|
2020-07-15 20:29:29 +00:00
|
|
|
} else {
|
2023-07-13 06:52:20 +00:00
|
|
|
files.add(fileToToggle);
|
2020-07-15 20:29:29 +00:00
|
|
|
}
|
2023-07-13 06:32:33 +00:00
|
|
|
lastSelectionOperationFiles.clear();
|
2023-07-13 06:52:20 +00:00
|
|
|
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) {
|
2023-07-13 06:52:20 +00:00
|
|
|
files.addAll(filesToSelect);
|
2023-07-13 06:32:33 +00:00
|
|
|
lastSelectionOperationFiles.clear();
|
2023-07-13 06:52:20 +00:00
|
|
|
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}) {
|
2023-07-13 05:47:53 +00:00
|
|
|
files.removeWhere((file) => filesToUnselect.contains(file));
|
2023-07-13 06:32:33 +00:00
|
|
|
lastSelectionOperationFiles.clear();
|
|
|
|
lastSelectionOperationFiles.addAll(filesToUnselect);
|
2022-12-15 10:02:46 +00:00
|
|
|
if (!skipNotify) {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2020-07-15 20:29:29 +00:00
|
|
|
}
|
|
|
|
|
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),
|
2022-06-29 09:41:20 +00:00
|
|
|
);
|
|
|
|
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-06-29 09:41:20 +00:00
|
|
|
);
|
2022-11-14 12:53:03 +00:00
|
|
|
return matchedFile != null;
|
2022-06-29 09:41:20 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-15 20:29:29 +00:00
|
|
|
void clearAll() {
|
2022-11-19 11:36:16 +00:00
|
|
|
Bus.instance.fire(ClearSelectionsEvent());
|
2023-07-13 06:32:33 +00:00
|
|
|
lastSelectionOperationFiles.addAll(files);
|
2020-07-15 20:29:29 +00:00
|
|
|
files.clear();
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2023-06-28 08:48:05 +00:00
|
|
|
|
2023-07-13 06:52:20 +00:00
|
|
|
///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) {
|
2023-07-13 06:52:20 +00:00
|
|
|
files.retainAll(filesToRetain);
|
2023-06-28 08:48:05 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
2020-07-15 20:29:29 +00:00
|
|
|
}
|