Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CreateAccountViewModel.cs
1using System.ComponentModel;
2using System.Runtime.CompilerServices;
3using CommunityToolkit.Mvvm.ComponentModel;
4using CommunityToolkit.Mvvm.Input;
12
14{
15 public partial class CreateAccountViewModel : BaseRegistrationViewModel
16 {
18 : base(RegistrationStep.CreateAccount)
19 {
20 }
21
23 protected override async Task OnInitialize()
24 {
25 await base.OnInitialize();
26
27 ServiceRef.XmppService.ConnectionStateChanged += this.XmppService_ConnectionStateChanged;
28 ServiceRef.XmppService.LegalIdentityChanged += this.XmppContracts_LegalIdentityChanged;
29 }
30
32 protected override async Task OnDispose()
33 {
34 ServiceRef.XmppService.ConnectionStateChanged -= this.XmppService_ConnectionStateChanged;
35 ServiceRef.XmppService.LegalIdentityChanged -= this.XmppContracts_LegalIdentityChanged;
36
37 await base.OnDispose();
38 }
39
41 public override async Task DoAssignProperties()
42 {
43 await base.DoAssignProperties();
44
45 if (this.CreateIdentityCommand.CanExecute(null))
46 await this.CreateIdentityCommand.ExecuteAsync(null);
48 await this.CheckAndHandleIdentityApplicationAsync();
49 }
50
51 private Task XmppService_ConnectionStateChanged(object _, XmppState NewState)
52 {
53 if (NewState == XmppState.Connected)
54 {
55 MainThread.BeginInvokeOnMainThread(async () =>
56 {
57 await this.DoAssignProperties();
58 });
59 }
60
61 return Task.CompletedTask;
62 }
63
64 private async Task XmppContracts_LegalIdentityChanged(object _, LegalIdentityEventArgs e)
65 {
66 MainThread.BeginInvokeOnMainThread(async () =>
67 {
68 this.OnPropertyChanged(nameof(IsLegalIdentityCreated));
69 this.CreateIdentityCommand.NotifyCanExecuteChanged();
70 await this.DoAssignProperties();
71 });
72 }
73
74 protected override void OnPropertyChanged(PropertyChangedEventArgs e)
75 {
76 base.OnPropertyChanged(e);
77
78 switch (e.PropertyName)
79 {
80 case nameof(this.IsBusy):
81 this.CreateIdentityCommand.NotifyCanExecuteChanged();
82 break;
83 }
84 }
85
89 public static bool IsXmppConnected => ServiceRef.XmppService.State == XmppState.Connected;
90
94 public static bool IsAccountCreated => !string.IsNullOrEmpty(ServiceRef.TagProfile.Account);
95
99 public static bool IsLegalIdentityCreated
100 {
101 get
102 {
103 return ServiceRef.TagProfile.LegalIdentity is not null &&
104 (ServiceRef.TagProfile.LegalIdentity.State == IdentityState.Approved ||
105 ServiceRef.TagProfile.LegalIdentity.State == IdentityState.Created);
106 }
107 }
108
109
114
115
119 [RelayCommand(CanExecute = nameof(CanCreateIdentity))]
120 private async Task CreateIdentity()
121 {
122 try
123 {
124 RegisterIdentityModel IdentityModel = CreateRegisterModel();
125 LegalIdentityAttachment[] Photos = []; // Photos are left empty
126
127 (bool Succeeded, LegalIdentity? AddedIdentity) = await ServiceRef.NetworkService.TryRequest(() =>
128 ServiceRef.XmppService.AddLegalIdentity(IdentityModel, true, Photos));
129
130 if (Succeeded && AddedIdentity is not null)
131 await ServiceRef.TagProfile.SetLegalIdentity(AddedIdentity, true);
132 }
133 catch (Exception ex)
134 {
135 ServiceRef.LogService.LogException(ex);
136 await ServiceRef.UiService.DisplayException(ex);
137 }
138 }
139
140 [RelayCommand]
141 private static async Task ValidateIdentity()
142 {
143 await Task.CompletedTask;
144 }
145
146
147 private async Task CheckAndHandleIdentityApplicationAsync()
148 {
149 this.IsBusy = true;
151 {
152 if (LegalIdentity.State == IdentityState.Approved)
153 {
154 if (Shell.Current.CurrentState.Location.OriginalString == Constants.Pages.RegistrationPage)
155 GoToRegistrationStep(RegistrationStep.DefinePassword);
156 }
157 else if (LegalIdentity.IsDiscarded())
158 {
161 GoToRegistrationStep(RegistrationStep.ValidatePhone);
162 }
163 }
164 this.IsBusy = true;
165 }
166
167 private static RegisterIdentityModel CreateRegisterModel()
168 {
169 RegisterIdentityModel IdentityModel = new();
170 string s;
171
172 if (!string.IsNullOrWhiteSpace(s = ServiceRef.TagProfile?.PhoneNumber?.Trim() ?? string.Empty))
173 {
174 if (string.IsNullOrWhiteSpace(s) && (ServiceRef.TagProfile?.LegalIdentity is LegalIdentity LegalIdentity))
176
177 IdentityModel.PhoneNr = s;
178 }
179
180 if (!string.IsNullOrWhiteSpace(s = ServiceRef.TagProfile?.EMail?.Trim() ?? string.Empty))
181 {
182 if (string.IsNullOrWhiteSpace(s) && (ServiceRef.TagProfile?.LegalIdentity is LegalIdentity LegalIdentity))
184
185 IdentityModel.EMail = s;
186 }
187
188 if (!string.IsNullOrWhiteSpace(s = ServiceRef.TagProfile?.SelectedCountry?.Trim() ?? string.Empty))
189 {
190 if (string.IsNullOrWhiteSpace(s) && (ServiceRef.TagProfile?.LegalIdentity is LegalIdentity LegalIdentity))
192
193 IdentityModel.CountryCode = s;
194 }
195
196 return IdentityModel;
197 }
198 }
199}
Absolute paths to important pages.
Definition: Constants.cs:733
const string RegistrationPage
Path to registration page.
Definition: Constants.cs:742
const string Phone
Phone number
Definition: Constants.cs:414
const string EMail
e-Mail address
Definition: Constants.cs:419
A set of never changing property constants and helpful values.
Definition: Constants.cs:7
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 ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:79
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:67
static bool IsAccountCreated
If App has an XMPP account defined.
static bool IsXmppConnected
If App is connected to the XMPP network.
RegistrationStep Step
This profile's current registration step.
Definition: ITagProfile.cs:161
Task SetLegalIdentity(LegalIdentity? Identity, bool RemoveOldAttachments)
Sets the legal identity of the profile.
string? Account
The account name for this profile
Definition: ITagProfile.cs:101
string? EMail
Verified e-mail address.
Definition: ITagProfile.cs:96
Task ClearLegalIdentity()
Revert the Set LegalIdentity
string? SelectedCountry
Selected country. Some countries have the same phone code, so we want to save the selected country
Definition: ITagProfile.cs:71
string? PhoneNumber
Verified phone number.
Definition: ITagProfile.cs:91
LegalIdentity? LegalIdentity
The legal identity of the current user/profile.
Definition: ITagProfile.cs:211
RegistrationStep
The different steps of a TAG Profile registration journey.
IdentityState
Lists recognized legal identity states.
XmppState
State of XMPP connection.
Definition: XmppState.cs:7