From 31244f325b519cd6e117231a2abc962484446c85 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Wed, 28 Jun 2023 14:46:02 +0200 Subject: [PATCH 1/2] Aggiunta componente sel periodo --- EgwCoreLib.BlazorTest/Pages/TestCompo.razor | 30 ++++++ EgwCoreLib.Razor/CalendarMonth.razor.cs | 22 ++-- EgwCoreLib.Razor/PeriodoSel.razor | 15 +++ EgwCoreLib.Razor/PeriodoSel.razor.cs | 93 ++++++++++++++++ EgwCoreLib.Utils/DateTimeExtensions.cs | 17 +++ EgwCoreLib.Utils/DtUtils.cs | 111 ++++++++++++++++++++ 6 files changed, 277 insertions(+), 11 deletions(-) create mode 100644 EgwCoreLib.BlazorTest/Pages/TestCompo.razor create mode 100644 EgwCoreLib.Razor/PeriodoSel.razor create mode 100644 EgwCoreLib.Razor/PeriodoSel.razor.cs create mode 100644 EgwCoreLib.Utils/DateTimeExtensions.cs create mode 100644 EgwCoreLib.Utils/DtUtils.cs diff --git a/EgwCoreLib.BlazorTest/Pages/TestCompo.razor b/EgwCoreLib.BlazorTest/Pages/TestCompo.razor new file mode 100644 index 0000000..43145cf --- /dev/null +++ b/EgwCoreLib.BlazorTest/Pages/TestCompo.razor @@ -0,0 +1,30 @@ +@page "/TestCompo" +@using EgwCoreLib.Utils; + +Test + +

TestCompo

+ +
+
+ @if (periodo != null) + { +
+ Periodo Selezionato: @($"{periodo.Inizio:yyyy/MM/dd}") → @($"{periodo.Fine:yyyy/MM/dd}") +
+ } +
+
+ +
+
+ +@code { + protected DtUtils.Periodo? periodo { get; set; } = null; + + protected async Task setPeriodo(DtUtils.Periodo newPeriodo) + { + await Task.Delay(1); + periodo = newPeriodo; + } +} diff --git a/EgwCoreLib.Razor/CalendarMonth.razor.cs b/EgwCoreLib.Razor/CalendarMonth.razor.cs index a1d6a70..90ee805 100644 --- a/EgwCoreLib.Razor/CalendarMonth.razor.cs +++ b/EgwCoreLib.Razor/CalendarMonth.razor.cs @@ -171,18 +171,18 @@ namespace EgwCoreLib.Razor private bool hasDot(DateTime dtCurr, string type) { bool answ = false; - if(EventList != null) + if (EventList != null) { - foreach(var item in EventList) + foreach (var item in EventList) { - if(item.Inizio.Date == dtCurr.Date && item.Type == type) + if (item.Inizio.Date == dtCurr.Date && item.Type == type) { answ = true; } } } return answ; - } + } private async Task clickDay(DateTime dtClick) { @@ -213,13 +213,13 @@ namespace EgwCoreLib.Razor private async Task addMonth() { - + await FixDate(); await MonthChanged.InvokeAsync(DtRif.AddMonths(1)); } private async Task subtractMonth() { - + await FixDate(); await MonthChanged.InvokeAsync(DtRif.AddMonths(-1)); } @@ -259,11 +259,11 @@ namespace EgwCoreLib.Razor //await reloadData(); } - /// - /// Sistemazione date x disegnare controlli - /// - /// - private async Task FixDate() + /// + /// Sistemazione date x disegnare controlli + /// + /// + private async Task FixDate() { await Task.Delay(1); // disegno sempre 6 righe = 42 gg diff --git a/EgwCoreLib.Razor/PeriodoSel.razor b/EgwCoreLib.Razor/PeriodoSel.razor new file mode 100644 index 0000000..d7effc7 --- /dev/null +++ b/EgwCoreLib.Razor/PeriodoSel.razor @@ -0,0 +1,15 @@ +@using EgwCoreLib.Utils; + +
+ Periodo + + + +
+ + diff --git a/EgwCoreLib.Razor/PeriodoSel.razor.cs b/EgwCoreLib.Razor/PeriodoSel.razor.cs new file mode 100644 index 0000000..8d4a46d --- /dev/null +++ b/EgwCoreLib.Razor/PeriodoSel.razor.cs @@ -0,0 +1,93 @@ +using EgwCoreLib.Utils; +using Microsoft.AspNetCore.Components; + +namespace EgwCoreLib.Razor +{ + public partial class PeriodoSel + { + #region Public Properties + + [Parameter] + public EventCallback E_PeriodoSel { get; set; } + + #endregion Public Properties + + #region Protected Properties + + protected DtUtils.Periodo CurrPeriodo + { + get => currPeriodo; + set + { + if (currPeriodo != value) + { + currPeriodo = value; + E_PeriodoSel.InvokeAsync(CurrPeriodo); + } + } + } + + protected DateTime Fine + { + get => CurrPeriodo.Fine; + set + { + if (CurrPeriodo.Fine != value) + { + CurrPeriodo.Fine = value; + perSelect = $"{DtUtils.PeriodSet.Period}"; + E_PeriodoSel.InvokeAsync(CurrPeriodo); + } + } + } + + protected DateTime Inizio + { + get => CurrPeriodo.Inizio; + set + { + if (CurrPeriodo.Inizio != value) + { + CurrPeriodo.Inizio = value; + perSelect = $"{DtUtils.PeriodSet.Period}"; + E_PeriodoSel.InvokeAsync(CurrPeriodo); + } + } + } + + protected List periodList { get => Enum.GetNames(typeof(DtUtils.PeriodSet)).ToList(); } + + protected string PerSelect + { + get => perSelect; + set + { + if (perSelect != value) + { + perSelect = value; + // effettuo preselezione... + Enum.TryParse(value, out DtUtils.PeriodSet newPer); + CurrPeriodo = new DtUtils.Periodo(newPer); + } + } + } + + #endregion Protected Properties + + #region Protected Methods + + protected override async Task OnInitializedAsync() + { + await E_PeriodoSel.InvokeAsync(CurrPeriodo); + } + + #endregion Protected Methods + + #region Private Properties + + private DtUtils.Periodo currPeriodo { get; set; } = new DtUtils.Periodo(); + private string perSelect { get; set; } = "Period"; + + #endregion Private Properties + } +} \ No newline at end of file diff --git a/EgwCoreLib.Utils/DateTimeExtensions.cs b/EgwCoreLib.Utils/DateTimeExtensions.cs new file mode 100644 index 0000000..6ad4288 --- /dev/null +++ b/EgwCoreLib.Utils/DateTimeExtensions.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EgwCoreLib.Utils +{ + public static class DateTimeExtensions + { + public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) + { + int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7; + return dt.AddDays(-1 * diff).Date; + } + } +} diff --git a/EgwCoreLib.Utils/DtUtils.cs b/EgwCoreLib.Utils/DtUtils.cs new file mode 100644 index 0000000..e3283ae --- /dev/null +++ b/EgwCoreLib.Utils/DtUtils.cs @@ -0,0 +1,111 @@ +namespace EgwCoreLib.Utils +{ + public class DtUtils + { + #region Public Enums + + public enum PeriodSet + { + Period = 0, + Today, + Yesterday, + LastMonth, + LastTrim, + LastWeek, + ThisMonth, + ThisTrim, + ThisWeek, + ThisYear + } + + #endregion Public Enums + + #region Public Classes + + public class Periodo + { + #region Public Constructors + + public Periodo() + { } + + public Periodo(DateTime inizio, DateTime fine) + { + this.Inizio = inizio; + this.Fine = fine; + } + + public Periodo(PeriodSet tipo) + { + int endMonth = 0; + DateTime oggi = DateTime.Today; + switch (tipo) + { + case DtUtils.PeriodSet.Today: + this.Inizio = oggi; + this.Fine = oggi.AddDays(1); + break; + + case DtUtils.PeriodSet.Yesterday: + this.Inizio = oggi.AddDays(-1); + this.Fine = oggi; + break; + + case DtUtils.PeriodSet.LastMonth: + this.Inizio = new DateTime(oggi.Year, oggi.Month, 1).AddMonths(-1); + this.Fine = new DateTime(oggi.Year, oggi.Month, 1); + break; + + case DtUtils.PeriodSet.LastTrim: + // calcolo trimestre da mese... + endMonth = (int)Math.Ceiling((float)oggi.Month / 3) * 3; + this.Fine = new DateTime(oggi.Year, endMonth, 1).AddMonths(-2); + this.Inizio = this.Fine.AddMonths(-3); + break; + + case DtUtils.PeriodSet.LastWeek: + this.Inizio = oggi.StartOfWeek(DayOfWeek.Monday).AddDays(-7); + this.Fine = this.Inizio.AddDays(7); + break; + + case DtUtils.PeriodSet.ThisMonth: + this.Inizio = new DateTime(oggi.Year, oggi.Month, 1); + this.Fine = new DateTime(oggi.Year, oggi.Month + 1, 1); + break; + + case DtUtils.PeriodSet.ThisTrim: + // calcolo trimestre da mese... + endMonth = (int)Math.Ceiling((float)oggi.Month / 3) * 3; + this.Fine = new DateTime(oggi.Year, endMonth, 1).AddMonths(1); + this.Inizio = this.Fine.AddMonths(-3); + break; + + case DtUtils.PeriodSet.ThisWeek: + this.Inizio = oggi.StartOfWeek(DayOfWeek.Monday); + this.Fine = this.Inizio.AddDays(7); + break; + + case DtUtils.PeriodSet.ThisYear: + this.Inizio = new DateTime(oggi.Year, 1, 1); + this.Fine = new DateTime(oggi.Year + 1, 1, 1); + break; + + case DtUtils.PeriodSet.Period: + default: + break; + } + } + + #endregion Public Constructors + + #region Public Properties + + public DateTime Fine { get; set; } = DateTime.Today.AddDays(+1); + public DateTime Inizio { get; set; } = DateTime.Today; + + #endregion Public Properties + } + + #endregion Public Classes + } +} \ No newline at end of file From be80f179d78e99a952b4b043e2a602760cfa65e4 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Wed, 28 Jun 2023 14:52:57 +0200 Subject: [PATCH 2/2] Aggiunto LastYear --- EgwCoreLib.Razor/PeriodoSel.razor | 1 + EgwCoreLib.Razor/PeriodoSel.razor.cs | 6 +++--- EgwCoreLib.Utils/DtUtils.cs | 10 ++++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/EgwCoreLib.Razor/PeriodoSel.razor b/EgwCoreLib.Razor/PeriodoSel.razor index d7effc7..21d22bc 100644 --- a/EgwCoreLib.Razor/PeriodoSel.razor +++ b/EgwCoreLib.Razor/PeriodoSel.razor @@ -10,6 +10,7 @@ } + diff --git a/EgwCoreLib.Razor/PeriodoSel.razor.cs b/EgwCoreLib.Razor/PeriodoSel.razor.cs index 8d4a46d..c39ee6c 100644 --- a/EgwCoreLib.Razor/PeriodoSel.razor.cs +++ b/EgwCoreLib.Razor/PeriodoSel.razor.cs @@ -35,7 +35,7 @@ namespace EgwCoreLib.Razor if (CurrPeriodo.Fine != value) { CurrPeriodo.Fine = value; - perSelect = $"{DtUtils.PeriodSet.Period}"; + perSelect = $"{DtUtils.PeriodSet.Select}"; E_PeriodoSel.InvokeAsync(CurrPeriodo); } } @@ -49,7 +49,7 @@ namespace EgwCoreLib.Razor if (CurrPeriodo.Inizio != value) { CurrPeriodo.Inizio = value; - perSelect = $"{DtUtils.PeriodSet.Period}"; + perSelect = $"{DtUtils.PeriodSet.Select}"; E_PeriodoSel.InvokeAsync(CurrPeriodo); } } @@ -86,7 +86,7 @@ namespace EgwCoreLib.Razor #region Private Properties private DtUtils.Periodo currPeriodo { get; set; } = new DtUtils.Periodo(); - private string perSelect { get; set; } = "Period"; + private string perSelect { get; set; } = "Select"; #endregion Private Properties } diff --git a/EgwCoreLib.Utils/DtUtils.cs b/EgwCoreLib.Utils/DtUtils.cs index e3283ae..3b8f2c6 100644 --- a/EgwCoreLib.Utils/DtUtils.cs +++ b/EgwCoreLib.Utils/DtUtils.cs @@ -6,12 +6,13 @@ public enum PeriodSet { - Period = 0, + Select = 0, Today, Yesterday, LastMonth, LastTrim, LastWeek, + LastYear, ThisMonth, ThisTrim, ThisWeek, @@ -63,6 +64,11 @@ this.Inizio = this.Fine.AddMonths(-3); break; + case DtUtils.PeriodSet.LastYear: + this.Inizio = new DateTime(oggi.Year-1, 1, 1); + this.Fine = new DateTime(oggi.Year , 1, 1); + break; + case DtUtils.PeriodSet.LastWeek: this.Inizio = oggi.StartOfWeek(DayOfWeek.Monday).AddDays(-7); this.Fine = this.Inizio.AddDays(7); @@ -90,7 +96,7 @@ this.Fine = new DateTime(oggi.Year + 1, 1, 1); break; - case DtUtils.PeriodSet.Period: + case DtUtils.PeriodSet.Select: default: break; }