3using System.Threading.Tasks;
4using CommunityToolkit.Mvvm.ComponentModel;
5using CommunityToolkit.Mvvm.Input;
6using Microsoft.Extensions.DependencyInjection;
7using Microsoft.Maui.ApplicationModel;
25 private bool hasAppliedForIdentity;
26 private bool callbacksRegistered;
34 private string errorMessage =
string.Empty;
37 private string statusMessage =
string.Empty;
39 public bool HasError => !
string.IsNullOrEmpty(this.ErrorMessage);
40 public bool HasStatusMessage => !
string.IsNullOrEmpty(this.StatusMessage);
41 public bool IsXmppConnected => ServiceRef.XmppService.State ==
XmppState.Connected;
43 public bool IsLegalIdentityCreated => ServiceRef.TagProfile.LegalIdentity is
LegalIdentity identity && identity.State ==
IdentityState.Approved;
45 public bool IsWaitingForIdentity => this.IsBusy || this.IsIdentityPendingApproval || (this.hasAppliedForIdentity && !this.IsLegalIdentityCreated);
46 public bool CanCreateIdentity => this.IsAccountCreated && ServiceRef.TagProfile.LegalIdentity is
null && !this.IsBusy;
50 await base.OnInitializeAsync();
52 if (!this.callbacksRegistered)
54 ServiceRef.XmppService.ConnectionStateChanged += this.XmppService_ConnectionStateChanged;
55 ServiceRef.XmppService.LegalIdentityChanged += this.XmppContracts_LegalIdentityChanged;
56 this.callbacksRegistered =
true;
63 ServiceRef.
LogService.LogInformational(
"Clearing approved identity prior to reverify onboarding step.");
68 await this.TryAutoApplyAsync();
69 await this.CheckApplicationStateAsync();
72 internal override async Task OnActivatedAsync()
74 await base.OnActivatedAsync();
75 await this.TryAutoApplyAsync();
76 await this.CheckApplicationStateAsync();
81 if (this.callbacksRegistered)
83 ServiceRef.XmppService.ConnectionStateChanged -= this.XmppService_ConnectionStateChanged;
84 ServiceRef.XmppService.LegalIdentityChanged -= this.XmppContracts_LegalIdentityChanged;
85 this.callbacksRegistered =
false;
88 await base.OnDisposeAsync();
91 private Task XmppService_ConnectionStateChanged(
object _,
XmppState newState)
95 MainThread.BeginInvokeOnMainThread(async () =>
97 await this.TryAutoApplyAsync();
98 await this.CheckApplicationStateAsync();
102 return Task.CompletedTask;
107 MainThread.BeginInvokeOnMainThread(async () =>
109 this.OnPropertyChanged(nameof(this.IsLegalIdentityCreated));
110 this.OnPropertyChanged(nameof(this.IsIdentityPendingApproval));
111 this.OnPropertyChanged(nameof(this.IsWaitingForIdentity));
112 await this.CheckApplicationStateAsync();
115 return Task.CompletedTask;
118 private async Task TryAutoApplyAsync()
125 if (!this.CanCreateIdentity || this.hasAppliedForIdentity)
133 XmppClient? client = ServiceRef.XmppService.State == XmppState.Connected ?
ServiceRef.
XmppService.GetType().GetProperty(
"ContractsClient") is
null ? null : null :
null;
143 this.SetHasAppliedForIdentity(
true);
144 this.ErrorMessage =
string.Empty;
145 this.StatusMessage =
string.Empty;
149 bool appliedSuccessfully =
false;
152 while (!timerCts.Token.IsCancellationRequested)
156 RegisterIdentityModel model = this.CreateRegisterModel();
159 if (succeeded && addedIdentity is not
null)
161 identity = addedIdentity;
163 this.RegisterIdentityNotificationIgnoreFilter(identity.
Id);
164 appliedSuccessfully =
true;
176 await Task.Delay(TimeSpan.FromSeconds(5), timerCts.Token);
178 catch (TaskCanceledException)
184 if (!appliedSuccessfully)
188 this.OnPropertyChanged(nameof(this.IsLegalIdentityCreated));
189 this.OnPropertyChanged(nameof(this.IsIdentityPendingApproval));
190 this.OnPropertyChanged(nameof(this.IsWaitingForIdentity));
192 if (!appliedSuccessfully)
196 this.SetHasAppliedForIdentity(
false);
200 private async Task CheckApplicationStateAsync()
204 if (!this.IsAccountCreated)
206 if (!this.hasAppliedForIdentity)
207 await this.TryAutoApplyAsync();
211 switch (legalIdentity.State)
214 this.ErrorMessage =
string.Empty;
215 this.StatusMessage =
string.Empty;
216 if (this.CoordinatorViewModel is not
null)
217 await this.CoordinatorViewModel.GoToStepCommand.ExecuteAsync(
OnboardingStep.DefinePassword);
220 this.ErrorMessage =
string.Empty;
226 await this.HandleIdentityFailureAsync(legalIdentity.State);
229 if (legalIdentity.IsDiscarded())
231 await this.HandleIdentityFailureAsync(legalIdentity.State);
236 this.OnPropertyChanged(nameof(this.IsLegalIdentityCreated));
237 this.OnPropertyChanged(nameof(this.IsIdentityPendingApproval));
238 this.OnPropertyChanged(nameof(this.IsWaitingForIdentity));
241 private async Task HandleIdentityFailureAsync(
IdentityState state)
243 this.StatusMessage =
string.Empty;
258 this.SetHasAppliedForIdentity(
false);
259 this.OnPropertyChanged(nameof(this.IsLegalIdentityCreated));
260 this.OnPropertyChanged(nameof(this.IsIdentityPendingApproval));
262 if (this.CoordinatorViewModel is not
null)
263 await this.CoordinatorViewModel.GoToStepCommand.ExecuteAsync(
OnboardingStep.ValidatePhone);
266 private void SetHasAppliedForIdentity(
bool value)
268 if (this.hasAppliedForIdentity == value)
271 this.hasAppliedForIdentity = value;
272 this.OnPropertyChanged(nameof(this.IsWaitingForIdentity));
275 private RegisterIdentityModel CreateRegisterModel()
277 RegisterIdentityModel model =
new();
280 model.PhoneNr = value;
284 model.CountryCode = value;
288 private void RegisterIdentityNotificationIgnoreFilter(
string IdentityId)
290 if (
string.IsNullOrWhiteSpace(IdentityId))
294 string IgnoredIdentityId = IdentityId;
300 if (!
string.Equals(Intent.
EntityId, IgnoredIdentityId, StringComparison.OrdinalIgnoreCase))
307 partial
void OnStatusMessageChanged(
string value)
309 this.OnPropertyChanged(nameof(this.HasStatusMessage));
312 partial
void OnIsBusyChanged(
bool value)
314 this.OnPropertyChanged(nameof(this.IsWaitingForIdentity));
const string Identities
Identities channel
static readonly TimeSpan GenericRequest
Generic request timeout
A set of never changing property constants and helpful values.
A strongly-typed resource class, for looking up localized strings, etc.
static string YourLegalIdentityHasBeenCompromised
Looks up a localized string similar to Your identity has been marked compromised. You have therefore ...
static string IdApplicationSentDescription
Looks up a localized string similar to An application for a new ID has been sent. Waiting for the app...
static string PleaseTryAgain
Looks up a localized string similar to Please Try Again.
static string YourLegalIdentityHasBeenObsoleted
Looks up a localized string similar to Your identity has been marked obsolete. You have therefore bee...
static string YourApplicationWasRejected
Looks up a localized string similar to Your application was rejected..
Represent an attachment to a LegalIdentity.
Represents a filter decision for notification handling.
static NotificationFilterDecision None
Gets a decision that does not ignore any operation.
Platform-neutral intent describing how to route a notification.
string? EntityId
Gets or sets an entity identifier associated with the action.
string? Channel
Gets or sets the push channel identifier.
Base class that references services in the app.
static IServiceProvider Provider
The service provider for the app. This is set before the app is started, and will be used to resolve ...
static ILogService LogService
Log service.
static INetworkService NetworkService
Network service.
static ITagProfile TagProfile
TAG Profile service.
static IReportingStringLocalizer Localizer
Localization service
static IXmppService XmppService
The XMPP service for XMPP communication.
Mirrors registration create-account behaviour for onboarding: waits for XMPP, applies for identity,...
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 ...
Event arguments for legal identity responses
DateTime Created
When the identity object was created
IdentityState State
Current state of identity
string Id
ID of the legal identity
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Interface for the redesigned notification service.
Task SetLegalIdentity(LegalIdentity? Identity, bool RemoveOldAttachments)
Sets the legal identity of the profile.
string? Account
The account name for this profile
string? EMail
Verified e-mail address.
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
string? PhoneNumber
Verified phone number.
LegalIdentity? LegalIdentity
The legal identity of the current user/profile.
bool NeedsUpdating()
Returns true if the current ITagProfile needs to have its values updated, false otherwise.
OnboardingScenario
Distinct onboarding scenarios.
OnboardingStep
Unified onboarding steps matching the registration journey.
IdentityState
Lists recognized legal identity states.
XmppState
State of XMPP connection.