Merge pull request #735 from ente-io/file_search

This commit is contained in:
Neeraj Gupta 2022-12-27 13:21:32 +05:30 committed by GitHub
commit 6d33b5904c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 25 deletions

View file

@ -1,5 +1,6 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart';
import 'package:photo_manager/photo_manager.dart';
@ -269,7 +270,7 @@ class File extends EnteFile {
if (pubMagicMetadata != null && pubMagicMetadata!.editedName != null) {
return pubMagicMetadata!.editedName!;
}
if (title == null) _logger.severe('File title is null');
if (title == null && kDebugMode) _logger.severe('File title is null');
return title ?? '';
}

View file

@ -52,20 +52,6 @@ class SearchService {
return _cachedFilesFuture;
}
Future<List<File>> getFileSearchResults(String query) async {
final List<File> fileSearchResults = [];
final List<File> files = await _getAllFiles();
for (var file in files) {
if (fileSearchResults.length >= _maximumResultsLimit) {
break;
}
if (file.title.toLowerCase().contains(query.toLowerCase())) {
fileSearchResults.add(file);
}
}
return fileSearchResults;
}
void clearCache() {
_cachedFilesFuture = null;
}
@ -208,7 +194,7 @@ class SearchService {
return searchResults;
}
Future<List<GenericSearchResult>> getCaptionResults(
Future<List<GenericSearchResult>> getCaptionAndNameResults(
String query,
) async {
final List<GenericSearchResult> searchResults = [];
@ -217,15 +203,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,
),
);
}

View file

@ -116,7 +116,7 @@ class SearchResultWidget extends StatelessWidget {
case ResultType.month:
return "Month";
case ResultType.file:
return "Memory";
return "File name";
case ResultType.event:
return "Day";
case ResultType.location:
@ -124,7 +124,7 @@ class SearchResultWidget extends StatelessWidget {
case ResultType.fileType:
return "Type";
case ResultType.fileExtension:
return "File Extension";
return "File extension";
case ResultType.fileCaption:
return "Description";
default:

View file

@ -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);