fix: incorrect number of selected files on selecting all from a grid after deleting items from it (#1684)

This commit is contained in:
Ashil 2024-01-29 16:30:20 +05:30 committed by GitHub
commit e39282177a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -56,8 +56,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
late Logger _logger;
late List<EnteFile> _files;
Set<EnteFile>? _filesAsSet;
late List<EnteFile> _filesInGroup;
late StreamSubscription<FilesUpdatedEvent>? _reloadEventSubscription;
late StreamSubscription<int> _currentIndexSubscription;
bool? _shouldRender;
@ -65,7 +64,8 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
@override
void initState() {
super.initState();
_areAllFromGroupSelectedNotifier = ValueNotifier(_areAllFromGroupSelected());
_areAllFromGroupSelectedNotifier =
ValueNotifier(_areAllFromGroupSelected());
widget.selectedFiles?.addListener(_selectedFilesListener);
_showSelectAllButtonNotifier = ValueNotifier(widget.showSelectAllByDefault);
@ -75,7 +75,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
void _init() {
_logger = Logger("LazyLoading_${widget.logTag}");
_shouldRender = true;
_files = widget.files;
_filesInGroup = widget.files;
_areAllFromGroupSelectedNotifier.value = _areAllFromGroupSelected();
_reloadEventSubscription = widget.reloadEvent?.listen((e) => _onReload(e));
@ -91,11 +91,6 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
});
}
Set<EnteFile> get _setOfFiles {
_filesAsSet ??= _files.toSet();
return _filesAsSet!;
}
bool _areAllFromGroupSelected() {
if (widget.selectedFiles != null &&
widget.selectedFiles!.files.length >= widget.files.length) {
@ -106,11 +101,11 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
}
Future _onReload(FilesUpdatedEvent event) async {
if (_files.isEmpty) {
if (_filesInGroup.isEmpty) {
return;
}
final DateTime groupDate =
DateTime.fromMicrosecondsSinceEpoch(_files[0].creationTime!);
DateTime.fromMicrosecondsSinceEpoch(_filesInGroup[0].creationTime!);
// iterate over files and check if any of the belongs to this group
final anyCandidateForGroup = event.updatedFiles.any((file) {
final fileDate = DateTime.fromMicrosecondsSinceEpoch(file.creationTime!);
@ -152,7 +147,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
final galleryState = context.findAncestorStateOfType<GalleryState>();
if (galleryState?.mounted ?? false) {
galleryState!.setState(() {});
_files = result.files;
_filesInGroup = result.files;
}
} else if (kDebugMode) {
debugPrint("Unexpected event ${event.type.name}");
@ -172,7 +167,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
@override
void didUpdateWidget(LazyGroupGallery oldWidget) {
super.didUpdateWidget(oldWidget);
if (!listEquals(_files, widget.files)) {
if (!listEquals(_filesInGroup, widget.files)) {
_reloadEventSubscription?.cancel();
_init();
}
@ -180,7 +175,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
@override
Widget build(BuildContext context) {
if (_files.isEmpty) {
if (_filesInGroup.isEmpty) {
return const SizedBox.shrink();
}
return Column(
@ -190,7 +185,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
children: [
if (widget.enableFileGrouping)
GroupHeaderWidget(
timestamp: _files[0].creationTime!,
timestamp: _filesInGroup[0].creationTime!,
gridSize: widget.photoGridSize,
),
Expanded(child: Container()),
@ -226,7 +221,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
),
onTap: () {
widget.selectedFiles?.toggleGroupSelection(
_setOfFiles,
_filesInGroup.toSet(),
);
},
);
@ -237,7 +232,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
_shouldRender!
? GroupGallery(
photoGridSize: widget.photoGridSize,
files: _files,
files: _filesInGroup,
tag: widget.tag,
asyncLoader: widget.asyncLoader,
selectedFiles: widget.selectedFiles,
@ -246,7 +241,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
// todo: perf eval should we have separate PlaceHolder for Groups
// instead of creating a large cached view
: PlaceHolderGridViewWidget(
_files.length,
_filesInGroup.length,
widget.photoGridSize,
),
],
@ -256,7 +251,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
void _selectedFilesListener() {
if (widget.selectedFiles == null) return;
_areAllFromGroupSelectedNotifier.value =
widget.selectedFiles!.files.containsAll(_setOfFiles);
widget.selectedFiles!.files.containsAll(_filesInGroup.toSet());
//Can remove this if we decide to show select all by default for all galleries
if (widget.selectedFiles!.files.isEmpty && !widget.showSelectAllByDefault) {