From a259560d290aca772793be03dcb3e76ca22a8c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Filipe=20da=20Silva=20Bispo?= Date: Mon, 23 May 2022 10:20:48 +0100 Subject: [PATCH] PS-592 Mobile - Name field is not prioritised in search results (#1907) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Search method now gives priority to matches in the Name property Co-authored-by: AndreĢ Bispo --- src/Core/Services/SearchService.cs | 48 +++++++++++++++++++----------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Core/Services/SearchService.cs b/src/Core/Services/SearchService.cs index 09610942e..8a5b6ac7d 100644 --- a/src/Core/Services/SearchService.cs +++ b/src/Core/Services/SearchService.cs @@ -74,28 +74,34 @@ namespace Bit.Core.Services CancellationToken ct = default, bool deleted = false) { ct.ThrowIfCancellationRequested(); + var matchedCiphers = new List(); + var lowPriorityMatchedCiphers = new List(); query = query.Trim().ToLower(); - return ciphers.Where(c => + + foreach (var c in ciphers) { ct.ThrowIfCancellationRequested(); if (c.Name?.ToLower().Contains(query) ?? false) { - return true; + matchedCiphers.Add(c); } - if (query.Length >= 8 && c.Id.StartsWith(query)) + else if (query.Length >= 8 && c.Id.StartsWith(query)) { - return true; + lowPriorityMatchedCiphers.Add(c); } - if (c.SubTitle?.ToLower().Contains(query) ?? false) + else if (c.SubTitle?.ToLower().Contains(query) ?? false) { - return true; + lowPriorityMatchedCiphers.Add(c); } - if (c.Login?.Uri?.ToLower()?.Contains(query) ?? false) + else if (c.Login?.Uri?.ToLower()?.Contains(query) ?? false) { - return true; + lowPriorityMatchedCiphers.Add(c); } - return false; - }).ToList(); + } + + ct.ThrowIfCancellationRequested(); + matchedCiphers.AddRange(lowPriorityMatchedCiphers); + return matchedCiphers; } public async Task> SearchSendsAsync(string query, Func filter = null, @@ -133,25 +139,31 @@ namespace Bit.Core.Services public List SearchSendsBasic(List sends, string query, CancellationToken ct = default, bool deleted = false) { + var matchedSends = new List(); + var lowPriorityMatchSends = new List(); ct.ThrowIfCancellationRequested(); query = query.Trim().ToLower(); - return sends.Where(s => + + foreach (var s in sends) { ct.ThrowIfCancellationRequested(); if (s.Name?.ToLower().Contains(query) ?? false) { - return true; + matchedSends.Add(s); } - if (s.Text?.Text?.ToLower().Contains(query) ?? false) + else if (s.Text?.Text?.ToLower().Contains(query) ?? false) { - return true; + lowPriorityMatchSends.Add(s); } - if (s.File?.FileName?.ToLower()?.Contains(query) ?? false) + else if (s.File?.FileName?.ToLower()?.Contains(query) ?? false) { - return true; + lowPriorityMatchSends.Add(s); } - return false; - }).ToList(); + } + + ct.ThrowIfCancellationRequested(); + matchedSends.AddRange(lowPriorityMatchSends); + return matchedSends; } } }