Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
fix: incorrect number of selected files on selecting all from a grid …
Browse files Browse the repository at this point in the history
…after deleting items from it (#1684)
  • Loading branch information
ashilkn authored Jan 29, 2024
2 parents 636ec6c + cbdcac4 commit e392821
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions lib/ui/viewer/gallery/component/group/lazy_group_gallery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ 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;

@override
void initState() {
super.initState();
_areAllFromGroupSelectedNotifier = ValueNotifier(_areAllFromGroupSelected());
_areAllFromGroupSelectedNotifier =
ValueNotifier(_areAllFromGroupSelected());

widget.selectedFiles?.addListener(_selectedFilesListener);
_showSelectAllButtonNotifier = ValueNotifier(widget.showSelectAllByDefault);
Expand All @@ -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));

Expand All @@ -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) {
Expand All @@ -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!);
Expand Down Expand Up @@ -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}");
Expand All @@ -172,15 +167,15 @@ 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();
}
}

@override
Widget build(BuildContext context) {
if (_files.isEmpty) {
if (_filesInGroup.isEmpty) {
return const SizedBox.shrink();
}
return Column(
Expand All @@ -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()),
Expand Down Expand Up @@ -226,7 +221,7 @@ class _LazyGroupGalleryState extends State<LazyGroupGallery> {
),
onTap: () {
widget.selectedFiles?.toggleGroupSelection(
_setOfFiles,
_filesInGroup.toSet(),
);
},
);
Expand All @@ -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,
Expand All @@ -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,
),
],
Expand All @@ -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) {
Expand Down

0 comments on commit e392821

Please sign in to comment.