Skip to content

Commit 90d9a83

Browse files
author
Declan Taylor
committed
Replaces ISuggestSource with IAsyncSuggest and modifies ISuggestResult.
Removes ISuggest file and uses IAsyncSuggest and ISuggestResult in SuggestBoxLib project in places of its interfaces. Moves logic for validation and assigning suggestions to the SuggestBox's ItemsSource out of viewmodel and into SuggestBox. This includes -removing CollectionEmptyValidationRule and QueryResults from AppViewModel in CachedPathSuggestBox.Demo -moving FastObservableCollection and NameOfExtension into SuggestBoxLib.
1 parent a43eeea commit 90d9a83

File tree

22 files changed

+282
-415
lines changed

22 files changed

+282
-415
lines changed

source/Demos/CachedPathSuggest/Infrastructure/DirectoryHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace CachedPathSuggest.Infrastructure
77
{
8-
internal static class DirectoryHelper
8+
public static class DirectoryHelper
99
{
1010
public static IEnumerable<(string path, string? label)>? EnumerateSubDirectories(string input)
1111
{
@@ -36,7 +36,7 @@ internal static class DirectoryHelper
3636
catch
3737
{
3838
// Catch invalid path exceptions here ...
39-
return EnumerateLogicalDrives();
39+
return Array.Empty<(string, string?)>();
4040
}
4141
}
4242

source/Demos/CachedPathSuggest/Service/ISuggest.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

source/Demos/CachedPathSuggestBox.Demo/App.xaml

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
x:Class="CachedPathSuggestBox.Demo.App"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
65
xmlns:suggestBoxLib="clr-namespace:SuggestBoxLib;assembly=SuggestBoxLib"
76
xmlns:vm="clr-namespace:CachedPathSuggest.ViewModels;assembly=CachedPathSuggest"
8-
xmlns:validationRules="clr-namespace:CachedPathSuggestBox.Demo.ValidationRules"
9-
xmlns:infrastructure="clr-namespace:CachedPathSuggestBox.Demo.Infrastructure"
107
StartupUri="MainWindow.xaml">
118
<Application.Resources>
129

@@ -80,36 +77,6 @@
8077
<Setter Property="Hint" Value="Enter a file-system-path or the Space key" />
8178
</Style>
8279

83-
<suggestBoxLib:SuggestBox x:Key="DiskPathSuggestBox"
84-
Style="{StaticResource SuggestBoxStyle}"
85-
ItemsSource="{Binding QueryResults, Mode=OneTime}"
86-
ItemContainerStyle="{StaticResource ItemContainerStyle}">
87-
<suggestBoxLib:SuggestBox.SelectedValuePath>
88-
<infrastructure:NameOf Type='{x:Type vm:BaseItem}' Member='Value' />
89-
</suggestBoxLib:SuggestBox.SelectedValuePath>
90-
<b:Interaction.Triggers>
91-
<b:EventTrigger EventName="QueryChanged">
92-
<b:InvokeCommandAction Command="{Binding TextChangedCommand}" PassEventArgsToCommand="True" />
93-
</b:EventTrigger>
94-
</b:Interaction.Triggers>
95-
<TextBox.Resources>
96-
<validationRules:BindingProxy x:Key="TargetProxy" Data="{Binding Path=QueryResults.Count}" />
97-
<validationRules:BindingProxy x:Key="SourceProxy"
98-
Data="{Binding Path=Text, ElementName=DiskPathSuggestBox, Mode=TwoWay}" />
99-
</TextBox.Resources>
100-
<TextBox.Text>
101-
<Binding Path="Text" UpdateSourceTrigger="PropertyChanged">
102-
<Binding.ValidationRules>
103-
<validationRules:IsEmptyValidationRule>
104-
<validationRules:ComparisonValue
105-
Value="{Binding Data, Source={StaticResource TargetProxy}}"
106-
BindingToTrigger="{Binding Data, Source={StaticResource SourceProxy}}" />
107-
</validationRules:IsEmptyValidationRule>
108-
</Binding.ValidationRules>
109-
</Binding>
110-
</TextBox.Text>
111-
</suggestBoxLib:SuggestBox>
112-
11380
</ResourceDictionary>
11481
</Application.Resources>
11582

source/Demos/CachedPathSuggestBox.Demo/MainWindow.xaml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
77
xmlns:viewModel="clr-namespace:CachedPathSuggestBox.Demo.ViewModel"
8+
xmlns:suggestBoxLib="clr-namespace:SuggestBoxLib;assembly=SuggestBoxLib"
9+
xmlns:infrastructure="clr-namespace:CachedPathSuggestBox.Demo.Infrastructure;assembly=SuggestBoxLib"
10+
xmlns:vm="clr-namespace:CachedPathSuggest.ViewModels;assembly=CachedPathSuggest"
11+
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
812
Title="MainWindow"
913
Width="800"
1014
Height="250"
@@ -18,7 +22,20 @@
1822
<ColumnDefinition Width="Auto" />
1923
</Grid.ColumnDefinitions>
2024

21-
<ContentControl Content="{StaticResource DiskPathSuggestBox}" />
25+
<suggestBoxLib:SuggestBox
26+
Style="{StaticResource SuggestBoxStyle}"
27+
SuggestSource="{Binding AsyncSuggest, Mode=OneTime}"
28+
ItemContainerStyle="{StaticResource ItemContainerStyle}"
29+
Text="{Binding Path=Text, UpdateSourceTrigger=PropertyChanged}">
30+
<suggestBoxLib:SuggestBox.SelectedValuePath>
31+
<infrastructure:NameOf Type='{x:Type vm:BaseItem}' Member='Value' />
32+
</suggestBoxLib:SuggestBox.SelectedValuePath>
33+
<b:Interaction.Triggers>
34+
<b:EventTrigger EventName="QueryChanged">
35+
<b:InvokeCommandAction Command="{Binding TextChangedCommand}" PassEventArgsToCommand="True" />
36+
</b:EventTrigger>
37+
</b:Interaction.Triggers>
38+
</suggestBoxLib:SuggestBox>
2239

2340
<WrapPanel Grid.Column="1" Margin="5">
2441
<FrameworkElement.Resources>

source/Demos/CachedPathSuggest/Service/CachedPathInformationSuggest.cs renamed to source/Demos/CachedPathSuggestBox.Demo/Service/CachedAsyncSuggest.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43
using System.Linq;
54
using System.Threading.Tasks;
65
using CachedPathSuggest.Infrastructure;
76
using CachedPathSuggest.ViewModels;
7+
using SuggestBoxLib.Interfaces;
8+
using SuggestBoxLib.Model;
89

9-
namespace CachedPathSuggest.Service
10+
namespace CachedPathSuggestBox.Demo.Service
1011
{
1112
/// <summary>
1213
/// Wraps a LiteDB to generate previously bookmarked suggestions through similarity for a given string.
1314
/// </summary>
14-
public class CachedPathInformationAsyncSuggest : IAsyncSuggest
15+
public class CachedAsyncSuggest : IAsyncSuggest
1516
{
1617
private static readonly int NumberOfResultsToReturn = 5;
1718
private readonly LiteRepository repository;
1819

19-
public CachedPathInformationAsyncSuggest(LiteRepository repository)
20+
public CachedAsyncSuggest(LiteRepository repository)
2021
{
2122
this.repository = repository;
2223
}
@@ -35,9 +36,9 @@ public CachedPathInformationAsyncSuggest(LiteRepository repository)
3536
/// f:\\do_letters
3637
/// g:\\document\lists.ico
3738
/// </example>
38-
public async Task<IReadOnlyCollection<BaseItem>?> SuggestAsync(string queryThis)
39+
public async Task<ISuggestResult> SuggestAsync(string queryThis)
3940
{
40-
return await Task.Run(() => GetPathInformations(queryThis, repository));
41+
return new SuggestResult(await Task.Run(() => GetPathInformations(queryThis, repository)));
4142

4243
static CachedPathInformation[] GetPathInformations(string key, LiteRepository repository)
4344
{

source/Demos/CachedPathSuggest/Service/CombinedSuggest.cs renamed to source/Demos/CachedPathSuggestBox.Demo/Service/CombinedAsyncSuggest.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
using System.Threading.Tasks;
55
using CachedPathSuggest.Infrastructure;
66
using CachedPathSuggest.ViewModels;
7+
using SuggestBoxLib.Interfaces;
8+
using SuggestBoxLib.Model;
79

8-
namespace CachedPathSuggest.Service
10+
namespace CachedPathSuggestBox.Demo.Service
911
{
1012
/// <summary>
1113
/// Wraps a LiteDB and a FileSystem data provider to generate similarity based suggestions
1214
/// for a given string.
1315
/// </summary>
1416
public class CombinedAsyncSuggest : IAsyncSuggest
1517
{
16-
private readonly CachedPathInformationAsyncSuggest
17-
cachedPathInformationAsyncSuggest = new(new LiteRepository());
18-
18+
private readonly CachedAsyncSuggest cachedAsyncSuggest = new(new LiteRepository());
1919
private readonly DirectoryAsyncSuggest directoryAsyncSuggest = new();
2020

2121
/// <summary>
@@ -29,20 +29,22 @@ private readonly CachedPathInformationAsyncSuggest
2929
/// in a single list. The list contains a separator item (if both types of items are returned).
3030
/// The separator item can be used for visual enhancement when displaying the list.
3131
/// </returns>
32-
public async Task<IReadOnlyCollection<BaseItem>?> SuggestAsync(string queryThis)
32+
public async Task<ISuggestResult> SuggestAsync(string queryThis)
3333
{
34-
var cachedSuggestions = await cachedPathInformationAsyncSuggest.SuggestAsync(queryThis);
34+
var cachedSuggestions = await cachedAsyncSuggest.SuggestAsync(queryThis);
3535
var directorySuggestions = await directoryAsyncSuggest.SuggestAsync(queryThis);
36-
return (cachedSuggestions, directorySuggestions) switch
36+
var suggestions = (cachedSuggestions, directorySuggestions) switch
3737
{
3838
(null, null) => null,
39-
({Count: 0}, {Count: 0}) => null,
40-
({Count: 0}, {Count: > 0}) => directorySuggestions,
41-
({Count: > 0}, {Count: 0}) => cachedSuggestions,
42-
({Count: > 0} c, {Count: > 0} d) => c
43-
.Concat(new[] {new ItemSeparator()}).Concat(d).ToArray(),
39+
({ Suggestions: { Count: 0 } }, { Suggestions: { Count: 0 } }) => null,
40+
({ Suggestions: { Count: 0 } }, { Suggestions: { Count: > 0 } }) => directorySuggestions.Suggestions,
41+
({ Suggestions: { Count: > 0 } }, { Suggestions: { Count: 0 } }) => cachedSuggestions.Suggestions,
42+
({ Suggestions: { Count: > 0 } c }, { Suggestions: { Count: > 0 } d }) => c
43+
44+
.Concat(new[] { new ItemSeparator() }).Concat(d).ToArray(),
4445
_ => throw new ArgumentOutOfRangeException()
4546
};
47+
return new SuggestResult(suggestions, suggestions?.Count > 0);
4648
}
4749

4850
/// <summary>
@@ -51,7 +53,7 @@ private readonly CachedPathInformationAsyncSuggest
5153
/// <param name="text"></param>
5254
public void AddCachedSuggestion(string text)
5355
{
54-
cachedPathInformationAsyncSuggest.Insert(text);
56+
cachedAsyncSuggest.Insert(text);
5557
}
5658

5759
/// <summary>
@@ -60,7 +62,7 @@ public void AddCachedSuggestion(string text)
6062
/// <param name="text"></param>
6163
public void RemoveCachedSuggestion(string text)
6264
{
63-
cachedPathInformationAsyncSuggest.Delete(text);
65+
cachedAsyncSuggest.Delete(text);
6466
}
6567

6668
/// <summary>
@@ -69,7 +71,7 @@ public void RemoveCachedSuggestion(string text)
6971
/// <param name="text"></param>
7072
public bool ContainsSuggestion(string text)
7173
{
72-
return cachedPathInformationAsyncSuggest.Match(text);
74+
return cachedAsyncSuggest.Match(text);
7375
}
7476
}
7577
}

source/Demos/CachedPathSuggest/Service/DirectorySuggestSource.cs renamed to source/Demos/CachedPathSuggestBox.Demo/Service/DirectoryAsyncSuggest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
using System.Threading.Tasks;
66
using CachedPathSuggest.Infrastructure;
77
using CachedPathSuggest.ViewModels;
8+
using SuggestBoxLib.Interfaces;
9+
using SuggestBoxLib.Model;
810

9-
namespace CachedPathSuggest.Service
11+
namespace CachedPathSuggestBox.Demo.Service
1012
{
1113
/// <summary>
1214
/// Wraps a LiteDB and a FileSystem data provider to generate similarity based suggestions
@@ -28,19 +30,19 @@ public DirectoryAsyncSuggest()
2830
/// <summary>
2931
/// Class constructor
3032
/// </summary>
31-
public async Task<IReadOnlyCollection<BaseItem>?> SuggestAsync(string queryThis)
33+
public async Task<ISuggestResult> SuggestAsync(string queryThis)
3234
{
3335
tokenSource.Cancel();
3436

3537
tokenSource = new CancellationTokenSource();
36-
var ty = Task.Run(() =>
38+
var ty =await Task.Run(() =>
3739
(string.IsNullOrWhiteSpace(queryThis)
3840
? DirectoryHelper.EnumerateLogicalDrives().ToArray()
3941
: DirectoryHelper.EnumerateSubDirectories(queryThis)?.ToArray() ??
4042
Array.Empty<(string name, string? vi)>())
4143
.Select(r => new PathInformation(r.path, r.label)).ToArray(), tokenSource.Token);
4244

43-
return await ty;
45+
return new SuggestResult(ty);
4446
}
4547
}
4648
}

source/Demos/CachedPathSuggestBox.Demo/ValidationRules/CollectionEmptyValidationRule.cs

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)