Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ServiceProvidersViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using System.Collections.ObjectModel;
3using System.ComponentModel;
4using IServiceProvider = Waher.Networking.XMPP.Contracts.IServiceProvider;
5
7{
12 {
13 private const int defaultIconHeight = 150;
14
15 private readonly ServiceProvidersNavigationArgs? navigationArgs;
16
21 public ServiceProvidersViewModel(ServiceProvidersNavigationArgs? Args)
22 : base()
23 {
24 this.navigationArgs = Args;
25
26 if (Args is not null)
27 {
28 this.Title = Args.Title;
29 this.Description = Args.Description;
30
31 foreach (IServiceProvider ServiceProvider in Args.ServiceProviders)
32 {
33 this.ServiceProviders.Add(new ServiceProviderViewModel(ServiceProvider, defaultIconHeight, this));
34 }
35 }
36 }
37
39 public override async Task OnDisposeAsync()
40 {
41 if (this.navigationArgs?.ServiceProvider is TaskCompletionSource<IServiceProvider> TaskSource)
42 TaskSource.TrySetResult(null);
43
44 await base.OnDisposeAsync();
45 }
46
47 #region Properties
48
52 [ObservableProperty]
53 private string? title;
54
58 [ObservableProperty]
59 private string? description;
60
64 public ObservableCollection<ServiceProviderViewModel> ServiceProviders { get; } = [];
65
69 [ObservableProperty]
70 private ServiceProviderViewModel? selectedServiceProvider;
71
73 protected override void OnPropertyChanged(PropertyChangedEventArgs e)
74 {
75 base.OnPropertyChanged(e);
76
77 switch (e.PropertyName)
78 {
79 case nameof(this.SelectedServiceProvider):
80 MainThread.BeginInvokeOnMainThread(async () =>
81 {
82 await this.TrySetResultAndClosePage(this.SelectedServiceProvider?.ServiceProvider);
83 });
84 break;
85 }
86 }
87
88 #endregion
89
91 public override Task GoBack()
92 {
93 return this.TrySetResultAndClosePage(null);
94 }
95
96 private async Task TrySetResultAndClosePage(IServiceProvider? ServiceProvider)
97 {
98 TaskCompletionSource<IServiceProvider?>? TaskSource = null;
99
100 if (this.navigationArgs is not null)
101 {
102 TaskSource = this.navigationArgs.ServiceProvider;
103 this.navigationArgs.ServiceProvider = null;
104 }
105
106 await base.GoBack();
107
108 TaskSource?.TrySetResult(ServiceProvider);
109 }
110
115 internal async Task SelectServiceProvider(ServiceProviderViewModel ServiceProvider)
116 {
117 await MainThread.InvokeOnMainThreadAsync(async () =>
118 {
119 await this.TrySetResultAndClosePage(ServiceProvider.ServiceProvider);
120 });
121 }
122 }
123}
The view model to bind to for when displaying a list of service providers.
override async Task OnDisposeAsync()
Method called when the view is disposed, and will not be used more. Use this method to unregister eve...
ObservableCollection< ServiceProviderViewModel > ServiceProviders
Holds a list of service providers
override Task GoBack()
Method called when user wants to navigate to the previous screen.
ServiceProvidersViewModel(ServiceProvidersNavigationArgs? Args)
Creates an instance of the ServiceProvidersViewModel class.
A view model that holds the XMPP state.
Interface for information about a service provider.
Definition: App.xaml.cs:4