From bf7f6032bf6d2339248391381726ca5bc32b65c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jib=C3=A9doublev=C3=A9?= Date: Wed, 3 Jan 2024 15:14:07 +0100 Subject: [PATCH] (#421) Refactor TimeSpan with Humanizer (#443) --- src/Lanceur/Views/AppSettingsViewModel.cs | 3 ++- src/Lanceur/Views/KeywordsViewModel.cs | 3 ++- src/Lanceur/Views/MainViewModel.cs | 3 ++- .../ViewModels/MainViewModelShould.Execute.cs | 5 +++-- .../Lanceur.Tests/ViewModels/MainViewModelShould.cs | 13 +++++++------ 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Lanceur/Views/AppSettingsViewModel.cs b/src/Lanceur/Views/AppSettingsViewModel.cs index e26e5806..5619d4a3 100644 --- a/src/Lanceur/Views/AppSettingsViewModel.cs +++ b/src/Lanceur/Views/AppSettingsViewModel.cs @@ -16,6 +16,7 @@ using System.Linq; using System.Reactive; using System.Reactive.Linq; +using Humanizer; namespace Lanceur.Views { @@ -105,7 +106,7 @@ public AppSettingsViewModel( private TimeSpan GetDelay() { var delay = _settingsFacade.Application.RestartDelay; - var time = TimeSpan.FromMilliseconds(delay); + var time = delay.Milliseconds(); return time; } diff --git a/src/Lanceur/Views/KeywordsViewModel.cs b/src/Lanceur/Views/KeywordsViewModel.cs index 03f47418..d4bda91f 100644 --- a/src/Lanceur/Views/KeywordsViewModel.cs +++ b/src/Lanceur/Views/KeywordsViewModel.cs @@ -26,6 +26,7 @@ using System.Reactive.Disposables; using System.Reactive.Linq; using System.Threading.Tasks; +using Humanizer; namespace Lanceur.Views { @@ -236,7 +237,7 @@ private void SetupBindings(IScheduler uiThread, CompositeDisposable d) this.WhenAnyValue(vm => vm.SearchQuery) .DistinctUntilChanged() - .Throttle(TimeSpan.FromMilliseconds(10), scheduler: uiThread) + .Throttle(10.Milliseconds(), scheduler: uiThread) .Select(x => new SearchRequest(x?.Trim(), AliasToCreate)) .Log(this, "Invoking search.", c => $"With criterion '{c.Query}' and alias to create '{c.AliasToCreate?.Name ?? ""}'") .InvokeCommand(Search) diff --git a/src/Lanceur/Views/MainViewModel.cs b/src/Lanceur/Views/MainViewModel.cs index 8ff68d78..0633e8bd 100644 --- a/src/Lanceur/Views/MainViewModel.cs +++ b/src/Lanceur/Views/MainViewModel.cs @@ -18,6 +18,7 @@ using System.Reactive; using System.Reactive.Linq; using System.Threading.Tasks; +using Humanizer; namespace Lanceur.Views { @@ -138,7 +139,7 @@ public MainViewModel( #region Query this.WhenAnyValue(vm => vm.Query.Value) - .Throttle(TimeSpan.FromMilliseconds(100), schedulerProvider.TaskpoolScheduler) + .Throttle(100.Milliseconds(), schedulerProvider.TaskpoolScheduler) .Select(x => x.Trim()) .Where(x => !x.IsNullOrWhiteSpace()) .Log(this, "Query changed.", x => $"'{x}'") diff --git a/src/Tests/Lanceur.Tests/ViewModels/MainViewModelShould.Execute.cs b/src/Tests/Lanceur.Tests/ViewModels/MainViewModelShould.Execute.cs index f083d68b..06ab1a64 100644 --- a/src/Tests/Lanceur.Tests/ViewModels/MainViewModelShould.Execute.cs +++ b/src/Tests/Lanceur.Tests/ViewModels/MainViewModelShould.Execute.cs @@ -12,6 +12,7 @@ using NSubstitute; using ReactiveUI.Testing; using System.Reactive.Concurrency; +using FluentAssertions.Extensions; using Xunit; namespace Lanceur.Tests.ViewModels @@ -30,7 +31,7 @@ public void ExecuteA_NOT_ExecutableQueryResult() .With(scheduler) .Build(); - scheduler.Schedule(TimeSpan.FromTicks(00), () => vm.CurrentAlias = new NotExecutableTestAlias()); + scheduler.Schedule(0.Ticks(), () => vm.CurrentAlias = new NotExecutableTestAlias()); var results = scheduler.Start( () => vm.ExecuteAlias.CanExecute, @@ -55,7 +56,7 @@ public void ExecuteAnExecutableQueryResult() .With(scheduler) .Build(); - scheduler.Schedule(TimeSpan.FromTicks(00), () => vm.CurrentAlias = new ExecutableTestAlias()); + scheduler.Schedule(0.Ticks(), () => vm.CurrentAlias = new ExecutableTestAlias()); var results = scheduler.Start( () => vm.ExecuteAlias.CanExecute, diff --git a/src/Tests/Lanceur.Tests/ViewModels/MainViewModelShould.cs b/src/Tests/Lanceur.Tests/ViewModels/MainViewModelShould.cs index d2ee1dd6..93295f89 100644 --- a/src/Tests/Lanceur.Tests/ViewModels/MainViewModelShould.cs +++ b/src/Tests/Lanceur.Tests/ViewModels/MainViewModelShould.cs @@ -18,6 +18,7 @@ using ReactiveUI.Testing; using Splat; using System.Reactive.Concurrency; +using FluentAssertions.Extensions; using Xunit; using Xunit.Abstractions; @@ -90,20 +91,20 @@ public void NotifyWhenCriterionChanges() .Build(); scheduler.Schedule(() => vm.Query.Value = "a"); - scheduler.Schedule(TimeSpan.FromTicks(200), () => vm.Query.Value += "b"); - scheduler.Schedule(TimeSpan.FromTicks(300), () => vm.Query.Value += "c"); - scheduler.Schedule(TimeSpan.FromTicks(400), () => vm.Query.Value += "d"); + scheduler.Schedule(200.Ticks(), () => vm.Query.Value += "b"); + scheduler.Schedule(300.Ticks(), () => vm.Query.Value += "c"); + scheduler.Schedule(400.Ticks(), () => vm.Query.Value += "d"); var results = scheduler.Start( () => vm.SearchAlias.IsExecuting, created: 0, subscribed: 100, - disposed: TimeSpan.FromMilliseconds(1_000).Ticks); + disposed: 1_000.Milliseconds().Ticks); results.Messages.AssertEqual( OnNext(100, false), - OnNext(TimeSpan.FromMilliseconds(100).Ticks + 402, true), - OnNext(TimeSpan.FromMilliseconds(100).Ticks + 404, false) + OnNext(100.Milliseconds().Ticks + 402, true), + OnNext(100.Milliseconds().Ticks + 404, false) ); }); }