Skip to content

Commit 19aa423

Browse files
committed
Use api functions in ImageLoader
1 parent f99859a commit 19aa423

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77
using System.Threading.Tasks;
88
using System.Windows.Media;
99
using System.Windows.Media.Imaging;
10-
using Flow.Launcher.Infrastructure.Logger;
10+
using CommunityToolkit.Mvvm.DependencyInjection;
1111
using Flow.Launcher.Infrastructure.Storage;
12+
using Flow.Launcher.Plugin;
1213

1314
namespace Flow.Launcher.Infrastructure.Image
1415
{
1516
public static class ImageLoader
1617
{
18+
// We should not initialize API in static constructor because it will create another API instance
19+
private static IPublicAPI api = null;
20+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
21+
22+
private static readonly string ClassName = nameof(ImageLoader);
23+
1724
private static readonly ImageCache ImageCache = new();
1825
private static SemaphoreSlim storageLock { get; } = new SemaphoreSlim(1, 1);
1926
private static BinaryStorage<List<(string, bool)>> _storage;
@@ -47,15 +54,14 @@ public static async Task InitializeAsync()
4754

4855
_ = Task.Run(async () =>
4956
{
50-
await Stopwatch.NormalAsync("|ImageLoader.Initialize|Preload images cost", async () =>
57+
await API.StopwatchLogInfoAsync(ClassName, "Preload images cost", async () =>
5158
{
5259
foreach (var (path, isFullImage) in usage)
5360
{
5461
await LoadAsync(path, isFullImage);
5562
}
5663
});
57-
Log.Info(
58-
$"|ImageLoader.Initialize|Number of preload images is <{ImageCache.CacheSize()}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
64+
API.LogInfo(ClassName, $"Number of preload images is <{ImageCache.CacheSize()}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
5965
});
6066
}
6167

@@ -71,7 +77,7 @@ await _storage.SaveAsync(ImageCache.EnumerateEntries()
7177
}
7278
catch (System.Exception e)
7379
{
74-
Log.Exception($"|ImageLoader.SaveAsync|Failed to save image cache to file", e);
80+
API.LogException(ClassName, "Failed to save image cache to file", e);
7581
}
7682
finally
7783
{
@@ -166,8 +172,8 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
166172
}
167173
catch (System.Exception e2)
168174
{
169-
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e);
170-
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2);
175+
API.LogException(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e);
176+
API.LogException(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2);
171177

172178
ImageSource image = ImageCache[Constant.MissingImgIcon, false];
173179
ImageCache[path, false] = image;

0 commit comments

Comments
 (0)