Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Input;

namespace CommunityToolkit.Maui.Behaviors;
Expand Down Expand Up @@ -97,13 +98,13 @@ public void Dispose()
}

/// <inheritdoc/>
protected override void OnViewPropertyChanged(InputView sender, PropertyChangedEventArgs e)
protected override async void OnViewPropertyChanged(InputView sender, PropertyChangedEventArgs e)
{
base.OnViewPropertyChanged(sender, e);

if (e.PropertyName == InputView.TextProperty.PropertyName)
{
OnTextPropertyChanged();
await OnTextPropertyChanged(sender, sender.Text);
}
}

Expand All @@ -121,38 +122,38 @@ protected virtual void Dispose(bool disposing)
}
}

void OnTextPropertyChanged()
async Task OnTextPropertyChanged(InputView view, string? text)
{
if (tokenSource != null)
if (tokenSource is not null)
{
tokenSource.Cancel();
tokenSource.Dispose();
}

tokenSource = new CancellationTokenSource();

Task.Delay(StoppedTypingTimeThreshold, tokenSource.Token)
.ContinueWith(task =>
{
if (task.IsFaulted && task.Exception != null)
{
throw task.Exception;
}

if (task.Status == TaskStatus.Canceled ||
View?.Text?.Length < MinimumLengthThreshold)
{
return;
}

if (View != null && ShouldDismissKeyboardAutomatically)
{
Dispatcher.DispatchIfRequired(View.Unfocus);
}

if (View != null && Command?.CanExecute(CommandParameter ?? View.Text) is true)
{
Command.Execute(CommandParameter ?? View.Text);
}
});
if (text is null || text.Length < MinimumLengthThreshold)
{
return;
}

var task = Task.Delay(StoppedTypingTimeThreshold, tokenSource.Token);
await task.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing | ConfigureAwaitOptions.ContinueOnCapturedContext);

if (task.Status is TaskStatus.Canceled)
{
Trace.WriteLine($"{nameof(UserStoppedTypingBehavior)}.{nameof(OnTextPropertyChanged)} cancelled");
return;
}

if (ShouldDismissKeyboardAutomatically)
{
view.Unfocus();
}

if (Command?.CanExecute(CommandParameter ?? text) is true)
{
Command.Execute(CommandParameter ?? text);
}
}
}
}