Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LoadingViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
6
8{
9 public partial class LoadingViewModel : BaseRegistrationViewModel
10 {
11 public LoadingViewModel()
12 : base(RegistrationStep.Complete)
13 {
14 }
15
17 protected override async Task OnInitialize()
18 {
19 await base.OnInitialize();
20
21 this.IsBusy = true;
22 ServiceRef.XmppService.ConnectionStateChanged += this.XmppService_ConnectionStateChanged;
23 ServiceRef.XmppService.Loaded += this.XmppService_Loaded;
24 }
25
27 protected override async Task OnDispose()
28 {
29 ServiceRef.XmppService.Loaded -= this.XmppService_Loaded;
30 ServiceRef.XmppService.ConnectionStateChanged -= this.XmppService_ConnectionStateChanged;
31
32 await base.OnDispose();
33 }
34
35 #region Properties
36
40 [ObservableProperty]
41 private string? connectionStateText;
42
46 [ObservableProperty]
47 private Brush? connectionStateColor;
48
52 [ObservableProperty]
53 [NotifyPropertyChangedFor(nameof(DisplayConnectionText))]
54 private string? stateSummaryText;
55
59 //[ObservableProperty]
60 //private bool isConnected;
61
62 public bool DisplayConnectionText => ServiceRef.TagProfile.IsCompleteOrWaitingForValidation() && !string.IsNullOrEmpty(this.ConnectionStateText);
63
64 #endregion
65
71 protected virtual Task XmppService_ConnectionStateChanged(object _, XmppState NewState)
72 {
73 if (MainThread.IsMainThread)
74 {
75 this.SetConnectionStateAndText(NewState);
76 return Task.CompletedTask;
77 }
78 else
79 {
80 return MainThread.InvokeOnMainThreadAsync(() =>
81 {
82 this.SetConnectionStateAndText(NewState);
83 });
84 }
85 }
86
89 {
90 this.ConnectionStateText = state.ToDisplayText();
91 //this.ConnectionStateColor = new SolidColorBrush(state.ToColor());
92 //this.IsConnected = state == XmppState.Connected;
93 this.StateSummaryText = (ServiceRef.TagProfile.LegalIdentity?.State)?.ToString() + " - " + this.ConnectionStateText;
94 }
95
96 private void XmppService_Loaded(object? Sender, LoadedEventArgs e)
97 {
98 try
99 {
100 if (e.IsLoaded && !e.IsResuming)
101 {
102 this.IsBusy = false;
103
104 // XmppService_Loaded method might be called from OnInitialize method.
105 // We cannot update the main page while some initialization is still running (well, we can technically but there will be chaos).
106 // Therefore, do not await this method and do not call it synchronously, even if we are already on the main thread.
107 Task ExecutionTask = Task.Run(() =>
108 {
109 // The right step will be loaded by TagProfile service,
110 // but we may choose to change it here if necessary
111
112 // GoToRegistrationStep(RegistrationStep.<TheOtherState>);
113 GoToRegistrationStep(ServiceRef.TagProfile.Step);
114 });
115 }
116 }
117 catch (Exception Exception)
118 {
119 ServiceRef.LogService?.LogException(Exception);
120 }
121 }
122 }
123}
Base class that references services in the app.
Definition: ServiceRef.cs:31
static ILogService LogService
Log service.
Definition: ServiceRef.cs:91
static ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:79
virtual Task XmppService_ConnectionStateChanged(object _, XmppState NewState)
Listens to connection state changes from the XMPP server.
bool DisplayConnectionText
Gets whether the view model is connected to an XMPP server.
RegistrationStep Step
This profile's current registration step.
Definition: ITagProfile.cs:161
bool IsCompleteOrWaitingForValidation()
Returns true if the registration process for this ITagProfile is either fully complete or is just awa...
LegalIdentity? LegalIdentity
The legal identity of the current user/profile.
Definition: ITagProfile.cs:211
RegistrationStep
The different steps of a TAG Profile registration journey.
XmppState
State of XMPP connection.
Definition: XmppState.cs:7