Forráskód Böngészése

Enable search by filename

Neeraj Gupta 2 éve
szülő
commit
5d2f1394ff

+ 22 - 6
lib/services/search_service.dart

@@ -208,7 +208,7 @@ class SearchService {
     return searchResults;
   }
 
-  Future<List<GenericSearchResult>> getCaptionResults(
+  Future<List<GenericSearchResult>> getCaptionAndNameResults(
     String query,
   ) async {
     final List<GenericSearchResult> searchResults = [];
@@ -217,15 +217,31 @@ class SearchService {
     }
     final RegExp pattern = RegExp(query, caseSensitive: false);
     final List<File> allFiles = await _getAllFiles();
-    final matchedFiles = allFiles
-        .where((e) => e.caption != null && pattern.hasMatch(e.caption))
-        .toList();
-    if (matchedFiles.isNotEmpty) {
+    final List<File> captionMatch = <File>[];
+    final List<File> displayNameMatch = <File>[];
+    for (File eachFile in allFiles) {
+      if (eachFile.caption != null && pattern.hasMatch(eachFile.caption)) {
+        captionMatch.add(eachFile);
+      }
+      if (pattern.hasMatch(eachFile.displayName)) {
+        displayNameMatch.add(eachFile);
+      }
+    }
+    if (captionMatch.isNotEmpty) {
       searchResults.add(
         GenericSearchResult(
           ResultType.fileCaption,
           query,
-          matchedFiles,
+          captionMatch,
+        ),
+      );
+    }
+    if (displayNameMatch.isNotEmpty) {
+      searchResults.add(
+        GenericSearchResult(
+          ResultType.file,
+          query,
+          displayNameMatch,
         ),
       );
     }

+ 3 - 2
lib/ui/viewer/search/search_widget.dart

@@ -208,8 +208,9 @@ class _SearchWidgetState extends State<SearchWidget> {
           await _searchService.getFileTypeResults(query);
       allResults.addAll(fileTypeSearchResults);
 
-      final fileCaptionResults = await _searchService.getCaptionResults(query);
-      allResults.addAll(fileCaptionResults);
+      final captionAndDisplayNameResult =
+          await _searchService.getCaptionAndNameResults(query);
+      allResults.addAll(captionAndDisplayNameResult);
 
       final fileExtnResult =
           await _searchService.getFileExtensionResults(query);