Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DurationViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using CommunityToolkit.Mvvm.Input;
6using System.ComponentModel;
7using System.Globalization;
8using System.Text;
9using Waher.Content;
10
12{
16 public partial class DurationViewModel : XmppViewModel
17 {
18 private bool skipEvaluations = false;
19
24 public DurationViewModel(DurationNavigationArgs? Args)
25 : base()
26 {
27 this.Entry = Args?.Entry;
28 }
29
31 public override async Task OnDisposeAsync()
32 {
33 this.EvaluateDuration();
34
35 await base.OnDisposeAsync();
36 }
37
38 #region Properties
39
43 [ObservableProperty]
44 private Waher.Content.Duration value;
45
49 [ObservableProperty]
50 [NotifyPropertyChangedFor(nameof(Value))]
51 [NotifyPropertyChangedFor(nameof(Years))]
52 [NotifyPropertyChangedFor(nameof(Months))]
53 [NotifyPropertyChangedFor(nameof(Days))]
54 [NotifyPropertyChangedFor(nameof(Hours))]
55 [NotifyPropertyChangedFor(nameof(Minutes))]
56 [NotifyPropertyChangedFor(nameof(Seconds))]
57 [NotifyPropertyChangedFor(nameof(IsNegativeDuration))]
58 private CompositeEntry? entry;
59
61 protected override void OnPropertyChanged(PropertyChangedEventArgs e)
62 {
63 base.OnPropertyChanged(e);
64
65 switch (e.PropertyName)
66 {
67 case nameof(this.IsNegativeDuration):
68 case nameof(this.Years):
69 case nameof(this.Months):
70 case nameof(this.Days):
71 case nameof(this.Hours):
72 case nameof(this.Minutes):
73 case nameof(this.Seconds):
74 this.EvaluateDuration();
75 break;
76
77 case nameof(this.Entry):
78 if ((this.Entry is not null) && (this.Entry.BindingContext is ParameterInfo ParameterInfo))
79 {
80 this.Value = ParameterInfo.DurationValue;
81 this.SplitDuration();
82 }
83 break;
84
85 case nameof(this.Value):
86 if ((this.Entry is not null) && (this.Entry.BindingContext is ParameterInfo ParameterInfo2))
87 ParameterInfo2.DurationValue = this.Value;
88 break;
89 }
90 }
91
95 [ObservableProperty]
96 private bool isNegativeDuration;
97
101 [ObservableProperty]
102 private string years = string.Empty;
103
107 [ObservableProperty]
108 private string months = string.Empty;
109
113 [ObservableProperty]
114 private string days = string.Empty;
115
119 [ObservableProperty]
120 private string hours = string.Empty;
121
125 [ObservableProperty]
126 private string minutes = string.Empty;
127
131 [ObservableProperty]
132 private string seconds = string.Empty;
133
137 [ObservableProperty]
138 private bool yearsOk = false;
139
143 [ObservableProperty]
144 private bool monthsOk = false;
145
149 [ObservableProperty]
150 private bool daysOk = false;
151
155 [ObservableProperty]
156 private bool hoursOk = false;
157
161 [ObservableProperty]
162 private bool minutesOk = false;
163
167 [ObservableProperty]
168 private bool secondsOk = false;
169
170 #endregion
171
172 #region Commands
173
174 [RelayCommand]
175 private void PlusMinus()
176 {
177 this.IsNegativeDuration = !this.IsNegativeDuration;
178 }
179
183 public void SplitDuration()
184 {
185 this.skipEvaluations = true;
186
187 this.IsNegativeDuration = this.Value.Negation;
188 this.Years = ComponentToString(this.Value.Years);
189 this.Months = ComponentToString(this.Value.Months);
190 this.Days = ComponentToString(this.Value.Days);
191 this.Hours = ComponentToString(this.Value.Hours);
192 this.Minutes = ComponentToString(this.Value.Minutes);
193 this.Seconds = ComponentToString(this.Value.Seconds);
194
195 this.skipEvaluations = false;
196 }
197
198 private static string ComponentToString(int Value)
199 {
200 if (Value == 0)
201 return string.Empty;
202 else
203 return Value.ToString(CultureInfo.InvariantCulture);
204 }
205
206 private static string ComponentToString(double Value)
207 {
208 if (Value == 0)
209 return string.Empty;
210 else
211 return Value.ToString(CultureInfo.InvariantCulture);
212 }
213
217 public void EvaluateDuration()
218 {
219 if (this.skipEvaluations)
220 return;
221
222 bool IsZero = true;
223 StringBuilder sb = new();
224
225 if (this.IsNegativeDuration)
226 sb.Append("-P");
227 else
228 sb.Append('P');
229
230 if (!string.IsNullOrEmpty(this.Years))
231 {
232 IsZero = false;
233 sb.Append(this.Years);
234 sb.Append('Y');
235 this.YearsOk = int.TryParse(this.Years, out _);
236 }
237 else
238 this.YearsOk = true;
239
240 if (!string.IsNullOrEmpty(this.Months))
241 {
242 IsZero = false;
243 sb.Append(this.Months);
244 sb.Append('M');
245 this.MonthsOk = int.TryParse(this.Months, out _);
246 }
247 else
248 this.MonthsOk = true;
249
250 if (!string.IsNullOrEmpty(this.Days))
251 {
252 IsZero = false;
253 sb.Append(this.Days);
254 sb.Append('D');
255 this.DaysOk = int.TryParse(this.Days, out _);
256 }
257 else
258 this.DaysOk = true;
259
260 bool IsHours = !string.IsNullOrEmpty(this.Hours);
261 bool IsMinutes = !string.IsNullOrEmpty(this.Minutes);
262 bool IsSeconds = !string.IsNullOrEmpty(this.Seconds);
263
264 if (IsHours || IsMinutes || IsSeconds)
265 {
266 IsZero = false;
267 sb.Append('T');
268
269 if (IsHours)
270 {
271 sb.Append(this.Hours);
272 sb.Append('H');
273 this.HoursOk = int.TryParse(this.Hours, out _);
274 }
275 else
276 this.HoursOk = true;
277
278 if (IsMinutes)
279 {
280 sb.Append(this.Minutes);
281 sb.Append('M');
282 this.MinutesOk = int.TryParse(this.Minutes, out _);
283 }
284 else
285 this.MinutesOk = true;
286
287 if (IsSeconds)
288 {
289 sb.Append(this.Seconds);
290 sb.Append('S');
291 this.SecondsOk = CommonTypes.TryParse(this.Seconds, out double _);
292 }
293 else
294 this.SecondsOk = true;
295 }
296 else
297 {
298 this.HoursOk = true;
299 this.MinutesOk = true;
300 this.SecondsOk = true;
301 }
302
303 if (IsZero)
304 sb.Append("PT0S");
305
306 if (Waher.Content.Duration.TryParse(sb.ToString(), out Waher.Content.Duration Result))
307 this.Value = Result;
308 }
309
310 #endregion
311 }
312}
A customizable entry control that allows setting views on the left and right sides of the entry.
The view model to bind to for when displaying the duration.
DurationViewModel(DurationNavigationArgs? Args)
Creates an instance of the DurationViewModel class.
void SplitDuration()
Split the current duration value into components.
void EvaluateDuration()
Evaluates the current duration.
override void OnPropertyChanged(PropertyChangedEventArgs e)
override async Task OnDisposeAsync()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
A view model that holds the XMPP state.
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:48
Definition: ImplTypes.g.cs:58
Definition: App.xaml.cs:4
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:14
static bool TryParse(string s, out Duration Result)
Tries to parse a duration value.
Definition: Duration.cs:86