Skip to content

Commit a7310d5

Browse files
committed
Use Customized LazyAsync class to load image instead of the weird way of updating lazy class async
1 parent 99086e2 commit a7310d5

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public static class ImageLoader
1818
private static readonly ConcurrentDictionary<string, string> GuidToKey = new ConcurrentDictionary<string, string>();
1919
private static IImageHashGenerator _hashGenerator;
2020
private static bool EnableImageHash = true;
21+
public static ImageSource defaultImage { get; } = new BitmapImage(new Uri(Constant.MissingImgIcon));
22+
2123

2224
private static readonly string[] ImageExtensions =
2325
{
@@ -37,6 +39,7 @@ public static void Initialize()
3739

3840
var usage = LoadStorageToConcurrentDictionary();
3941

42+
4043
foreach (var icon in new[] { Constant.DefaultIcon, Constant.MissingImgIcon })
4144
{
4245
ImageSource img = new BitmapImage(new Uri(icon));
@@ -213,13 +216,10 @@ private static BitmapSource GetThumbnail(string path, ThumbnailOptions option =
213216

214217
public static bool CacheContainImage(string path)
215218
{
216-
return ImageCache.ContainsKey(path);
217-
}
218-
public static ImageSource LoadDefault(bool loadFullImage = false)
219-
{
220-
return LoadInternal(Constant.MissingImgIcon, loadFullImage).ImageSource;
219+
return ImageCache.ContainsKey(path) && ImageCache[path] != null;
221220
}
222221

222+
223223
public static ImageSource Load(string path, bool loadFullImage = false)
224224
{
225225
var imageResult = LoadInternal(path, loadFullImage);
@@ -230,7 +230,7 @@ public static ImageSource Load(string path, bool loadFullImage = false)
230230
string hash = EnableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
231231
if (hash != null)
232232
{
233-
233+
234234
if (GuidToKey.TryGetValue(hash, out string key))
235235
{ // image already exists
236236
img = ImageCache[key] ?? img;

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,57 @@
88
using Flow.Launcher.Infrastructure.Logger;
99
using Flow.Launcher.Infrastructure.UserSettings;
1010
using Flow.Launcher.Plugin;
11-
11+
using Microsoft.FSharp.Core;
1212

1313
namespace Flow.Launcher.ViewModel
1414
{
1515
public class ResultViewModel : BaseModel
1616
{
17+
public class LazyAsync<T> : Lazy<Task<T>>
18+
{
19+
private T defaultValue;
20+
21+
22+
private readonly Action _updateCallback;
23+
public T Value
24+
{
25+
get
26+
{
27+
if (!IsValueCreated)
28+
{
29+
base.Value.ContinueWith(_ =>
30+
{
31+
_updateCallback();
32+
});
33+
return defaultValue;
34+
}
35+
else if (!base.Value.IsCompleted)
36+
{
37+
return defaultValue;
38+
}
39+
else return base.Value.Result;
40+
}
41+
}
42+
public LazyAsync(Func<Task<T>> factory, T defaultValue, Action updateCallback) : base(factory)
43+
{
44+
if (defaultValue != null)
45+
{
46+
this.defaultValue = defaultValue;
47+
}
48+
_updateCallback = updateCallback;
49+
50+
}
51+
}
52+
1753
public ResultViewModel(Result result, Settings settings)
1854
{
1955
if (result != null)
2056
{
2157
Result = result;
22-
Image = new Lazy<ImageSource>(SetImage);
58+
Image = new LazyAsync<ImageSource>(SetImage, ImageLoader.defaultImage, () =>
59+
{
60+
OnPropertyChanged(nameof(Image));
61+
});
2362
}
2463

2564
Settings = settings;
@@ -39,9 +78,9 @@ public ResultViewModel(Result result, Settings settings)
3978
? Result.SubTitle
4079
: Result.SubTitleToolTip;
4180

42-
public Lazy<ImageSource> Image { get; set; }
81+
public LazyAsync<ImageSource> Image { get; set; }
4382

44-
private ImageSource SetImage()
83+
private async Task<ImageSource> SetImage()
4584
{
4685
var imagePath = Result.IcoPath;
4786
if (string.IsNullOrEmpty(imagePath) && Result.Icon != null)
@@ -62,14 +101,9 @@ private ImageSource SetImage()
62101
return ImageLoader.Load(imagePath);
63102
else
64103
{
65-
Task.Run(() =>
66-
{
67-
Image = new Lazy<ImageSource>(() => ImageLoader.Load(imagePath));
68-
OnPropertyChanged(nameof(Image));
69-
});
70-
71-
return ImageLoader.LoadDefault();
104+
return await Task.Run(() => ImageLoader.Load(imagePath));
72105
}
106+
73107
}
74108

75109
public Result Result { get; }

0 commit comments

Comments
 (0)