Skip to content

Commit a9a05cf

Browse files
author
Declan Taylor
committed
Fixed issue with MakeSuggestionsPrivate which was unnecassarily duplicating functionality. Fixes issue with PathInformation returning an empty string if the path ended with \\.
Combined two ISuggest classes into CombinedSuggest.
1 parent 62397aa commit a9a05cf

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

source/Demos/CachedPathSuggestBoxDemo/Infrastructure/CachedPathInformationSuggest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public void Insert(string path)
4141
/// </example>
4242
public async Task<IEnumerable<object>?> MakeSuggestions(string queryThis)
4343
{
44-
return await Task.Run(() => MakeSuggestionsPrivate(queryThis).ToArray());
44+
return await Task.Run(() => MakeSuggestionsPrivate().ToArray());
4545

46-
IEnumerable<object> MakeSuggestionsPrivate(string queryThis) => from item in GetPathInformations(queryThis)
47-
where item.Name.Contains(queryThis, StringComparison.CurrentCultureIgnoreCase)
46+
IEnumerable<object> MakeSuggestionsPrivate() =>
47+
from item in GetPathInformations(queryThis)
4848
select new { Header = item.FullName, Value = item.FullName };
4949

5050
static PathInformation[] GetPathInformations(string key)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#nullable enable
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace CachedPathSuggestBoxDemo.Infrastructure
9+
{
10+
class CombinedSuggest:ISuggest
11+
{
12+
private readonly DirectorySuggest directorySuggest = new DirectorySuggest();
13+
14+
public readonly CachedPathInformationSuggest CachedPathInformationSuggest = new CachedPathInformationSuggest();
15+
16+
public async Task<IEnumerable<object>?> MakeSuggestions(string queryThis)
17+
{
18+
19+
var suggestions1 = (await CachedPathInformationSuggest.MakeSuggestions(queryThis))?.ToArray();
20+
21+
var suggestions2 = (await directorySuggest.MakeSuggestions(queryThis))?.ToArray();
22+
23+
if (suggestions2 == null && suggestions1?.Any()==false)
24+
{
25+
return null;
26+
}
27+
28+
return suggestions2!=null ?
29+
suggestions1?.Concat(suggestions2) :
30+
suggestions1;
31+
}
32+
}
33+
}

source/Demos/CachedPathSuggestBoxDemo/Infrastructure/LiteRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public IEnumerable<KeyValuePair<string, DateTime>> Filter(string key, string? co
3131
{
3232
using var db = new LiteDatabase(DbPath);
3333
var col = db.GetCollection<KeyValuePair<string, DateTime>>(collectionName ?? CollectionName);
34-
return string.IsNullOrEmpty(key)?
35-
new KeyValuePair<string, DateTime>[]{}:
34+
return string.IsNullOrWhiteSpace(key)?
35+
col.Query().ToArray():
3636
col.Find(Query.Contains(nameof(KeyValuePair<string, DateTime>.Key), key)).ToArray();
3737
}
3838

source/Demos/CachedPathSuggestBoxDemo/Infrastructure/PathInformation.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ public class PathInformation : IEquatable<PathInformation>
77
{
88
public PathInformation(string argValue)
99
{
10-
Name = new DirectoryInfo(argValue).Parent == null ? argValue : Path.GetFileName(argValue);
10+
Name = new DirectoryInfo(argValue).Parent == null ? argValue :
11+
argValue.EndsWith("\\")? Path.GetDirectoryName(argValue):
12+
Path.GetFileName(argValue);
13+
1114
FullName = argValue;
1215
}
1316

@@ -18,17 +21,16 @@ public PathInformation(string argValue)
1821

1922
public bool Equals(PathInformation other)
2023
{
21-
if (ReferenceEquals(null, other)) return false;
24+
if (other is null) return false;
2225
if (ReferenceEquals(this, other)) return true;
2326
return Name == other.Name && FullName == other.FullName;
2427
}
2528

2629
public override bool Equals(object obj)
2730
{
28-
if (ReferenceEquals(null, obj)) return false;
31+
if (obj is null) return false;
2932
if (ReferenceEquals(this, obj)) return true;
30-
if (obj.GetType() != this.GetType()) return false;
31-
return Equals((PathInformation)obj);
33+
return obj.GetType() == this.GetType() && Equals((PathInformation)obj);
3234
}
3335

3436
public override int GetHashCode()

source/Demos/CachedPathSuggestBoxDemo/MainWindow.xaml.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+

32
using System.Linq;
43
using System.Windows;
54
using CachedPathSuggestBoxDemo.Infrastructure;
@@ -11,10 +10,8 @@ namespace CachedPathSuggestBoxDemo
1110
/// </summary>
1211
public partial class MainWindow : Window
1312
{
14-
private readonly DirectorySuggest directorySuggest = new DirectorySuggest();
13+
private readonly CombinedSuggest combinedSuggest =new CombinedSuggest();
1514
private readonly FastObservableCollection<object> listQueryResult = new FastObservableCollection<object>();
16-
public readonly CachedPathInformationSuggest cachedPathInformationSuggest =new CachedPathInformationSuggest();
17-
1815
public MainWindow()
1916
{
2017
InitializeComponent();
@@ -36,28 +33,20 @@ private async void Execute(object p)
3633
if (!(p is string newText))
3734
return;
3835

39-
var suggestions2 = await directorySuggest.MakeSuggestions(newText);
40-
36+
var suggestions = (await combinedSuggest.MakeSuggestions(newText))?.ToArray();
4137
listQueryResult.Clear();
42-
if (suggestions2 == null)
38+
if (suggestions == null)
4339
{
4440
this.DiskPathSuggestBox.ValidText = false;
4541
return;
4642
}
4743
this.DiskPathSuggestBox.ValidText = true;
48-
49-
var suggestions1 = await cachedPathInformationSuggest.MakeSuggestions(newText);
50-
51-
var enumerable = suggestions1
52-
.Concat(suggestions2)
53-
.ToArray();
54-
55-
listQueryResult.AddItems(enumerable);
44+
listQueryResult.AddItems(suggestions);
5645
}
5746

5847
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
5948
{
60-
cachedPathInformationSuggest.Insert(Text);
49+
combinedSuggest.CachedPathInformationSuggest.Insert(Text);
6150
}
6251
}
6352
}

0 commit comments

Comments
 (0)