Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ServiceProviderViewModel.cs
1using CommunityToolkit.Mvvm.Input;
3using IServiceProvider = Waher.Networking.XMPP.Contracts.IServiceProvider;
4
6{
13 public partial class ServiceProviderViewModel(IServiceProvider ServiceProvider, int IconHeight, ServiceProvidersViewModel Parent)
14 : XmppViewModel
15 {
16 private readonly IServiceProvider serviceProvider = ServiceProvider;
17 private readonly ServiceProvidersViewModel parent = Parent;
18 private ImageSource? iconSource;
19
23 public IServiceProvider ServiceProvider => this.serviceProvider;
24
28 public string Id => this.serviceProvider.Id;
29
33 public string Name => this.serviceProvider.Name;
34
38 public bool HasIcon => !string.IsNullOrEmpty(this.serviceProvider.IconUrl);
39
43 public string IconUrl => this.serviceProvider.IconUrl;
44
48 public bool ShowImage => this.HasIcon;
49
53 public bool ShowText => !this.HasIcon || this.IconWidth <= 250 || this.serviceProvider.GetType().Assembly == typeof(App).Assembly;
54
58 public int IconHeight { get; } = IconHeight;
59
63 public int IconWidth
64 {
65 get
66 {
67 if (!this.HasIcon || this.serviceProvider.IconHeight == 0)
68 return 0;
69
70 double s = ((double)this.IconHeight) / this.serviceProvider.IconHeight;
71
72 return (int)(this.serviceProvider.IconWidth * s + 0.5);
73 }
74 }
75
76 private bool hasRun = false;
77
81 public ImageSource? IconUrlSource
82 {
83 get
84 {
85 if (this.iconSource == null && !this.hasRun)
86 {
87 //Load the icon
88 Task.Run(this.UpdateIconUrlSourceAsync);
89 this.hasRun = true;
90 }
91 return this.iconSource;
92 }
93 }
94
95 public async Task UpdateIconUrlSourceAsync()
96 {
97 if (this.iconSource != null) return; // Already loaded or loading
98
99 try
100 {
101 //Check if URI is SVG
102 bool isSvg = this.IconUrl.EndsWith(".svg", StringComparison.OrdinalIgnoreCase);
103
104 if (this.IconUrl.StartsWith("resource://", StringComparison.Ordinal))
105 this.iconSource = ImageSource.FromResource(this.IconUrl[11..]);
106 else if (this.IconUrl.StartsWith("file://", StringComparison.Ordinal))
107 {
108 if(isSvg)
109 this.iconSource = this.IconUrl[7..^4];
110 else
111 this.iconSource = this.IconUrl[7..];
112 }
113 else if (Uri.TryCreate(this.IconUrl, UriKind.Absolute, out Uri? ParsedUri))
114 {
115 if(isSvg)
116 this.iconSource = await ServiceRef.UiService.ConvertSvgUriToImageSource(this.IconUrl);
117 else
118 this.iconSource = ImageSource.FromUri(ParsedUri);
119 }
120 else
121 {
122 //if it is an SVG, remove the extension
123 //Maui does not inheritly support SVGs, but can implicitly convert them to png
124 if(isSvg)
125 this.iconSource = ImageSource.FromFile(this.IconUrl[..^4]);
126 else
127 this.iconSource = ImageSource.FromFile(this.IconUrl);
128 }
129 }
130 catch (Exception ex)
131 {
132 ServiceRef.LogService.LogException(ex);
133 }
134
135 MainThread.BeginInvokeOnMainThread(() => this.OnPropertyChanged(nameof(this.IconUrlSource)));
136 }
137
141 [RelayCommand]
142 private Task SelectServiceProvider()
143 {
144 return this.parent.SelectServiceProvider(this);
145 }
146 }
147}
The Application class, representing an instance of the Neuro-Access app.
Definition: App.xaml.cs:69
Base class that references services in the app.
Definition: ServiceRef.cs:31
static ILogService LogService
Log service.
Definition: ServiceRef.cs:91
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
Interface for information about a service provider.
Definition: App.xaml.cs:4