Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qhy040404 committed Jun 12, 2024
1 parent ca2bf13 commit 21e9b7c
Show file tree
Hide file tree
Showing 6 changed files with 599 additions and 657 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,31 @@ public AutoSuggestTokenBox()
DefaultStyleKey = typeof(TokenizingTextBox.TokenizingTextBox);
}

public IEnumerable<SearchToken> Tokens
{
get => ((IList)ItemsSource).OfType<SearchToken>();
}

public override void OnTextChanged(Microsoft.UI.Xaml.Controls.AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (IsTokenLimitReached())
{
return;
}

if (string.IsNullOrWhiteSpace(Text))
{
sender.ItemsSource = AvailableTokens
.ExceptBy(Tokens, kvp => kvp.Value)
.OrderBy(kvp => kvp.Value.Kind)
.ThenBy(kvp => kvp.Value.Order)
.Select(kvp => kvp.Value);
}

if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
sender.ItemsSource = AvailableTokens
.ExceptBy(Tokens, kvp => kvp.Value)
.Where(kvp => kvp.Value.Value.Contains(Text, StringComparison.OrdinalIgnoreCase))
.OrderBy(kvp => kvp.Value.Kind)
.ThenBy(kvp => kvp.Value.Order)
Expand Down Expand Up @@ -70,7 +83,7 @@ public override void OnTokenItemAdded(TokenizingTextBox.TokenizingTextBox sender
{
if (args is SearchToken { Kind: SearchTokenKind.None } token)
{
((IList)sender.ItemsSource).Remove(token);
((IList)ItemsSource).Remove(token);
}

base.OnTokenItemAdded(sender, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using CommunityToolkit.WinUI.Helpers;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace Snap.Hutao.Control.TokenizingTextBox;
Expand All @@ -12,6 +13,11 @@ internal sealed class InterspersedObservableCollection : IList, IEnumerable<obje
private readonly Dictionary<int, object> interspersedObjects = [];
private bool isInsertingOriginal;

public InterspersedObservableCollection()
: this(new ObservableCollection<object>())
{
}

public InterspersedObservableCollection(object itemsSource)
{
if (itemsSource is not IList list)
Expand Down Expand Up @@ -303,11 +309,10 @@ private int ToOuterIndex(int innerIndex)
if (innerIndex >= key)
{
innerIndex++;
continue;
}
else
{
break;
}

break;
}

return innerIndex;
Expand All @@ -324,11 +329,10 @@ private int ToOuterIndexAfterRemoval(int innerIndexToProject)
if (innerIndexToProject >= key)
{
innerIndexToProject++;
continue;
}
else
{
break;
}

break;
}

return innerIndexToProject;
Expand All @@ -338,12 +342,10 @@ private bool ItemKeySearch(object value, out int key)
{
if (interspersedObjects.ContainsValue(value))
{
if (value is null)
{
key = interspersedObjects.First(kvp => kvp.Value is null).Key;
}
key = value is null
? interspersedObjects.First(kvp => kvp.Value is null).Key
: interspersedObjects.First(kvp => kvp.Value.Equals(value)).Key;

key = interspersedObjects.First(kvp => kvp.Value.Equals(value)).Key;
return true;
}

Expand Down
Loading

0 comments on commit 21e9b7c

Please sign in to comment.