From 90560b40249fbfffbd128a43fbd824c9fe2822e1 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 28 Jul 2023 19:03:12 +0200 Subject: [PATCH 1/4] Update componente loading con stima fake --- EgwCoreLib.BlazorTest/Pages/TestLoading.razor | 14 ++ .../Pages/TestLoading.razor.cs | 58 +++++++ EgwCoreLib.Razor/ProgressDisplay.razor | 33 ++++ EgwCoreLib.Razor/ProgressDisplay.razor.cs | 157 ++++++++++++++++++ 4 files changed, 262 insertions(+) create mode 100644 EgwCoreLib.BlazorTest/Pages/TestLoading.razor create mode 100644 EgwCoreLib.BlazorTest/Pages/TestLoading.razor.cs create mode 100644 EgwCoreLib.Razor/ProgressDisplay.razor create mode 100644 EgwCoreLib.Razor/ProgressDisplay.razor.cs diff --git a/EgwCoreLib.BlazorTest/Pages/TestLoading.razor b/EgwCoreLib.BlazorTest/Pages/TestLoading.razor new file mode 100644 index 0000000..51306b3 --- /dev/null +++ b/EgwCoreLib.BlazorTest/Pages/TestLoading.razor @@ -0,0 +1,14 @@ +@page "/TestLoading" + +

TestLoading

+
+ + +
+
+ + +
+ + + diff --git a/EgwCoreLib.BlazorTest/Pages/TestLoading.razor.cs b/EgwCoreLib.BlazorTest/Pages/TestLoading.razor.cs new file mode 100644 index 0000000..24296ba --- /dev/null +++ b/EgwCoreLib.BlazorTest/Pages/TestLoading.razor.cs @@ -0,0 +1,58 @@ +namespace EgwCoreLib.BlazorTest.Pages +{ + public partial class TestLoading + { + #region Protected Properties + + protected double expTime { get; set; } = 20; + protected int numSteps { get; set; } = 4; + + #endregion Protected Properties + + #region Protected Methods + + protected async Task StartLongTimer() + { + double stdWait = expTime / numSteps; + int nextWait = 1000; + int stepVal = maxVal / numSteps; + // imposto i valori x progress.. + maxVal = 100; + expTimeMSec = stdWait * 1000; + //nextVal = stepVal; + //currVal = 0; + for (int currStep = 1; currStep <= numSteps; currStep++) + { + //await Task.Delay(1); + // aggiorno valori + currVal = (currStep - 1) * stepVal; + nextVal = currStep * stepVal; + // se max mi fermo... + if (nextVal > maxVal) + { + nextVal = maxVal; + } + await InvokeAsync(StateHasChanged); + nextWait = (int)(rnd.Next(2000, 2500) * stdWait); + // attendo step successivi... + await Task.Delay(nextWait); + } + await Task.Delay(1); + currVal = maxVal; + await InvokeAsync(StateHasChanged); + } + + #endregion Protected Methods + + #region Private Fields + + private int currVal = 100; + private double expTimeMSec = 10000; + private int maxVal = 100; + private int nextVal = 100; + private Random rnd = new Random(); + private string titleMsg = "Test Progress"; + + #endregion Private Fields + } +} \ No newline at end of file diff --git a/EgwCoreLib.Razor/ProgressDisplay.razor b/EgwCoreLib.Razor/ProgressDisplay.razor new file mode 100644 index 0000000..7c2164d --- /dev/null +++ b/EgwCoreLib.Razor/ProgressDisplay.razor @@ -0,0 +1,33 @@ +@*
+
+

loading data

+ +
+
+*@ + +@if (IsLoading) +{ + + + +} \ No newline at end of file diff --git a/EgwCoreLib.Razor/ProgressDisplay.razor.cs b/EgwCoreLib.Razor/ProgressDisplay.razor.cs new file mode 100644 index 0000000..03a4d2d --- /dev/null +++ b/EgwCoreLib.Razor/ProgressDisplay.razor.cs @@ -0,0 +1,157 @@ +using Microsoft.AspNetCore.Components; + +namespace EgwCoreLib.Razor +{ + public partial class ProgressDisplay : IDisposable + { + #region Public Properties + + [Parameter] + public double CurrVal + { + get => ActVal; + set + { + ActVal = value; + StartVal = value; + IsLoading = ActVal < MaxVal; + } + } + + [Parameter] + public double ExpTimeMSec { get; set; } = 10000; + + [Parameter] + public bool IsLoading { get; set; } = false; + + [Parameter] + public double MaxVal { get; set; } = 100; + + [Parameter] + public double NextVal { get; set; } = 100; + + [Parameter] + public string Title { get; set; } = "Progress"; + + #endregion Public Properties + + #region Public Methods + + public void Dispose() + { + periodicTimer?.Dispose(); + } + + #endregion Public Methods + + #region Protected Fields + + protected PeriodicTimer? periodicTimer = null; + + #endregion Protected Fields + + #region Protected Properties + + protected string currWidth { get; set; } = ""; + protected string modalCss { get; set; } = "alert alert-success"; + + #endregion Protected Properties + + #region Protected Methods + +#if false + protected override void OnInitialized() + { + RunTimer(); // fire-and-forget + } +#endif + + protected override async Task OnParametersSetAsync() + { + // salvo valori aggiornamento... + if (ExpTimeMSec > 0) + { + lastUpdate = DateTime.Now; + nextUpdate = lastUpdate.AddMilliseconds(ExpTimeMSec); + await RefreshDisplay(); + } + if(IsLoading) + { + periodicTimer?.Dispose(); + periodicTimer = new PeriodicTimer(TimeSpan.FromMilliseconds(RefInt)); + RunTimer(); + } + else + { + periodicTimer = null; + } + } + + protected async Task RefreshDisplay() + { + currWidth = $"{(double)ActVal / MaxVal:P0}"; + DisplayMsg = $"{ActVal / MaxVal:P2}"; + await InvokeAsync(StateHasChanged); + } + + #endregion Protected Methods + + #region Private Fields + + [Parameter] + public int RefreshInterval + { + get => RefInt; + set + { + if (RefInt != value && value > 0) + { + RefInt = value; +#if false + periodicTimer?.Dispose(); + periodicTimer = new PeriodicTimer(TimeSpan.FromMilliseconds(value)); + RunTimer(); +#endif + } + } + } + private int RefInt { get; set; } = 100; + + #endregion Private Fields + + #region Private Properties + + private double ActVal { get; set; } = 100; + private string DisplayMsg { get; set; } = "Loading..."; + private DateTime lastUpdate { get; set; } = DateTime.Now.AddMinutes(-1); + private DateTime nextUpdate { get; set; } = DateTime.Now; + private double StartVal { get; set; } = 100; + + #endregion Private Properties + + #region Private Methods + + private async void RunTimer() + { + while (periodicTimer!=null && await periodicTimer.WaitForNextTickAsync()) + { + if (IsLoading) + { + DateTime adesso = DateTime.Now; + if (nextUpdate <= adesso) + { + nextUpdate = adesso.AddMilliseconds(RefreshInterval); + } + // calcolo delta ms... + var deltaMs = adesso.Subtract(lastUpdate).TotalMilliseconds; + var denomMs = nextUpdate.Subtract(lastUpdate).TotalMilliseconds; + // aggiorno display... + ActVal = StartVal + (NextVal - StartVal) * (deltaMs / denomMs); + await RefreshDisplay(); + } + } + } + + #endregion Private Methods + } +} \ No newline at end of file From 7fd09155e016bb2adc6e7fc798791796f449932e Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Sat, 29 Jul 2023 09:50:51 +0200 Subject: [PATCH 2/4] Affinamento display update --- EgwCoreLib.BlazorTest/Pages/TestLoading.razor | 2 +- .../Pages/TestLoading.razor.cs | 12 +- EgwCoreLib.Razor/ProgressDisplay.razor | 43 ++-- EgwCoreLib.Razor/ProgressDisplay.razor.cs | 186 ++++++++++++------ 4 files changed, 154 insertions(+), 89 deletions(-) diff --git a/EgwCoreLib.BlazorTest/Pages/TestLoading.razor b/EgwCoreLib.BlazorTest/Pages/TestLoading.razor index 51306b3..9e66ea2 100644 --- a/EgwCoreLib.BlazorTest/Pages/TestLoading.razor +++ b/EgwCoreLib.BlazorTest/Pages/TestLoading.razor @@ -11,4 +11,4 @@ - + diff --git a/EgwCoreLib.BlazorTest/Pages/TestLoading.razor.cs b/EgwCoreLib.BlazorTest/Pages/TestLoading.razor.cs index 24296ba..eaaecf0 100644 --- a/EgwCoreLib.BlazorTest/Pages/TestLoading.razor.cs +++ b/EgwCoreLib.BlazorTest/Pages/TestLoading.razor.cs @@ -4,7 +4,7 @@ namespace EgwCoreLib.BlazorTest.Pages { #region Protected Properties - protected double expTime { get; set; } = 20; + protected double expTime { get; set; } = 6; protected int numSteps { get; set; } = 4; #endregion Protected Properties @@ -18,12 +18,11 @@ namespace EgwCoreLib.BlazorTest.Pages int stepVal = maxVal / numSteps; // imposto i valori x progress.. maxVal = 100; - expTimeMSec = stdWait * 1000; + expTimeMSec = (int)(stdWait * 1000); //nextVal = stepVal; //currVal = 0; for (int currStep = 1; currStep <= numSteps; currStep++) { - //await Task.Delay(1); // aggiorno valori currVal = (currStep - 1) * stepVal; nextVal = currStep * stepVal; @@ -33,7 +32,8 @@ namespace EgwCoreLib.BlazorTest.Pages nextVal = maxVal; } await InvokeAsync(StateHasChanged); - nextWait = (int)(rnd.Next(2000, 2500) * stdWait); + // simulo ritardo importante (da 2 a 3 volte + nextWait = (int)(rnd.Next(2000, 3000) * stdWait); // attendo step successivi... await Task.Delay(nextWait); } @@ -47,11 +47,11 @@ namespace EgwCoreLib.BlazorTest.Pages #region Private Fields private int currVal = 100; - private double expTimeMSec = 10000; + private int expTimeMSec = 10000; private int maxVal = 100; private int nextVal = 100; private Random rnd = new Random(); - private string titleMsg = "Test Progress"; + private string titleMsg = "SIM Progress"; #endregion Private Fields } diff --git a/EgwCoreLib.Razor/ProgressDisplay.razor b/EgwCoreLib.Razor/ProgressDisplay.razor index 7c2164d..988f338 100644 --- a/EgwCoreLib.Razor/ProgressDisplay.razor +++ b/EgwCoreLib.Razor/ProgressDisplay.razor @@ -1,29 +1,28 @@ -@*
-
-

loading data

- -
-
-*@ - -@if (IsLoading) +@if (isLoading) { -