Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WrappedDatePicker.cs
1using System;
2using Microsoft.Maui.Controls;
4
6{
11 public class WrappedDatePicker : DatePicker, IDatePicker
12 {
13 private bool settingDate;
14
15 // Safe bounds used to avoid DateTimeOffset validation exceptions (year 0 / >10000 after offset math).
16 private static readonly DateTime SafeMinDate = new DateTime(1900, 1, 1);
17 private static readonly DateTime SafeMaxDate = new DateTime(2100, 12, 31);
18
19 // NOTE: IDatePicker.Date is now DateTime?
20 DateTime? IDatePicker.Date
21 {
22 get => this.Date;
23 set
24 {
25 if (this.settingDate)
26 return;
27
28 // Allow "no selection"
29 if (value is null)
30 {
31 if (this.Date is null)
32 return; // no change
33
34 this.settingDate = true;
35 try
36 {
37 this.Date = null;
38 }
39 finally
40 {
41 this.settingDate = false;
42 }
43
44 return;
45 }
46
47 // Non-null value -> sanitize & clamp
48 DateTime sanitized = value.Value.SanitizeForDatePicker(SafeMinDate, SafeMaxDate);
49
50 // If current date has same non-null value, skip
51 if (this.Date.HasValue && this.Date.Value == sanitized)
52 return;
53
54 this.settingDate = true;
55 try
56 {
57 // Ensure bounds before assigning.
58 if (!this.MinimumDate.HasValue || this.MinimumDate.Value < SafeMinDate)
59 this.MinimumDate = SafeMinDate;
60
61 if (!this.MaximumDate.HasValue || this.MaximumDate.Value > SafeMaxDate)
62 this.MaximumDate = SafeMaxDate;
63
64 if (sanitized < this.MinimumDate)
65 sanitized = this.MinimumDate.Value;
66 else if (sanitized > this.MaximumDate)
67 sanitized = this.MaximumDate.Value;
68
69 this.Date = sanitized; // nullable DateTime?
70 }
71 finally
72 {
73 this.settingDate = false;
74 }
75 }
76 }
77
81 protected override void OnHandlerChanged()
82 {
83 base.OnHandlerChanged();
84
85 // Clamp bounds once handler exists.
86 if (!this.MinimumDate.HasValue || this.MinimumDate == DateTime.MinValue || this.MinimumDate < SafeMinDate)
87 this.MinimumDate = SafeMinDate;
88
89 if (!this.MaximumDate.HasValue || this.MaximumDate == DateTime.MaxValue || this.MaximumDate > SafeMaxDate)
90 this.MaximumDate = SafeMaxDate;
91
92 // Clamp current date to bounds, if present.
93 if (this.Date is DateTime currentValue)
94 {
95 DateTime current = currentValue.SanitizeForDatePicker(SafeMinDate, SafeMaxDate);
96
97 if (current < this.MinimumDate)
98 current = this.MinimumDate.Value;
99 else if (current > this.MaximumDate)
100 current = this.MaximumDate.Value;
101
102 if (current != currentValue)
103 this.Date = current;
104 }
105 }
106 }
107}
DatePicker wrapper preventing redundant reentrant Date updates and guarding against invalid extreme d...
override void OnHandlerChanged()
When handler is created, normalize min/max to safe values to prevent implicit DateTimeOffset construc...