PS-592 Mobile - Name field is not prioritised in search results (#1907)

- Search method now gives priority to matches in the Name property

Co-authored-by: André Bispo <abispo@bitwarden.com>
This commit is contained in:
André Filipe da Silva Bispo 2022-05-23 10:20:48 +01:00 committed by GitHub
parent 22c746543a
commit a259560d29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -74,28 +74,34 @@ namespace Bit.Core.Services
CancellationToken ct = default, bool deleted = false) CancellationToken ct = default, bool deleted = false)
{ {
ct.ThrowIfCancellationRequested(); ct.ThrowIfCancellationRequested();
var matchedCiphers = new List<CipherView>();
var lowPriorityMatchedCiphers = new List<CipherView>();
query = query.Trim().ToLower(); query = query.Trim().ToLower();
return ciphers.Where(c =>
foreach (var c in ciphers)
{ {
ct.ThrowIfCancellationRequested(); ct.ThrowIfCancellationRequested();
if (c.Name?.ToLower().Contains(query) ?? false) 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<List<SendView>> SearchSendsAsync(string query, Func<SendView, bool> filter = null, public async Task<List<SendView>> SearchSendsAsync(string query, Func<SendView, bool> filter = null,
@ -133,25 +139,31 @@ namespace Bit.Core.Services
public List<SendView> SearchSendsBasic(List<SendView> sends, string query, CancellationToken ct = default, public List<SendView> SearchSendsBasic(List<SendView> sends, string query, CancellationToken ct = default,
bool deleted = false) bool deleted = false)
{ {
var matchedSends = new List<SendView>();
var lowPriorityMatchSends = new List<SendView>();
ct.ThrowIfCancellationRequested(); ct.ThrowIfCancellationRequested();
query = query.Trim().ToLower(); query = query.Trim().ToLower();
return sends.Where(s =>
foreach (var s in sends)
{ {
ct.ThrowIfCancellationRequested(); ct.ThrowIfCancellationRequested();
if (s.Name?.ToLower().Contains(query) ?? false) 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;
} }
} }
} }