From 748f8b927067f0cc03a5f91a5d546f1ab84bc287 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Sat, 14 Sep 2024 23:02:57 +0200 Subject: [PATCH] #1192 fixed path filter --- lib/model/filters/path.dart | 10 ++++++---- test/model/filters_test.dart | 13 +++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/model/filters/path.dart b/lib/model/filters/path.dart index 4bb6c7cde..7515ab635 100644 --- a/lib/model/filters/path.dart +++ b/lib/model/filters/path.dart @@ -9,22 +9,24 @@ class PathFilter extends CollectionFilter { static const type = 'path'; // including trailing separator - final String path; + late final String path; // without trailing separator - final String _rootAlbum; + late final String _rootAlbum; late final EntryFilter _test; @override List get props => [path, reversed]; - PathFilter(this.path, {super.reversed = false}) : _rootAlbum = path.substring(0, path.length - 1) { + PathFilter(String path, {super.reversed = false}) { + this.path = path.endsWith(pContext.separator) ? path : '$path${pContext.separator}'; + _rootAlbum = this.path.substring(0, this.path.length - 1); _test = (entry) { final dir = entry.directory; if (dir == null) return false; // avoid string building in most cases - return dir.startsWith(_rootAlbum) && '$dir${pContext.separator}'.startsWith(path); + return dir.startsWith(_rootAlbum) && '$dir${pContext.separator}'.startsWith(this.path); }; } diff --git a/test/model/filters_test.dart b/test/model/filters_test.dart index 44781b96c..db4cca9a5 100644 --- a/test/model/filters_test.dart +++ b/test/model/filters_test.dart @@ -93,9 +93,14 @@ void main() { final subImage = FakeMediaStoreService.newImage(subAlbum, 'image1'); final siblingImage = FakeMediaStoreService.newImage(siblingAlbum, 'image1'); - final path = PathFilter('$rootAlbum/'); - expect(path.test(rootImage), true); - expect(path.test(subImage), true); - expect(path.test(siblingImage), false); + final untrailedPath = PathFilter(rootAlbum); + expect(untrailedPath.test(rootImage), true); + expect(untrailedPath.test(subImage), true); + expect(untrailedPath.test(siblingImage), false); + + final trailedPath = PathFilter('$rootAlbum/'); + expect(trailedPath.test(rootImage), true); + expect(trailedPath.test(subImage), true); + expect(trailedPath.test(siblingImage), false); }); }