You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.1 KiB
72 lines
2.1 KiB
using AuroraDesk.Core.Interfaces;
|
|
using AuroraDesk.Core.Entities;
|
|
using HeroIconsAvalonia.Enums;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace AuroraDesk.Infrastructure.Services;
|
|
|
|
/// <summary>
|
|
/// 图标服务实现
|
|
/// </summary>
|
|
public class IconService : IIconService
|
|
{
|
|
private readonly ILogger<IconService>? _logger;
|
|
private IEnumerable<HeroIconItem>? _cachedIcons;
|
|
|
|
public IconService(ILogger<IconService>? logger = null)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public IEnumerable<HeroIconItem> GetIcons()
|
|
{
|
|
// 如果缓存存在,直接返回
|
|
if (_cachedIcons != null)
|
|
{
|
|
return _cachedIcons;
|
|
}
|
|
|
|
// 缓存不存在,同步创建(操作很快,不需要异步)
|
|
try
|
|
{
|
|
_logger?.LogInformation("开始加载 HeroIcons 图标");
|
|
|
|
// 优化:使用 LINQ 批量创建,比循环更快
|
|
var iconTypes = Enum.GetValues(typeof(IconType)).Cast<IconType>().ToArray();
|
|
|
|
// 预分配容量,避免多次扩容
|
|
var icons = new List<HeroIconItem>(iconTypes.Length * 2);
|
|
|
|
// 使用 LINQ Select 批量创建,性能更好
|
|
var outlineIcons = iconTypes.Select(iconType => new HeroIconItem
|
|
{
|
|
IconType = iconType,
|
|
IconKind = IconKind.Outline
|
|
});
|
|
|
|
var solidIcons = iconTypes.Select(iconType => new HeroIconItem
|
|
{
|
|
IconType = iconType,
|
|
IconKind = IconKind.Solid
|
|
});
|
|
|
|
// 合并两个集合
|
|
icons.AddRange(outlineIcons);
|
|
icons.AddRange(solidIcons);
|
|
|
|
_cachedIcons = icons;
|
|
_logger?.LogInformation("成功加载 {Count} 个 HeroIcons 图标", icons.Count);
|
|
|
|
return icons;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger?.LogError(ex, "加载 HeroIcons 图标时发生错误");
|
|
return Enumerable.Empty<HeroIconItem>();
|
|
}
|
|
}
|
|
}
|
|
|
|
|