@@ -11,62 +11,12 @@ namespace Flow.Launcher.ViewModel
11
11
{
12
12
public class ResultViewModel : BaseModel
13
13
{
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
-
56
14
public ResultViewModel ( Result result , Settings settings )
57
15
{
58
16
if ( result != null )
59
17
{
60
18
Result = result ;
61
-
62
- Image = new LazyAsync < ImageSource > (
63
- SetImage ,
64
- ImageLoader . DefaultImage ,
65
- ( ) =>
66
- {
67
- OnPropertyChanged ( nameof ( Image ) ) ;
68
- } ) ;
69
- }
19
+ }
70
20
71
21
Settings = settings ;
72
22
}
@@ -85,44 +35,54 @@ public ResultViewModel(Result result, Settings settings)
85
35
? Result . SubTitle
86
36
: Result . SubTitleToolTip ;
87
37
88
- public LazyAsync < ImageSource > Image { get ; set ; }
38
+ private bool ImageLoaded ;
39
+
40
+ private ImageSource image = ImageLoader . DefaultImage ;
89
41
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 ( )
91
56
{
92
57
var imagePath = Result . IcoPath ;
93
58
if ( string . IsNullOrEmpty ( imagePath ) && Result . Icon != null )
94
59
{
95
60
try
96
61
{
97
- return Result . Icon ( ) ;
62
+ Image = Result . Icon ( ) ;
63
+ return ;
98
64
}
99
65
catch ( Exception e )
100
66
{
101
67
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 ;
103
68
}
104
69
}
105
70
106
71
if ( ImageLoader . CacheContainImage ( imagePath ) )
72
+ {
107
73
// 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
+ }
109
77
110
- return await Task . Run ( ( ) => ImageLoader . Load ( imagePath ) ) ;
78
+ Image = await Task . Run ( ( ) => ImageLoader . Load ( imagePath ) ) . ConfigureAwait ( false ) ;
111
79
}
112
80
113
81
public Result Result { get ; }
114
82
115
83
public override bool Equals ( object obj )
116
84
{
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 ) ;
126
86
}
127
87
128
88
public override int GetHashCode ( )
0 commit comments