1using CommunityToolkit.Mvvm.Input;
13 public partial class ServiceProviderViewModel(IServiceProvider ServiceProvider, int IconHeight, ServiceProvidersViewModel Parent)
16 private readonly IServiceProvider serviceProvider = ServiceProvider;
17 private readonly ServiceProvidersViewModel parent = Parent;
18 private ImageSource? iconSource;
24 private const double WideAspectThreshold = 2;
29 public IServiceProvider ServiceProvider => this.serviceProvider;
34 public string Id => this.serviceProvider.Id;
39 public string Name => this.serviceProvider.Name;
44 public bool HasIcon => !
string.IsNullOrEmpty(this.serviceProvider.IconUrl);
49 public string IconUrl => this.serviceProvider.IconUrl;
54 private bool HasValidIconDimensions => this.serviceProvider.IconWidth > 0 && this.serviceProvider.IconHeight > 0;
59 private double IconAspectRatio
63 if (!this.HasValidIconDimensions)
66 return (
double)this.serviceProvider.IconWidth / this.serviceProvider.IconHeight;
73 public bool IsWideHero => this.HasIcon && this.HasValidIconDimensions && this.IconAspectRatio >= WideAspectThreshold;
78 public bool ShowImage => this.HasIcon;
87 public bool ShowText =>
90 this.serviceProvider.GetType().Assembly == typeof(
App).Assembly;
95 public int IconHeight {
get; } = IconHeight;
107 if (!this.HasIcon || this.serviceProvider.IconHeight == 0)
110 double s = ((double)this.IconHeight) / this.serviceProvider.IconHeight;
112 return (
int)(this.serviceProvider.IconWidth * s + 0.5);
122 public int DisplayIconHeight
131 return this.IconHeight;
141 public int DisplayIconWidth
150 return this.IconHeight;
153 return this.IconWidth;
157 private bool hasRun =
false;
162 public ImageSource? IconUrlSource
166 if (this.iconSource is
null && !this.hasRun)
169 Task.Run(this.UpdateIconUrlSourceAsync);
172 return this.iconSource;
180 public async Task UpdateIconUrlSourceAsync()
182 if (this.iconSource is not
null)
return;
187 bool isSvg = this.IconUrl.EndsWith(
".svg", StringComparison.OrdinalIgnoreCase);
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))
194 this.iconSource = this.IconUrl[7..^4];
196 this.iconSource = this.IconUrl[7..];
198 else if (Uri.TryCreate(
this.IconUrl, UriKind.Absolute, out Uri? ParsedUri))
203 this.iconSource = ImageSource.FromUri(ParsedUri);
210 this.iconSource = ImageSource.FromFile(this.IconUrl[..^4]);
212 this.iconSource = ImageSource.FromFile(this.IconUrl);
220 MainThread.BeginInvokeOnMainThread(() => this.OnPropertyChanged(nameof(this.IconUrlSource)));
227 private Task SelectServiceProvider()
229 return this.parent.SelectServiceProvider(
this);
Represents an instance of the Neuro-Access app.
Base class that references services in the app.
static ILogService LogService
Log service.
static IUiService UiService
Service serializing and managing UI-related tasks.
Interface for information about a service provider.