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
24 private const double WideAspectThreshold = 2;
25
29 public IServiceProvider ServiceProvider => this.serviceProvider;
30
34 public string Id => this.serviceProvider.Id;
35
39 public string Name => this.serviceProvider.Name;
40
44 public bool HasIcon => !string.IsNullOrEmpty(this.serviceProvider.IconUrl);
45
49 public string IconUrl => this.serviceProvider.IconUrl;
50
54 private bool HasValidIconDimensions => this.serviceProvider.IconWidth > 0 && this.serviceProvider.IconHeight > 0;
55
59 private double IconAspectRatio
60 {
61 get
62 {
63 if (!this.HasValidIconDimensions)
64 return 1.0;
65
66 return (double)this.serviceProvider.IconWidth / this.serviceProvider.IconHeight;
67 }
68 }
69
73 public bool IsWideHero => this.HasIcon && this.HasValidIconDimensions && this.IconAspectRatio >= WideAspectThreshold;
74
78 public bool ShowImage => this.HasIcon;
79
87 public bool ShowText =>
88 !this.HasIcon ||
89 !this.IsWideHero ||
90 this.serviceProvider.GetType().Assembly == typeof(App).Assembly;
91
95 public int IconHeight { get; } = IconHeight;
96
103 public int IconWidth
104 {
105 get
106 {
107 if (!this.HasIcon || this.serviceProvider.IconHeight == 0)
108 return 0;
109
110 double s = ((double)this.IconHeight) / this.serviceProvider.IconHeight;
111
112 return (int)(this.serviceProvider.IconWidth * s + 0.5);
113 }
114 }
115
122 public int DisplayIconHeight
123 {
124 get
125 {
126 if (!this.ShowImage)
127 return 0;
128
129 // With text -> uniform square (height == width == IconHeight)
130 // Image-only -> use the requested height (width is proportional)
131 return this.IconHeight;
132 }
133 }
134
141 public int DisplayIconWidth
142 {
143 get
144 {
145 if (!this.ShowImage)
146 return 0;
147
148 // If text is visible, we present the icon as a square for uniformity.
149 if (this.ShowText)
150 return this.IconHeight;
151
152 // Image-only (wide hero) preserves proportional width.
153 return this.IconWidth;
154 }
155 }
156
157 private bool hasRun = false;
158
162 public ImageSource? IconUrlSource
163 {
164 get
165 {
166 if (this.iconSource is null && !this.hasRun)
167 {
168 //Load the icon
169 Task.Run(this.UpdateIconUrlSourceAsync);
170 this.hasRun = true;
171 }
172 return this.iconSource;
173 }
174 }
175
180 public async Task UpdateIconUrlSourceAsync()
181 {
182 if (this.iconSource is not null) return; // Already loaded or loading
183
184 try
185 {
186 //Check if URI is SVG
187 bool isSvg = this.IconUrl.EndsWith(".svg", StringComparison.OrdinalIgnoreCase);
188
189 if (this.IconUrl.StartsWith("resource://", StringComparison.Ordinal))
190 this.iconSource = ImageSource.FromResource(this.IconUrl[11..]);
191 else if (this.IconUrl.StartsWith("file://", StringComparison.Ordinal))
192 {
193 if (isSvg)
194 this.iconSource = this.IconUrl[7..^4];
195 else
196 this.iconSource = this.IconUrl[7..];
197 }
198 else if (Uri.TryCreate(this.IconUrl, UriKind.Absolute, out Uri? ParsedUri))
199 {
200 if (isSvg)
201 this.iconSource = await ServiceRef.UiService.ConvertSvgUriToImageSource(this.IconUrl);
202 else
203 this.iconSource = ImageSource.FromUri(ParsedUri);
204 }
205 else
206 {
207 //if it is an SVG, remove the extension
208 //Maui does not inheritly support SVGs, but can implicitly convert them to png
209 if (isSvg)
210 this.iconSource = ImageSource.FromFile(this.IconUrl[..^4]);
211 else
212 this.iconSource = ImageSource.FromFile(this.IconUrl);
213 }
214 }
215 catch (Exception ex)
216 {
217 ServiceRef.LogService.LogException(ex);
218 }
219
220 MainThread.BeginInvokeOnMainThread(() => this.OnPropertyChanged(nameof(this.IconUrlSource)));
221 }
222
226 [RelayCommand]
227 private Task SelectServiceProvider()
228 {
229 return this.parent.SelectServiceProvider(this);
230 }
231 }
232}
Represents an instance of the Neuro-Access app.
Definition: App.xaml.cs:125
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
Interface for information about a service provider.
Definition: App.xaml.cs:4