-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistry.cs
67 lines (52 loc) · 1.9 KB
/
Registry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Blazored.LocalStorage;
using Microsoft.Extensions.Options;
using SysAdminsMedia.BlazorIconify.Extensions;
namespace SysAdminsMedia.BlazorIconify;
public sealed class Registry(IOptions<IconifyOptions> options, ILocalStorageService localStorage)
{
private const string CachedIconsKey = "cached-icons";
private List<IconMetaData> _icons = [];
public string GetApiUrl()
{
return options.Value.ApiUrl ?? "https://api.iconify.design/";
}
public string GetDefaultColor()
{
return options.Value.DefaultColor ?? string.Empty;
}
public string GetErrorIcon()
{
return options.Value.ErrorIcon ?? "ic:baseline-do-not-disturb";
}
public async Task AddIcon(IconMetaData metadata)
{
if(string.IsNullOrEmpty(metadata.Name)) return;
if (IsRegistered(metadata.Name)) return;
_icons.Add(metadata);
await localStorage.SetItemAsync(CachedIconsKey, _icons);
}
public async Task<IconMetaData?> GetIcon(string icon, string? color = "")
{
if (string.IsNullOrEmpty(icon)) return null;
var icons = await GetCachedIcons();
return icons.FirstOrDefault(x => x.Name == icon && x.Color == color);
}
public async Task<bool> IsCached(string icon, string? color = "")
{
if (string.IsNullOrEmpty(icon)) return false;
var icons = await GetCachedIcons();
return icons.Exists(x => x.Name == icon && x.Color == color);
}
public async Task Clear()
{
_icons.Clear();
await localStorage.RemoveItemAsync(CachedIconsKey);
}
private async Task<List<IconMetaData>> GetCachedIcons()
{
if (_icons.Count > 0) return _icons;
return _icons = await localStorage.GetItemAsync<List<IconMetaData>>(CachedIconsKey) ?? [];
}
private bool IsRegistered(string icon) =>
_icons.Exists(x => x.Name == icon);
}