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;
9using System.Collections.ObjectModel;
11
13{
18 {
19 private readonly IAuthenticationService authenticationService = ServiceRef.Provider.GetRequiredService<IAuthenticationService>();
20
21 private readonly string? requestorFullJid;
22 private readonly string? petitionId;
23 private readonly PhotosLoader photosLoader;
24
30 {
31 this.Photos = [];
32 this.photosLoader = new PhotosLoader(this.Photos);
33
34 if (Args is not null)
35 {
36 this.RequestorIdentity = Args.RequestorIdentity;
37 this.requestorFullJid = Args.RequestorFullJid;
38 this.RequestedContract = Args.RequestedContract;
39 this.petitionId = Args.PetitionId;
40 this.Purpose = Args.Purpose;
41 }
42 }
43
45 public override async Task OnInitializeAsync()
46 {
47 await base.OnInitializeAsync();
48
49 this.AssignProperties();
50
51 if (this.RequestorIdentity?.Attachments is not null)
52 this.LoadPhotos();
53 }
54
55 private async void LoadPhotos()
56 {
57 try
58 {
59 if (this.RequestorIdentity?.Attachments is not null)
60 {
61 Photo? First = await this.photosLoader.LoadPhotos(this.RequestorIdentity.Attachments, SignWith.LatestApprovedId);
62
63 this.FirstPhotoSource = First?.Source;
64 this.FirstPhotoRotation = First?.Rotation ?? 0;
65 }
66 }
67 catch (Exception ex)
68 {
69 ServiceRef.LogService.LogException(ex);
70 }
71 }
72
74 public override async Task OnDisposeAsync()
75 {
76 this.photosLoader.CancelLoadPhotos();
77
78 await base.OnDisposeAsync();
79 }
80
84 public ObservableCollection<Photo> Photos { get; }
85
89 public Contract? RequestedContract { get; private set; }
90
94 public LegalIdentity? RequestorIdentity { get; private set; }
95
99 [RelayCommand]
100 private async Task Accept()
101 {
102 if (!await this.authenticationService.AuthenticateUserAsync(AuthenticationPurpose.AcceptPetitionRequest, true))
103 return;
104
105 bool Succeeded = await ServiceRef.NetworkService.TryRequest(() => ServiceRef.XmppService.SendPetitionContractResponse(
106 this.RequestedContract!.ContractId, this.petitionId!, this.requestorFullJid!, true));
107
108 if (Succeeded)
109 await this.GoBack();
110 }
111
115 [RelayCommand]
116 private async Task Decline()
117 {
118 bool succeeded = await ServiceRef.NetworkService.TryRequest(() => ServiceRef.XmppService.SendPetitionContractResponse(
119 this.RequestedContract!.ContractId, this.petitionId!, this.requestorFullJid!, false));
120
121 if (succeeded)
122 await this.GoBack();
123 }
124
128 [RelayCommand]
129 private async Task Ignore()
130 {
131 await this.GoBack();
132 }
133
134 // Full name of requesting entity.
135 public string FullName => ContactInfo.GetFullName(this.FirstName, this.MiddleNames, this.LastNames);
136
137 #region Properties
138
142 [ObservableProperty]
143 private DateTime? created;
144
148 [ObservableProperty]
149 private DateTime? updated;
150
154 [ObservableProperty]
155 private DateTime? expires;
156
160 [ObservableProperty]
161 private string? legalId;
162
166 [ObservableProperty]
167 private string? networkId;
168
172 [ObservableProperty]
173 private IdentityState? state;
174
178 [ObservableProperty]
179 private DateTime? from;
180
184 [ObservableProperty]
185 private DateTime? to;
186
190 [ObservableProperty]
191 [NotifyPropertyChangedFor(nameof(FullName))]
192 private string? firstName;
193
197 [ObservableProperty]
198 [NotifyPropertyChangedFor(nameof(FullName))]
199 private string? middleNames;
200
204 [ObservableProperty]
205 [NotifyPropertyChangedFor(nameof(FullName))]
206 private string? lastNames;
207
211 [ObservableProperty]
212 private string? personalNumber;
213
217 [ObservableProperty]
218 private string? address;
219
223 [ObservableProperty]
224 private string? address2;
225
229 [ObservableProperty]
230 private string? zipCode;
231
235 [ObservableProperty]
236 private string? area;
237
241 [ObservableProperty]
242 private string? city;
243
247 [ObservableProperty]
248 private string? region;
249
253 [ObservableProperty]
254 private string? countryCode;
255
259 [ObservableProperty]
260 private string? nationalityCode;
261
265 [ObservableProperty]
266 private string? gender;
267
271 [ObservableProperty]
272 private DateTime? birthDate;
273
277 [ObservableProperty]
278 private string? orgName;
279
283 [ObservableProperty]
284 private string? orgNumber;
285
289 [ObservableProperty]
290 private string? orgDepartment;
291
295 [ObservableProperty]
296 private string? orgRole;
297
301 [ObservableProperty]
302 private string? orgAddress;
303
307 [ObservableProperty]
308 private string? orgAddress2;
309
313 [ObservableProperty]
314 private string? orgZipCode;
315
319 [ObservableProperty]
320 private string? orgArea;
321
325 [ObservableProperty]
326 private string? orgCity;
327
331 [ObservableProperty]
332 private string? orgRegion;
333
337 [ObservableProperty]
338 private string? orgCountryCode;
339
343 [ObservableProperty]
344 private bool hasOrg;
345
349 [ObservableProperty]
350 private bool hasPhotos;
351
355 [ObservableProperty]
356 private string? phoneNr;
357
361 [ObservableProperty]
362 private string? eMail;
363
367 [ObservableProperty]
368 private string? deviceId;
369
373 [ObservableProperty]
374 private bool isApproved;
375
379 [ObservableProperty]
380 private string? purpose;
381
385 [ObservableProperty]
386 private ImageSource? firstPhotoSource;
387
391 [ObservableProperty]
392 private int? firstPhotoRotation;
393
394 #endregion
395
396 private void AssignProperties()
397 {
398 if (this.RequestorIdentity is not null)
399 {
400 this.Created = this.RequestorIdentity.Created;
401 this.Updated = this.RequestorIdentity.Updated.GetDateOrNullIfMinValue();
402 this.Expires = this.RequestorIdentity.To.GetDateOrNullIfMinValue();
403 this.LegalId = this.RequestorIdentity.Id;
404 this.NetworkId = this.RequestorIdentity.GetJid();
405 this.State = this.RequestorIdentity.State;
406 this.From = this.RequestorIdentity.From.GetDateOrNullIfMinValue();
407 this.To = this.RequestorIdentity.To.GetDateOrNullIfMinValue();
409 this.MiddleNames = this.RequestorIdentity[Constants.XmppProperties.MiddleNames];
411 this.PersonalNumber = this.RequestorIdentity[Constants.XmppProperties.PersonalNumber];
418 this.CountryCode = this.RequestorIdentity[Constants.XmppProperties.Country];
419 this.NationalityCode = this.RequestorIdentity[Constants.XmppProperties.Nationality];
421
422 string BirthDayStr = this.RequestorIdentity[Constants.XmppProperties.BirthDay];
423 string BirthMonthStr = this.RequestorIdentity[Constants.XmppProperties.BirthMonth];
424 string BirthYearStr = this.RequestorIdentity[Constants.XmppProperties.BirthYear];
425
426 if (!string.IsNullOrEmpty(BirthDayStr) && int.TryParse(BirthDayStr, out int BirthDay) &&
427 !string.IsNullOrEmpty(BirthMonthStr) && int.TryParse(BirthMonthStr, out int BirthMonth) &&
428 !string.IsNullOrEmpty(BirthYearStr) && int.TryParse(BirthYearStr, out int BirthYear))
429 {
430 try
431 {
432 this.BirthDate = new DateTime(BirthYear, BirthMonth, BirthDay);
433 }
434 catch (Exception ex)
435 {
436 ServiceRef.LogService.LogException(ex);
437 this.BirthDate = null;
438 }
439 }
440
443 this.OrgDepartment = this.RequestorIdentity[Constants.XmppProperties.OrgDepartment];
446 this.OrgAddress2 = this.RequestorIdentity[Constants.XmppProperties.OrgAddress2];
451 this.OrgCountryCode = this.RequestorIdentity[Constants.XmppProperties.OrgCountry];
452 this.HasOrg =
453 !string.IsNullOrEmpty(this.OrgName) ||
454 !string.IsNullOrEmpty(this.OrgNumber) ||
455 !string.IsNullOrEmpty(this.OrgDepartment) ||
456 !string.IsNullOrEmpty(this.OrgRole) ||
457 !string.IsNullOrEmpty(this.OrgAddress) ||
458 !string.IsNullOrEmpty(this.OrgAddress2) ||
459 !string.IsNullOrEmpty(this.OrgZipCode) ||
460 !string.IsNullOrEmpty(this.OrgArea) ||
461 !string.IsNullOrEmpty(this.OrgCity) ||
462 !string.IsNullOrEmpty(this.OrgRegion) ||
463 !string.IsNullOrEmpty(this.OrgCountryCode);
464 this.HasPhotos = this.Photos.Count > 0;
465 this.PhoneNr = this.RequestorIdentity[Constants.XmppProperties.Phone];
468 this.IsApproved = this.RequestorIdentity.State == IdentityState.Approved;
469 }
470 else
471 {
472 this.Created = DateTime.MinValue;
473 this.Updated = DateTime.MinValue;
474 this.LegalId = Constants.NotAvailableValue;
475 this.State = IdentityState.Compromised;
476 this.From = null;
477 this.To = null;
478 this.FirstName = Constants.NotAvailableValue;
479 this.MiddleNames = Constants.NotAvailableValue;
480 this.LastNames = Constants.NotAvailableValue;
481 this.PersonalNumber = Constants.NotAvailableValue;
482 this.Address = Constants.NotAvailableValue;
483 this.Address2 = Constants.NotAvailableValue;
484 this.ZipCode = Constants.NotAvailableValue;
485 this.Area = Constants.NotAvailableValue;
486 this.City = Constants.NotAvailableValue;
487 this.Region = Constants.NotAvailableValue;
488 this.CountryCode = Constants.NotAvailableValue;
489 this.NationalityCode = string.Empty;
490 this.Gender = string.Empty;
491 this.BirthDate = null;
492 this.OrgName = Constants.NotAvailableValue;
493 this.OrgNumber = Constants.NotAvailableValue;
494 this.OrgDepartment = Constants.NotAvailableValue;
495 this.OrgRole = Constants.NotAvailableValue;
496 this.OrgAddress = Constants.NotAvailableValue;
497 this.OrgAddress2 = Constants.NotAvailableValue;
498 this.OrgZipCode = Constants.NotAvailableValue;
499 this.OrgArea = Constants.NotAvailableValue;
500 this.OrgCity = Constants.NotAvailableValue;
501 this.OrgRegion = Constants.NotAvailableValue;
502 this.OrgCountryCode = Constants.NotAvailableValue;
503 this.HasOrg = false;
504 this.HasPhotos = false;
505 this.PhoneNr = Constants.NotAvailableValue;
506 this.EMail = Constants.NotAvailableValue;
507 this.DeviceId = Constants.NotAvailableValue;
508 this.IsApproved = false;
509 }
510 }
511
515 [RelayCommand]
516 private async Task Copy(object Item)
517 {
518 try
519 {
520 if (Item is string Label)
521 {
522 if (Label == this.LegalId)
523 {
524 await Clipboard.SetTextAsync(Constants.UriSchemes.IotId + ":" + this.LegalId);
525 await ServiceRef.UiService.DisplayAlert(
528 }
529 else
530 {
531 await Clipboard.SetTextAsync(Label);
532 await ServiceRef.UiService.DisplayAlert(
535 }
536 }
537 }
538 catch (Exception ex)
539 {
540 ServiceRef.LogService.LogException(ex);
541 await ServiceRef.UiService.DisplayException(ex);
542 }
543 }
544 }
545}
const string IotId
The IoT ID URI Scheme (iotid)
Definition: Constants.cs:153
const string PersonalNumber
Personal number
Definition: Constants.cs:394
const string OrgAddress2
Organization Address line 2
Definition: Constants.cs:474
const string OrgArea
Organization Area
Definition: Constants.cs:479
const string OrgRegion
Organization Region
Definition: Constants.cs:494
const string Nationality
Nationality
Definition: Constants.cs:434
const string BirthYear
Birth Year
Definition: Constants.cs:454
const string OrgCity
Organization City
Definition: Constants.cs:484
const string OrgRole
Organization Role
Definition: Constants.cs:509
const string Phone
Phone number
Definition: Constants.cs:524
const string EMail
e-Mail address
Definition: Constants.cs:529
const string OrgZipCode
Organization Zip Code
Definition: Constants.cs:489
const string MiddleNames
Middle names
Definition: Constants.cs:379
const string OrgCountry
Organization Country
Definition: Constants.cs:499
const string OrgDepartment
Organization Department
Definition: Constants.cs:504
const string Address2
Address line 2
Definition: Constants.cs:404
const string OrgAddress
Organization Address line 1
Definition: Constants.cs:469
const string Address
Address line 1
Definition: Constants.cs:399
const string LastNames
Last names
Definition: Constants.cs:384
const string OrgNumber
Organization number
Definition: Constants.cs:464
const string BirthMonth
Birth Month
Definition: Constants.cs:449
const string FirstName
First name
Definition: Constants.cs:374
const string OrgName
Organization name
Definition: Constants.cs:459
A set of never changing property constants and helpful values.
Definition: Constants.cs:24
const string NotAvailableValue
A generic "no value available" string.
Definition: Constants.cs:28
A strongly-typed resource class, for looking up localized strings, etc.
static string IdCopiedSuccessfully
Looks up a localized string similar to A link to the ID was copied to the clipboard....
static string TagValueCopiedToClipboard
Looks up a localized string similar to Tag value copied to clipboard.
static string SuccessTitle
Looks up a localized string similar to Success.
Contains information about a contact.
Definition: ContactInfo.cs:22
static string GetFullName(LegalIdentity? Identity)
Gets the full name of a person.
Definition: ContactInfo.cs:578
Base class that references services in the app.
Definition: ServiceRef.cs:43
static IServiceProvider Provider
The service provider for the app. This is set before the app is started, and will be used to resolve ...
Definition: ServiceRef.cs:48
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
static INetworkService NetworkService
Network service.
Definition: ServiceRef.cs:226
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:190
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.
PetitionContractViewModel(PetitionContractNavigationArgs? Args)
Creates a new instance of the PetitionContractViewModel class.
override async Task OnDisposeAsync()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
override async Task OnInitializeAsync()
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, Attachment? Attachment)
Class containing information about a photo.
Definition: Photo.cs:10
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:82