-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.cs
50 lines (38 loc) · 1.45 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
using Blazored.LocalStorage;
namespace SysAdminsMedia.BlazorIconify;
public sealed class Registry(ILocalStorageService LocalStorage)
{
private const string CachedIconsKey = "cached-icons";
private List<IconMetaData> _icons = [];
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);
}