Skip to content

Commit e8691c2

Browse files
committed
No need for the wrapped Lazy instance in image loading.
1 parent d813a47 commit e8691c2

File tree

2 files changed

+27
-67
lines changed

2 files changed

+27
-67
lines changed

Flow.Launcher/ResultListBox.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<ColumnDefinition Width="0" />
4343
</Grid.ColumnDefinitions>
4444
<Image x:Name="ImageIcon" Width="32" Height="32" HorizontalAlignment="Left"
45-
Source="{Binding Image.Value}" />
45+
Source="{Binding Image}" />
4646
<Grid Margin="5 0 5 0" Grid.Column="1" HorizontalAlignment="Stretch">
4747
<Grid.RowDefinitions>
4848
<RowDefinition />

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,12 @@ namespace Flow.Launcher.ViewModel
1111
{
1212
public class ResultViewModel : BaseModel
1313
{
14-
public class LazyAsync<T> : Lazy<ValueTask<T>>
15-
{
16-
private readonly T defaultValue;
17-
18-
private readonly Action _updateCallback;
19-
public new T Value
20-
{
21-
get
22-
{
23-
if (!IsValueCreated)
24-
{
25-
_ = Exercute(); // manually use callback strategy
26-
27-
return defaultValue;
28-
}
29-
30-
if (!base.Value.IsCompletedSuccessfully)
31-
return defaultValue;
32-
33-
return base.Value.Result;
34-
35-
// If none of the variables captured by the local function are captured by other lambdas,
36-
// the compiler can avoid heap allocations.
37-
async ValueTask Exercute()
38-
{
39-
await base.Value.ConfigureAwait(false);
40-
_updateCallback();
41-
}
42-
43-
}
44-
}
45-
public LazyAsync(Func<ValueTask<T>> factory, T defaultValue, Action updateCallback) : base(factory)
46-
{
47-
if (defaultValue != null)
48-
{
49-
this.defaultValue = defaultValue;
50-
}
51-
52-
_updateCallback = updateCallback;
53-
}
54-
}
55-
5614
public ResultViewModel(Result result, Settings settings)
5715
{
5816
if (result != null)
5917
{
6018
Result = result;
61-
62-
Image = new LazyAsync<ImageSource>(
63-
SetImage,
64-
ImageLoader.DefaultImage,
65-
() =>
66-
{
67-
OnPropertyChanged(nameof(Image));
68-
});
69-
}
19+
}
7020

7121
Settings = settings;
7222
}
@@ -85,44 +35,54 @@ public ResultViewModel(Result result, Settings settings)
8535
? Result.SubTitle
8636
: Result.SubTitleToolTip;
8737

88-
public LazyAsync<ImageSource> Image { get; set; }
38+
private bool ImageLoaded;
39+
40+
private ImageSource image = ImageLoader.DefaultImage;
8941

90-
private async ValueTask<ImageSource> SetImage()
42+
public ImageSource Image
43+
{
44+
get
45+
{
46+
if (!ImageLoaded)
47+
{
48+
ImageLoaded = true;
49+
LoadImage();
50+
}
51+
return image;
52+
}
53+
private set => image = value;
54+
}
55+
private async void LoadImage()
9156
{
9257
var imagePath = Result.IcoPath;
9358
if (string.IsNullOrEmpty(imagePath) && Result.Icon != null)
9459
{
9560
try
9661
{
97-
return Result.Icon();
62+
Image = Result.Icon();
63+
return;
9864
}
9965
catch (Exception e)
10066
{
10167
Log.Exception($"|ResultViewModel.Image|IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", e);
102-
return ImageLoader.DefaultImage;
10368
}
10469
}
10570

10671
if (ImageLoader.CacheContainImage(imagePath))
72+
{
10773
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
108-
return ImageLoader.Load(imagePath);
74+
Image = ImageLoader.Load(imagePath);
75+
return;
76+
}
10977

110-
return await Task.Run(() => ImageLoader.Load(imagePath));
78+
Image = await Task.Run(() => ImageLoader.Load(imagePath)).ConfigureAwait(false);
11179
}
11280

11381
public Result Result { get; }
11482

11583
public override bool Equals(object obj)
11684
{
117-
var r = obj as ResultViewModel;
118-
if (r != null)
119-
{
120-
return Result.Equals(r.Result);
121-
}
122-
else
123-
{
124-
return false;
125-
}
85+
return obj is ResultViewModel r && Result.Equals(r.Result);
12686
}
12787

12888
public override int GetHashCode()

0 commit comments

Comments
 (0)