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;
5using System.ComponentModel;
6using System.Globalization;
7using System.Text;
8using Waher.Content;
9
11{
15 public partial class DurationViewModel : XmppViewModel
16 {
17 private bool skipEvaluations = false;
18
23 public DurationViewModel(DurationNavigationArgs? Args)
24 : base()
25 {
26 this.Entry = Args?.Entry;
27 }
28
30 protected override async Task OnDispose()
31 {
32 this.EvaluateDuration();
33
34 await base.OnDispose();
35 }
36
37 #region Properties
38
42 [ObservableProperty]
43 private Waher.Content.Duration value;
44
48 [ObservableProperty]
49 [NotifyPropertyChangedFor(nameof(Value))]
50 [NotifyPropertyChangedFor(nameof(Years))]
51 [NotifyPropertyChangedFor(nameof(Months))]
52 [NotifyPropertyChangedFor(nameof(Days))]
53 [NotifyPropertyChangedFor(nameof(Hours))]
54 [NotifyPropertyChangedFor(nameof(Minutes))]
55 [NotifyPropertyChangedFor(nameof(Seconds))]
56 [NotifyPropertyChangedFor(nameof(IsNegativeDuration))]
57 private Entry? entry;
58
60 protected override void OnPropertyChanged(PropertyChangedEventArgs e)
61 {
62 base.OnPropertyChanged(e);
63
64 switch (e.PropertyName)
65 {
66 case nameof(this.IsNegativeDuration):
67 case nameof(this.Years):
68 case nameof(this.Months):
69 case nameof(this.Days):
70 case nameof(this.Hours):
71 case nameof(this.Minutes):
72 case nameof(this.Seconds):
73 this.EvaluateDuration();
74 break;
75
76 case nameof(this.Entry):
77 if ((this.Entry is not null) && (this.Entry.BindingContext is ParameterInfo ParameterInfo))
78 {
79 this.Value = ParameterInfo.DurationValue;
80 this.SplitDuration();
81 }
82 break;
83
84 case nameof(this.Value):
85 if ((this.Entry is not null) && (this.Entry.BindingContext is ParameterInfo ParameterInfo2))
86 ParameterInfo2.DurationValue = this.Value;
87 break;
88 }
89 }
90
94 [ObservableProperty]
95 private bool isNegativeDuration;
96
100 [ObservableProperty]
101 private string years = string.Empty;
102
106 [ObservableProperty]
107 private string months = string.Empty;
108
112 [ObservableProperty]
113 private string days = string.Empty;
114
118 [ObservableProperty]
119 private string hours = string.Empty;
120
124 [ObservableProperty]
125 private string minutes = string.Empty;
126
130 [ObservableProperty]
131 private string seconds = string.Empty;
132
136 [ObservableProperty]
137 private bool yearsOk = false;
138
142 [ObservableProperty]
143 private bool monthsOk = false;
144
148 [ObservableProperty]
149 private bool daysOk = false;
150
154 [ObservableProperty]
155 private bool hoursOk = false;
156
160 [ObservableProperty]
161 private bool minutesOk = false;
162
166 [ObservableProperty]
167 private bool secondsOk = false;
168
169 #endregion
170
171 #region Commands
172
173 [RelayCommand]
174 private void PlusMinus()
175 {
176 this.IsNegativeDuration = !this.IsNegativeDuration;
177 }
178
182 public void SplitDuration()
183 {
184 this.skipEvaluations = true;
185
186 this.IsNegativeDuration = this.Value.Negation;
187 this.Years = ComponentToString(this.Value.Years);
188 this.Months = ComponentToString(this.Value.Months);
189 this.Days = ComponentToString(this.Value.Days);
190 this.Hours = ComponentToString(this.Value.Hours);
191 this.Minutes = ComponentToString(this.Value.Minutes);
192 this.Seconds = ComponentToString(this.Value.Seconds);
193
194 this.skipEvaluations = false;
195 }
196
197 private static string ComponentToString(int Value)
198 {
199 if (Value == 0)
200 return string.Empty;
201 else
202 return Value.ToString(CultureInfo.InvariantCulture);
203 }
204
205 private static string ComponentToString(double Value)
206 {
207 if (Value == 0)
208 return string.Empty;
209 else
210 return Value.ToString(CultureInfo.InvariantCulture);
211 }
212
216 public void EvaluateDuration()
217 {
218 if (this.skipEvaluations)
219 return;
220
221 bool IsZero = true;
222 StringBuilder sb = new();
223
224 if (this.IsNegativeDuration)
225 sb.Append("-P");
226 else
227 sb.Append('P');
228
229 if (!string.IsNullOrEmpty(this.Years))
230 {
231 IsZero = false;
232 sb.Append(this.Years);
233 sb.Append('Y');
234 this.YearsOk = int.TryParse(this.Years, out _);
235 }
236 else
237 this.YearsOk = true;
238
239 if (!string.IsNullOrEmpty(this.Months))
240 {
241 IsZero = false;
242 sb.Append(this.Months);
243 sb.Append('M');
244 this.MonthsOk = int.TryParse(this.Months, out _);
245 }
246 else
247 this.MonthsOk = true;
248
249 if (!string.IsNullOrEmpty(this.Days))
250 {
251 IsZero = false;
252 sb.Append(this.Days);
253 sb.Append('D');
254 this.DaysOk = int.TryParse(this.Days, out _);
255 }
256 else
257 this.DaysOk = true;
258
259 bool IsHours = !string.IsNullOrEmpty(this.Hours);
260 bool IsMinutes = !string.IsNullOrEmpty(this.Minutes);
261 bool IsSeconds = !string.IsNullOrEmpty(this.Seconds);
262
263 if (IsHours || IsMinutes || IsSeconds)
264 {
265 IsZero = false;
266 sb.Append('T');
267
268 if (IsHours)
269 {
270 sb.Append(this.Hours);
271 sb.Append('H');
272 this.HoursOk = int.TryParse(this.Hours, out _);
273 }
274 else
275 this.HoursOk = true;
276
277 if (IsMinutes)
278 {
279 sb.Append(this.Minutes);
280 sb.Append('M');
281 this.MinutesOk = int.TryParse(this.Minutes, out _);
282 }
283 else
284 this.MinutesOk = true;
285
286 if (IsSeconds)
287 {
288 sb.Append(this.Seconds);
289 sb.Append('S');
290 this.SecondsOk = CommonTypes.TryParse(this.Seconds, out double _);
291 }
292 else
293 this.SecondsOk = true;
294 }
295 else
296 {
297 this.HoursOk = true;
298 this.MinutesOk = true;
299 this.SecondsOk = true;
300 }
301
302 if (IsZero)
303 sb.Append("PT0S");
304
305 if (Waher.Content.Duration.TryParse(sb.ToString(), out Waher.Content.Duration Result))
306 this.Value = Result;
307 }
308
309 #endregion
310 }
311}
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 OnDispose()
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:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Definition: App.xaml.cs:4
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:13
static bool TryParse(string s, out Duration Result)
Tries to parse a duration value.
Definition: Duration.cs:85