Skip to content

Commit e83e21d

Browse files
Add TryGetValue for ImageLoader
1 parent 35d96bd commit e83e21d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Flow.Launcher.Infrastructure/Image/ImageCache.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ public bool ContainsKey(string key, bool isFullImage)
8787
return key is not null && Data.ContainsKey((key, isFullImage)) && Data[(key, isFullImage)].imageSource != null;
8888
}
8989

90+
public bool TryGetValue(string key, bool isFullImage, out ImageSource image)
91+
{
92+
if (key is not null)
93+
{
94+
bool hasKey = Data.TryGetValue((key, isFullImage), out var imageUsage);
95+
image = hasKey ? imageUsage.imageSource : null;
96+
return hasKey;
97+
}
98+
else
99+
{
100+
image = null;
101+
return false;
102+
}
103+
}
104+
90105
public int CacheSize()
91106
{
92107
return Data.Count;

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ public static bool CacheContainImage(string path, bool loadFullImage = false)
251251
return ImageCache.ContainsKey(path, false) && ImageCache[path, loadFullImage] != null;
252252
}
253253

254+
public static bool TryGetValue(string path, bool loadFullImage, out ImageSource image)
255+
{
256+
return ImageCache.TryGetValue(path, loadFullImage, out image);
257+
}
258+
254259
public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullImage = false)
255260
{
256261
var imageResult = await LoadInternalAsync(path, loadFullImage);

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ private async Task LoadImageAsync()
214214
{
215215
var imagePath = Result.IcoPath;
216216
var iconDelegate = Result.Icon;
217-
if (ImageLoader.CacheContainImage(imagePath, false))
217+
if (ImageLoader.TryGetValue(imagePath, false, out ImageSource img))
218218
{
219-
image = await LoadImageInternalAsync(imagePath, iconDelegate, false).ConfigureAwait(false);
219+
image = img;
220220
}
221221
else
222222
{
@@ -229,9 +229,9 @@ private async Task LoadPreviewImageAsync()
229229
{
230230
var imagePath = Result.Preview.PreviewImagePath ?? Result.IcoPath;
231231
var iconDelegate = Result.Preview.PreviewDelegate ?? Result.Icon;
232-
if (ImageLoader.CacheContainImage(imagePath, true))
232+
if (ImageLoader.TryGetValue(imagePath, true, out ImageSource img))
233233
{
234-
previewImage = await LoadImageInternalAsync(imagePath, iconDelegate, true).ConfigureAwait(false);
234+
previewImage = img;
235235
}
236236
else
237237
{

0 commit comments

Comments
 (0)