From 7f879aaf575b979c41fe5368c58bd841fcc4a2a9 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Fri, 19 Apr 2024 13:34:25 +0530 Subject: [PATCH] [mob][photos] Hide incoming file from home screen is saved --- .../services/filter/collection_ignore.dart | 44 ++++++++++++++----- mobile/lib/services/filter/db_filters.dart | 18 ++++++-- mobile/lib/ui/home/home_gallery_widget.dart | 1 + 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/mobile/lib/services/filter/collection_ignore.dart b/mobile/lib/services/filter/collection_ignore.dart index d9e27f094..f7f50a7df 100644 --- a/mobile/lib/services/filter/collection_ignore.dart +++ b/mobile/lib/services/filter/collection_ignore.dart @@ -1,26 +1,38 @@ import 'package:photos/models/file/file.dart'; import "package:photos/services/filter/filter.dart"; -// CollectionsIgnoreFilter will filter out files that are in present in the -// given collections. This is useful for filtering out files that are in archive -// or hidden collections from home page and other places -class CollectionsIgnoreFilter extends Filter { +// CollectionsOrHashIgnoreFilter will filter out all files that are in present in the +// given collections collectionIDs. This is useful for filtering out files that are in archive +// or hidden collections from home page and other places. Based on flag, it will also filter out +// shared files if the user already as another file with the same hash. +class CollectionsAndSavedFileFilter extends Filter { final Set collectionIDs; + final bool ignoreSavedFiles; + final int ownerID; Set? _ignoredUploadIDs; + Set ownedFileHashes = {}; - CollectionsIgnoreFilter(this.collectionIDs, List files) : super() { + CollectionsAndSavedFileFilter( + this.collectionIDs, + this.ownerID, + List files, + this.ignoreSavedFiles, + ) : super() { init(files); } void init(List files) { _ignoredUploadIDs = {}; - if (collectionIDs.isEmpty) return; for (var file in files) { - if (file.collectionID != null && - file.isUploaded && - collectionIDs.contains(file.collectionID!)) { - _ignoredUploadIDs!.add(file.uploadedFileID!); + if (file.collectionID != null && file.isUploaded) { + if (collectionIDs.contains(file.collectionID!)) { + _ignoredUploadIDs!.add(file.uploadedFileID!); + } else if (ignoreSavedFiles && + file.ownerID == ownerID && + (file.hash ?? '').isNotEmpty) { + ownedFileHashes.add(file.hash!); + } } } } @@ -37,6 +49,16 @@ class CollectionsIgnoreFilter extends Filter { } return true; } - return !_ignoredUploadIDs!.contains(file.uploadedFileID!); + if (_ignoredUploadIDs!.contains(file.uploadedFileID!)) { + return false; // this file should be filtered out + } + if (ignoreSavedFiles && + file.ownerID != ownerID && + (file.hash ?? '').isNotEmpty) { + // if the file is shared and the user already has a file with the same hash + // then filter it out by returning false + return !ownedFileHashes.contains(file.hash!); + } + return true; } } diff --git a/mobile/lib/services/filter/db_filters.dart b/mobile/lib/services/filter/db_filters.dart index ad062fe1c..0c969dc52 100644 --- a/mobile/lib/services/filter/db_filters.dart +++ b/mobile/lib/services/filter/db_filters.dart @@ -1,3 +1,4 @@ +import "package:photos/core/configuration.dart"; import 'package:photos/models/file/file.dart'; import "package:photos/services/filter/collection_ignore.dart"; import "package:photos/services/filter/dedupe_by_upload_id.dart"; @@ -12,11 +13,14 @@ class DBFilterOptions { Set? ignoredCollectionIDs; bool dedupeUploadID; bool hideIgnoredForUpload; + // If true, shared files that are already saved in the users account will be ignored. + bool ignoreSavedFiles; DBFilterOptions({ this.ignoredCollectionIDs, this.hideIgnoredForUpload = false, this.dedupeUploadID = true, + this.ignoreSavedFiles = false, }); static DBFilterOptions dedupeOption = DBFilterOptions( @@ -42,12 +46,18 @@ Future> applyDBFilters( if (options.dedupeUploadID) { filters.add(DedupeUploadIDFilter()); } - if (options.ignoredCollectionIDs != null && - options.ignoredCollectionIDs!.isNotEmpty) { - final collectionIgnoreFilter = - CollectionsIgnoreFilter(options.ignoredCollectionIDs!, files); + + if ((options.ignoredCollectionIDs ?? {}).isNotEmpty || + options.ignoreSavedFiles) { + final collectionIgnoreFilter = CollectionsAndSavedFileFilter( + options.ignoredCollectionIDs ?? {}, + Configuration.instance.getUserID() ?? 0, + files, + options.ignoreSavedFiles, + ); filters.add(collectionIgnoreFilter); } + final List filterFiles = []; for (final file in files) { if (filters.every((f) => f.filter(file))) { diff --git a/mobile/lib/ui/home/home_gallery_widget.dart b/mobile/lib/ui/home/home_gallery_widget.dart index 195eb7b95..5d9f9c09d 100644 --- a/mobile/lib/ui/home/home_gallery_widget.dart +++ b/mobile/lib/ui/home/home_gallery_widget.dart @@ -41,6 +41,7 @@ class HomeGalleryWidget extends StatelessWidget { hideIgnoredForUpload: true, dedupeUploadID: true, ignoredCollectionIDs: collectionsToHide, + ignoreSavedFiles: true, ); if (hasSelectedAllForBackup) { result = await FilesDB.instance.getAllLocalAndUploadedFiles(