From b344b1fd0df41c3d6488c6f882b431494cc1d861 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 18 May 2021 18:05:55 +0200 Subject: [PATCH] Aggiunta preliminare componente selezione filtro --- MP.Stats/Components/InputDateTime.cs | 119 +++++++++++++++++++ MP.Stats/Components/SelectionFilter.razor | 135 ++++++++++++++++++++++ MP.Stats/Data/SelectData.cs | 51 ++++++++ 3 files changed, 305 insertions(+) create mode 100644 MP.Stats/Components/InputDateTime.cs create mode 100644 MP.Stats/Components/SelectionFilter.razor create mode 100644 MP.Stats/Data/SelectData.cs diff --git a/MP.Stats/Components/InputDateTime.cs b/MP.Stats/Components/InputDateTime.cs new file mode 100644 index 00000000..2aa787fd --- /dev/null +++ b/MP.Stats/Components/InputDateTime.cs @@ -0,0 +1,119 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components.Rendering; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading.Tasks; + +namespace MP.Stats.Components +{ + public class InputDateTime : InputDate + { + #region Private Fields + + private const string DateFormat = "yyyy-MM-ddTHH:mm"; + + #endregion Private Fields + + #region Private Methods + + private static bool TryParseDateTime(string value, out TValue result) + { + var success = BindConverter.TryConvertToDateTime(value, CultureInfo.InvariantCulture, DateFormat, out var parsedValue); + if (success) + { + result = (TValue)(object)parsedValue; + return true; + } + else + { + result = default; + return false; + } + } + + private static bool TryParseDateTimeOffset(string value, out TValue result) + { + var success = BindConverter.TryConvertToDateTimeOffset(value, CultureInfo.InvariantCulture, DateFormat, out var parsedValue); + if (success) + { + result = (TValue)(object)parsedValue; + return true; + } + else + { + result = default; + return false; + } + } + + #endregion Private Methods + + #region Protected Methods + + /// + protected override void BuildRenderTree(RenderTreeBuilder builder) + { + builder.OpenElement(0, "input"); + builder.AddMultipleAttributes(1, AdditionalAttributes); + builder.AddAttribute(2, "type", "datetime-local"); + builder.AddAttribute(3, "class", CssClass); + builder.AddAttribute(4, "value", BindConverter.FormatValue(CurrentValueAsString)); + builder.AddAttribute(5, "onchange", EventCallback.Factory.CreateBinder(this, __value => CurrentValueAsString = __value, CurrentValueAsString)); + builder.CloseElement(); + } + + /// + protected override string FormatValueAsString(TValue value) + { + switch (value) + { + case DateTime dateTimeValue: + return BindConverter.FormatValue(dateTimeValue, DateFormat, CultureInfo.InvariantCulture); + + case DateTimeOffset dateTimeOffsetValue: + return BindConverter.FormatValue(dateTimeOffsetValue, DateFormat, CultureInfo.InvariantCulture); + + default: + return string.Empty; // Handles null for Nullable, etc. + } + } + + /// + protected override bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage) + { + // Unwrap nullable types. We don't have to deal with receiving empty values for nullable + // types here, because the underlying InputBase already covers that. + var targetType = Nullable.GetUnderlyingType(typeof(TValue)) ?? typeof(TValue); + + bool success; + if (targetType == typeof(DateTime)) + { + success = TryParseDateTime(value, out result); + } + else if (targetType == typeof(DateTimeOffset)) + { + success = TryParseDateTimeOffset(value, out result); + } + else + { + throw new InvalidOperationException($"The type '{targetType}' is not a supported date type."); + } + + if (success) + { + validationErrorMessage = null; + return true; + } + else + { + validationErrorMessage = string.Format(ParsingErrorMessage, FieldIdentifier.FieldName); + return false; + } + } + + #endregion Protected Methods + } +} \ No newline at end of file diff --git a/MP.Stats/Components/SelectionFilter.razor b/MP.Stats/Components/SelectionFilter.razor new file mode 100644 index 00000000..d88bd68b --- /dev/null +++ b/MP.Stats/Components/SelectionFilter.razor @@ -0,0 +1,135 @@ +@using MP.Stats.Components +@using MP.Stats.Data + + +
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+ + +
+
+
+
+ +@code { + + protected DateTime DateStart + { + get + { + return SelFilter.DateStart; + } + set + { + bool doReport = (!SelFilter.DateStart.Equals(value)); + SelFilter.DateStart = value; + if (doReport) + { + reportChange(); + } + } + } + protected DateTime DateEnd + { + get + { + return SelFilter.DateEnd; + } + set + { + bool doReport = (!SelFilter.DateEnd.Equals(value)); + SelFilter.DateEnd = value; + if (doReport) + { + reportChange(); + } + } + } + + protected string IdxMacchina + { + get + { + return SelFilter.IdxMacchina; + } + set + { + bool doReport = (!SelFilter.IdxMacchina.Equals(value)); + SelFilter.IdxMacchina = value; + if (doReport) + { + reportChange(); + } + } + } + protected int IdxODL + { + get + { + return SelFilter.IdxOdl; + } + set + { + bool doReport = (!SelFilter.IdxOdl.Equals(value)); + SelFilter.IdxOdl = value; + if (doReport) + { + reportChange(); + } + } + } + + protected string CodArticolo + { + get + { + return SelFilter.CodArticolo; + } + set + { + bool doReport = (!SelFilter.CodArticolo.Equals(value)); + SelFilter.CodArticolo = value; + if (doReport) + { + reportChange(); + } + } + } + + [Parameter] + public SelectData SelFilter { get; set; } + + [Parameter] + public EventCallback filterChanged { get; set; } + + private void reportChange() + { + filterChanged.InvokeAsync(SelFilter); + } +} \ No newline at end of file diff --git a/MP.Stats/Data/SelectData.cs b/MP.Stats/Data/SelectData.cs new file mode 100644 index 00000000..05be9c99 --- /dev/null +++ b/MP.Stats/Data/SelectData.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MP.Stats.Data +{ + public class SelectData + { + #region Public Properties + + public string CodArticolo { get; set; } = "*"; + public DateTime DateEnd { get; set; } = DateTime.Now.AddMinutes(1); + public DateTime DateStart { get; set; } = DateTime.Now.AddDays(-7); + public string IdxMacchina { get; set; } = "*"; + public int IdxOdl { get; set; } = -999; + public string KeyRichiesta { get; set; } = "*"; + + #endregion Public Properties + + #region Public Methods + + public override bool Equals(object obj) + { + if (!(obj is SelectData item)) + return false; + + if (CodArticolo != item.CodArticolo) + return false; + if (DateEnd != item.DateEnd) + return false; + if (DateStart != item.DateStart) + return false; + if (IdxMacchina != item.IdxMacchina) + return false; + if (IdxOdl != item.IdxOdl) + return false; + if (KeyRichiesta != item.KeyRichiesta) + return false; + + return true; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } + + #endregion Public Methods + } +} \ No newline at end of file