Skip to content

Commit aeb72d2

Browse files
Separate image loading logic
1. Separate image loading logic 2. Make IconDelegate async while calling to avoid blocking UI thread
1 parent 5a7eb62 commit aeb72d2

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,50 +173,53 @@ public ImageSource PreviewImage
173173

174174
public GlyphInfo Glyph { get; set; }
175175

176-
private async Task LoadImageAsync()
176+
private async Task<ImageSource> LoadImageInternalAsync(string imagePath, Result.IconDelegate icon, bool loadFullImage)
177177
{
178-
var imagePath = Result.IcoPath;
179-
if (string.IsNullOrEmpty(imagePath) && Result.Icon != null)
178+
if (string.IsNullOrEmpty(imagePath) && icon != null)
180179
{
181180
try
182181
{
183-
image = Result.Icon();
184-
return;
182+
var image = await Task.Run(() => icon()).ConfigureAwait(false);
183+
return image;
185184
}
186185
catch (Exception e)
187186
{
188187
Log.Exception(
189-
$"|ResultViewModel.Image|IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>",
188+
$"|ResultViewModel.LoadImageInternalAsync|IcoPath is empty and exception when calling IconDelegate for result <{Result.Title}> of plugin <{Result.PluginDirectory}>",
190189
e);
191190
}
192191
}
193192

194-
var loadFullImage = (Path.GetExtension(imagePath) ?? "").Equals(".url", StringComparison.OrdinalIgnoreCase);
193+
return await ImageLoader.LoadAsync(imagePath, loadFullImage).ConfigureAwait(false);
194+
}
195195

196-
if (ImageLoader.CacheContainImage(imagePath))
196+
private async Task LoadImageAsync()
197+
{
198+
var imagePath = Result.IcoPath;
199+
var iconDelegate = Result.Icon;
200+
if (ImageLoader.CacheContainImage(imagePath, false))
197201
{
198-
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
199-
image = await ImageLoader.LoadAsync(imagePath, loadFullImage).ConfigureAwait(false);
200-
return;
202+
image = await LoadImageInternalAsync(imagePath, iconDelegate, false).ConfigureAwait(false);
203+
}
204+
else
205+
{
206+
// We need to modify the property not field here to trigger the OnPropertyChanged event
207+
Image = await LoadImageInternalAsync(imagePath, iconDelegate, false).ConfigureAwait(false);
201208
}
202-
203-
// We need to modify the property not field here to trigger the OnPropertyChanged event
204-
Image = await ImageLoader.LoadAsync(imagePath, loadFullImage).ConfigureAwait(false);
205209
}
206210

207-
208211
private async Task LoadPreviewImageAsync()
209212
{
210213
var imagePath = Result.PreviewImage ?? Result.IcoPath;
211-
if (imagePath == null && Result.Icon != null)
214+
var iconDelegate = Result.Icon;
215+
if (ImageLoader.CacheContainImage(imagePath, true))
212216
{
213-
// For UWP programs from program plugin
214-
// TODO: Consider https://github.com/Flow-Launcher/Flow.Launcher/pull/1492#issuecomment-1304829947
215-
PreviewImage = Result.Icon();
217+
previewImage = await LoadImageInternalAsync(imagePath, iconDelegate, true).ConfigureAwait(false);
216218
}
217219
else
218220
{
219-
PreviewImage = await ImageLoader.LoadAsync(imagePath, true).ConfigureAwait(false);
221+
// We need to modify the property not field here to trigger the OnPropertyChanged event
222+
PreviewImage = await LoadImageInternalAsync(imagePath, iconDelegate, true).ConfigureAwait(false);
220223
}
221224
}
222225

0 commit comments

Comments
 (0)