8
8
using Flow . Launcher . Infrastructure . Logger ;
9
9
using Flow . Launcher . Infrastructure . UserSettings ;
10
10
using Flow . Launcher . Plugin ;
11
-
11
+ using Microsoft . FSharp . Core ;
12
12
13
13
namespace Flow . Launcher . ViewModel
14
14
{
15
15
public class ResultViewModel : BaseModel
16
16
{
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
+
17
53
public ResultViewModel ( Result result , Settings settings )
18
54
{
19
55
if ( result != null )
20
56
{
21
57
Result = result ;
22
- Image = new Lazy < ImageSource > ( SetImage ) ;
58
+ Image = new LazyAsync < ImageSource > ( SetImage , ImageLoader . defaultImage , ( ) =>
59
+ {
60
+ OnPropertyChanged ( nameof ( Image ) ) ;
61
+ } ) ;
23
62
}
24
63
25
64
Settings = settings ;
@@ -39,9 +78,9 @@ public ResultViewModel(Result result, Settings settings)
39
78
? Result . SubTitle
40
79
: Result . SubTitleToolTip ;
41
80
42
- public Lazy < ImageSource > Image { get ; set ; }
81
+ public LazyAsync < ImageSource > Image { get ; set ; }
43
82
44
- private ImageSource SetImage ( )
83
+ private async Task < ImageSource > SetImage ( )
45
84
{
46
85
var imagePath = Result . IcoPath ;
47
86
if ( string . IsNullOrEmpty ( imagePath ) && Result . Icon != null )
@@ -62,14 +101,9 @@ private ImageSource SetImage()
62
101
return ImageLoader . Load ( imagePath ) ;
63
102
else
64
103
{
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 ) ) ;
72
105
}
106
+
73
107
}
74
108
75
109
public Result Result { get ; }
0 commit comments