Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PetitionContractViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using CommunityToolkit.Mvvm.Input;
8using System.Collections.ObjectModel;
10
12{
17 {
18 private readonly string? requestorFullJid;
19 private readonly string? petitionId;
20 private readonly PhotosLoader photosLoader;
21
27 {
28 this.Photos = [];
29 this.photosLoader = new PhotosLoader(this.Photos);
30
31 if (Args is not null)
32 {
33 this.RequestorIdentity = Args.RequestorIdentity;
34 this.requestorFullJid = Args.RequestorFullJid;
35 this.RequestedContract = Args.RequestedContract;
36 this.petitionId = Args.PetitionId;
37 this.Purpose = Args.Purpose;
38 }
39 }
40
42 protected override async Task OnInitialize()
43 {
44 await base.OnInitialize();
45
46 this.AssignProperties();
47
48 if (this.RequestorIdentity?.Attachments is not null)
49 this.LoadPhotos();
50 }
51
52 private async void LoadPhotos()
53 {
54 try
55 {
56 if (this.RequestorIdentity?.Attachments is not null)
57 {
58 Photo? First = await this.photosLoader.LoadPhotos(this.RequestorIdentity.Attachments, SignWith.LatestApprovedId);
59
60 this.FirstPhotoSource = First?.Source;
61 this.FirstPhotoRotation = First?.Rotation ?? 0;
62 }
63 }
64 catch (Exception ex)
65 {
66 ServiceRef.LogService.LogException(ex);
67 }
68 }
69
71 protected override async Task OnDispose()
72 {
73 this.photosLoader.CancelLoadPhotos();
74
75 await base.OnDispose();
76 }
77
81 public ObservableCollection<Photo> Photos { get; }
82
86 public Contract? RequestedContract { get; private set; }
87
91 public LegalIdentity? RequestorIdentity { get; private set; }
92
96 [RelayCommand]
97 private async Task Accept()
98 {
99 if (!await App.AuthenticateUser(AuthenticationPurpose.AcceptPetitionRequest, true))
100 return;
101
102 bool succeeded = await ServiceRef.NetworkService.TryRequest(() => ServiceRef.XmppService.SendPetitionContractResponse(
103 this.RequestedContract!.ContractId, this.petitionId!, this.requestorFullJid!, true));
104
105 if (succeeded)
106 await this.GoBack();
107 }
108
112 [RelayCommand]
113 private async Task Decline()
114 {
115 bool succeeded = await ServiceRef.NetworkService.TryRequest(() => ServiceRef.XmppService.SendPetitionContractResponse(
116 this.RequestedContract!.ContractId, this.petitionId!, this.requestorFullJid!, false));
117
118 if (succeeded)
119 await this.GoBack();
120 }
121
125 [RelayCommand]
126 private async Task Ignore()
127 {
128 await this.GoBack();
129 }
130
131 // Full name of requesting entity.
132 public string FullName => ContactInfo.GetFullName(this.FirstName, this.MiddleNames, this.LastNames);
133
134 #region Properties
135
139 [ObservableProperty]
140 private DateTime? created;
141
145 [ObservableProperty]
146 private DateTime? updated;
147
151 [ObservableProperty]
152 private DateTime? expires;
153
157 [ObservableProperty]
158 private string? legalId;
159
163 [ObservableProperty]
164 private string? networkId;
165
169 [ObservableProperty]
170 private IdentityState? state;
171
175 [ObservableProperty]
176 private DateTime? from;
177
181 [ObservableProperty]
182 private DateTime? to;
183
187 [ObservableProperty]
188 [NotifyPropertyChangedFor(nameof(FullName))]
189 private string? firstName;
190
194 [ObservableProperty]
195 [NotifyPropertyChangedFor(nameof(FullName))]
196 private string? middleNames;
197
201 [ObservableProperty]
202 [NotifyPropertyChangedFor(nameof(FullName))]
203 private string? lastNames;
204
208 [ObservableProperty]
209 private string? personalNumber;
210
214 [ObservableProperty]
215 private string? address;
216
220 [ObservableProperty]
221 private string? address2;
222
226 [ObservableProperty]
227 private string? zipCode;
228
232 [ObservableProperty]
233 private string? area;
234
238 [ObservableProperty]
239 private string? city;
240
244 [ObservableProperty]
245 private string? region;
246
250 [ObservableProperty]
251 private string? countryCode;
252
256 [ObservableProperty]
257 private string? nationalityCode;
258
262 [ObservableProperty]
263 private string? gender;
264
268 [ObservableProperty]
269 private DateTime? birthDate;
270
274 [ObservableProperty]
275 private string? orgName;
276
280 [ObservableProperty]
281 private string? orgNumber;
282
286 [ObservableProperty]
287 private string? orgDepartment;
288
292 [ObservableProperty]
293 private string? orgRole;
294
298 [ObservableProperty]
299 private string? orgAddress;
300
304 [ObservableProperty]
305 private string? orgAddress2;
306
310 [ObservableProperty]
311 private string? orgZipCode;
312
316 [ObservableProperty]
317 private string? orgArea;
318
322 [ObservableProperty]
323 private string? orgCity;
324
328 [ObservableProperty]
329 private string? orgRegion;
330
334 [ObservableProperty]
335 private string? orgCountryCode;
336
340 [ObservableProperty]
341 private bool hasOrg;
342
346 [ObservableProperty]
347 private bool hasPhotos;
348
352 [ObservableProperty]
353 private string? phoneNr;
354
358 [ObservableProperty]
359 private string? eMail;
360
364 [ObservableProperty]
365 private string? deviceId;
366
370 [ObservableProperty]
371 private bool isApproved;
372
376 [ObservableProperty]
377 private string? purpose;
378
382 [ObservableProperty]
383 private ImageSource? firstPhotoSource;
384
388 [ObservableProperty]
389 private int? firstPhotoRotation;
390
391 #endregion
392
393 private void AssignProperties()
394 {
395 if (this.RequestorIdentity is not null)
396 {
397 this.Created = this.RequestorIdentity.Created;
398 this.Updated = this.RequestorIdentity.Updated.GetDateOrNullIfMinValue();
399 this.Expires = this.RequestorIdentity.To.GetDateOrNullIfMinValue();
400 this.LegalId = this.RequestorIdentity.Id;
401 this.NetworkId = this.RequestorIdentity.GetJid();
402 this.State = this.RequestorIdentity.State;
403 this.From = this.RequestorIdentity.From.GetDateOrNullIfMinValue();
404 this.To = this.RequestorIdentity.To.GetDateOrNullIfMinValue();
406 this.MiddleNames = this.RequestorIdentity[Constants.XmppProperties.MiddleNames];
408 this.PersonalNumber = this.RequestorIdentity[Constants.XmppProperties.PersonalNumber];
415 this.CountryCode = this.RequestorIdentity[Constants.XmppProperties.Country];
416 this.NationalityCode = this.RequestorIdentity[Constants.XmppProperties.Nationality];
418
419 string BirthDayStr = this.RequestorIdentity[Constants.XmppProperties.BirthDay];
420 string BirthMonthStr = this.RequestorIdentity[Constants.XmppProperties.BirthMonth];
421 string BirthYearStr = this.RequestorIdentity[Constants.XmppProperties.BirthYear];
422
423 if (!string.IsNullOrEmpty(BirthDayStr) && int.TryParse(BirthDayStr, out int BirthDay) &&
424 !string.IsNullOrEmpty(BirthMonthStr) && int.TryParse(BirthMonthStr, out int BirthMonth) &&
425 !string.IsNullOrEmpty(BirthYearStr) && int.TryParse(BirthYearStr, out int BirthYear))
426 {
427 try
428 {
429 this.BirthDate = new DateTime(BirthYear, BirthMonth, BirthDay);
430 }
431 catch (Exception ex)
432 {
433 ServiceRef.LogService.LogException(ex);
434 this.BirthDate = null;
435 }
436 }
437
440 this.OrgDepartment = this.RequestorIdentity[Constants.XmppProperties.OrgDepartment];
443 this.OrgAddress2 = this.RequestorIdentity[Constants.XmppProperties.OrgAddress2];
448 this.OrgCountryCode = this.RequestorIdentity[Constants.XmppProperties.OrgCountry];
449 this.HasOrg =
450 !string.IsNullOrEmpty(this.OrgName) ||
451 !string.IsNullOrEmpty(this.OrgNumber) ||
452 !string.IsNullOrEmpty(this.OrgDepartment) ||
453 !string.IsNullOrEmpty(this.OrgRole) ||
454 !string.IsNullOrEmpty(this.OrgAddress) ||
455 !string.IsNullOrEmpty(this.OrgAddress2) ||
456 !string.IsNullOrEmpty(this.OrgZipCode) ||
457 !string.IsNullOrEmpty(this.OrgArea) ||
458 !string.IsNullOrEmpty(this.OrgCity) ||
459 !string.IsNullOrEmpty(this.OrgRegion) ||
460 !string.IsNullOrEmpty(this.OrgCountryCode);
461 this.HasPhotos = this.Photos.Count > 0;
462 this.PhoneNr = this.RequestorIdentity[Constants.XmppProperties.Phone];
465 this.IsApproved = this.RequestorIdentity.State == IdentityState.Approved;
466 }
467 else
468 {
469 this.Created = DateTime.MinValue;
470 this.Updated = DateTime.MinValue;
471 this.LegalId = Constants.NotAvailableValue;
472 this.State = IdentityState.Compromised;
473 this.From = null;
474 this.To = null;
475 this.FirstName = Constants.NotAvailableValue;
476 this.MiddleNames = Constants.NotAvailableValue;
477 this.LastNames = Constants.NotAvailableValue;
478 this.PersonalNumber = Constants.NotAvailableValue;
479 this.Address = Constants.NotAvailableValue;
480 this.Address2 = Constants.NotAvailableValue;
481 this.ZipCode = Constants.NotAvailableValue;
482 this.Area = Constants.NotAvailableValue;
483 this.City = Constants.NotAvailableValue;
484 this.Region = Constants.NotAvailableValue;
485 this.CountryCode = Constants.NotAvailableValue;
486 this.NationalityCode = string.Empty;
487 this.Gender = string.Empty;
488 this.BirthDate = null;
489 this.OrgName = Constants.NotAvailableValue;
490 this.OrgNumber = Constants.NotAvailableValue;
491 this.OrgDepartment = Constants.NotAvailableValue;
492 this.OrgRole = Constants.NotAvailableValue;
493 this.OrgAddress = Constants.NotAvailableValue;
494 this.OrgAddress2 = Constants.NotAvailableValue;
495 this.OrgZipCode = Constants.NotAvailableValue;
496 this.OrgArea = Constants.NotAvailableValue;
497 this.OrgCity = Constants.NotAvailableValue;
498 this.OrgRegion = Constants.NotAvailableValue;
499 this.OrgCountryCode = Constants.NotAvailableValue;
500 this.HasOrg = false;
501 this.HasPhotos = false;
502 this.PhoneNr = Constants.NotAvailableValue;
503 this.EMail = Constants.NotAvailableValue;
504 this.DeviceId = Constants.NotAvailableValue;
505 this.IsApproved = false;
506 }
507 }
508
512 [RelayCommand]
513 private async Task Copy(object Item)
514 {
515 try
516 {
517 if (Item is string Label)
518 {
519 if (Label == this.LegalId)
520 {
521 await Clipboard.SetTextAsync(Constants.UriSchemes.IotId + ":" + this.LegalId);
522 await ServiceRef.UiService.DisplayAlert(
523 ServiceRef.Localizer[nameof(AppResources.SuccessTitle)],
524 ServiceRef.Localizer[nameof(AppResources.IdCopiedSuccessfully)]);
525 }
526 else
527 {
528 await Clipboard.SetTextAsync(Label);
529 await ServiceRef.UiService.DisplayAlert(
530 ServiceRef.Localizer[nameof(AppResources.SuccessTitle)],
531 ServiceRef.Localizer[nameof(AppResources.TagValueCopiedToClipboard)]);
532 }
533 }
534 }
535 catch (Exception ex)
536 {
537 ServiceRef.LogService.LogException(ex);
538 await ServiceRef.UiService.DisplayException(ex);
539 }
540 }
541 }
542}
The Application class, representing an instance of the Neuro-Access app.
Definition: App.xaml.cs:69
static Task< bool > AuthenticateUser(AuthenticationPurpose Purpose, bool Force=false)
Authenticates the user using the configured authentication method.
Definition: App.xaml.cs:981
const string IotId
The IoT ID URI Scheme (iotid)
Definition: Constants.cs:99
const string PersonalNumber
Personal number
Definition: Constants.cs:284
const string OrgAddress2
Organization Address line 2
Definition: Constants.cs:364
const string OrgArea
Organization Area
Definition: Constants.cs:369
const string OrgRegion
Organization Region
Definition: Constants.cs:384
const string Nationality
Nationality
Definition: Constants.cs:324
const string BirthYear
Birth Year
Definition: Constants.cs:344
const string OrgCity
Organization City
Definition: Constants.cs:374
const string OrgRole
Organization Role
Definition: Constants.cs:399
const string Phone
Phone number
Definition: Constants.cs:414
const string EMail
e-Mail address
Definition: Constants.cs:419
const string OrgZipCode
Organization Zip Code
Definition: Constants.cs:379
const string MiddleNames
Middle names
Definition: Constants.cs:274
const string OrgCountry
Organization Country
Definition: Constants.cs:389
const string OrgDepartment
Organization Department
Definition: Constants.cs:394
const string Address2
Address line 2
Definition: Constants.cs:294
const string OrgAddress
Organization Address line 1
Definition: Constants.cs:359
const string Address
Address line 1
Definition: Constants.cs:289
const string LastNames
Last names
Definition: Constants.cs:279
const string OrgNumber
Organization number
Definition: Constants.cs:354
const string BirthMonth
Birth Month
Definition: Constants.cs:339
const string FirstName
First name
Definition: Constants.cs:269
const string OrgName
Organization name
Definition: Constants.cs:349
A set of never changing property constants and helpful values.
Definition: Constants.cs:7
const string NotAvailableValue
A generic "no value available" string.
Definition: Constants.cs:11
Contains information about a contact.
Definition: ContactInfo.cs:21
static string GetFullName(LegalIdentity? Identity)
Gets the full name of a person.
Definition: ContactInfo.cs:577
Base class that references services in the app.
Definition: ServiceRef.cs:31
static ILogService LogService
Log service.
Definition: ServiceRef.cs:91
static INetworkService NetworkService
Network service.
Definition: ServiceRef.cs:103
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
static IStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:235
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:67
A base class for all view models, inheriting from the BindableObject. NOTE: using this class requir...
virtual async Task GoBack()
Method called when user wants to navigate to the previous screen.
Holds navigation parameters specific to views displaying a contract petition request.
The view model to bind to when displaying petitioning of a contract in a view or page.
ObservableCollection< Photo > Photos
The list of photos related to the contract being petitioned.
override async Task OnDispose()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
PetitionContractViewModel(PetitionContractNavigationArgs? Args)
Creates a new instance of the PetitionContractViewModel class.
override async Task OnInitialize()
Method called when view is initialized for the first time. Use this method to implement registration ...
Contains the definition of a contract
Definition: Contract.cs:22
class Photo(byte[] Binary, int Rotation)
Class containing information about a photo.
Definition: Photo.cs:8
AuthenticationPurpose
Purpose for requesting the user to authenticate itself.
IdentityState
Lists recognized legal identity states.
SignWith
Options on what keys to use when signing data.
Definition: Enumerations.cs:84