Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmppViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
6
8{
12 public partial class XmppViewModel : BaseViewModel
13 {
17 protected XmppViewModel()
18 : base()
19 {
20 this.StateSummaryText = ServiceRef.Localizer[nameof(AppResources.XmppState_Offline)];
21 this.ConnectionStateText = ServiceRef.Localizer[nameof(AppResources.XmppState_Offline)];
22 this.ConnectionStateColor = new SolidColorBrush(Colors.Red);
23 this.StateSummaryText = string.Empty;
25 }
27 protected override async Task OnInitialize()
28 {
29 await base.OnInitialize();
30
31 ServiceRef.XmppService.ConnectionStateChanged += this.XmppService_ConnectionStateChanged;
32 }
33
35 protected override async Task OnDispose()
36 {
37 ServiceRef.XmppService.ConnectionStateChanged -= this.XmppService_ConnectionStateChanged;
38 await base.OnDispose();
39 }
40
41 #region Properties
42
46 [ObservableProperty]
47 private string connectionStateText;
48
52 [ObservableProperty]
53 private Brush connectionStateColor;
54
58 [ObservableProperty]
59 private string stateSummaryText;
60
64 [ObservableProperty]
65 private bool isConnected;
66
67 #endregion
68
73 protected virtual void SetConnectionStateAndText(XmppState State)
74 {
75 this.ConnectionStateText = State.ToDisplayText();
76 this.ConnectionStateColor = new SolidColorBrush(State.ToColor());
77 this.IsConnected = State == XmppState.Connected;
78 this.StateSummaryText = (ServiceRef.TagProfile.LegalIdentity?.State)?.ToString() + " - " + this.ConnectionStateText;
79 }
80
86 protected virtual Task XmppService_ConnectionStateChanged(object? _, XmppState NewState)
87 {
88 if (MainThread.IsMainThread)
89 {
90 this.SetConnectionStateAndText(NewState);
91 return Task.CompletedTask;
92 }
93 else
94 {
95 return MainThread.InvokeOnMainThreadAsync(() =>
96 {
97 this.SetConnectionStateAndText(NewState);
98 });
99 }
100 }
101 }
102}
Base class that references services in the app.
Definition: ServiceRef.cs:31
static ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:79
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...
A view model that holds the XMPP state.
override async Task OnInitialize()
Method called when view is initialized for the first time. Use this method to implement registration ...
XmppViewModel()
Creates an instance of a XmppViewModel.
override async Task OnDispose()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
virtual void SetConnectionStateAndText(XmppState State)
Sets both the connection state and connection text to the appropriate value.
virtual Task XmppService_ConnectionStateChanged(object? _, XmppState NewState)
Listens to connection state changes from the XMPP server.
XmppState
State of XMPP connection.
Definition: XmppState.cs:7