@@ -4,53 +4,36 @@ namespace MVUX.Presentation.FeedViewSample;
4
4
5
5
public partial record FeedViewModel
6
6
{
7
- private readonly Random _random = new ( ) ;
8
-
9
- public IFeed < Person > DefaultPerson { get ; }
10
-
11
- public IFeed < Person > CustomLoadingPerson { get ; }
12
-
13
- public IFeed < Person > ErrorPerson { get ; }
14
-
15
- public FeedViewModel ( )
7
+ private static readonly Person [ ] _people = new [ ]
16
8
{
17
- DefaultPerson = Feed . Async ( GetRandomPersonAsync ) ;
9
+ new Person ( "Master" , "Yoda" ) ,
10
+ new Person ( "Darth" , "Vader" ) ,
11
+ new Person ( "Luke" , "Skywalker" ) ,
12
+ } ;
18
13
19
- CustomLoadingPerson = Feed . Async ( GetRandomPersonWithLargerDelayAsync ) ;
14
+ public IFeed < Person > DefaultPerson => Feed . Async ( GetRandomPersonAsync ) ;
20
15
21
- ErrorPerson = Feed . Async ( ThrowErrorAsync ) ;
22
- }
16
+ public IFeed < Person > CustomLoadingPerson => Feed . Async ( GetRandomPersonWithLargerDelayAsync ) ;
17
+
18
+ public IFeed < Person > ErrorPerson => Feed . Async ( ThrowErrorAsync ) ;
23
19
24
20
private async ValueTask < Person > GetRandomPersonAsync ( CancellationToken ct )
25
21
{
22
+ // This simulates a network delay
26
23
await Task . Delay ( 2000 , ct ) ;
27
-
28
- var people = new [ ]
29
- {
30
- new Person ( "Master" , "Yoda" ) ,
31
- new Person ( "Darth" , "Vader" ) ,
32
- new Person ( "Luke" , "Skywalker" ) ,
33
- } ;
34
-
35
- return people [ _random . Next ( 0 , people . Length ) ] ;
24
+ return _people [ Random . Shared . Next ( _people . Length ) ] ;
36
25
}
37
26
38
27
private async ValueTask < Person > GetRandomPersonWithLargerDelayAsync ( CancellationToken ct )
39
28
{
29
+ // This simulates a network delay
40
30
await Task . Delay ( 5000 , ct ) ;
41
-
42
- var people = new [ ]
43
- {
44
- new Person ( "Master" , "Yoda" ) ,
45
- new Person ( "Darth" , "Vader" ) ,
46
- new Person ( "Luke" , "Skywalker" ) ,
47
- } ;
48
-
49
- return people [ _random . Next ( 0 , people . Length ) ] ;
31
+ return _people [ Random . Shared . Next ( _people . Length ) ] ;
50
32
}
51
33
52
34
private async ValueTask < Person > ThrowErrorAsync ( CancellationToken ct )
53
35
{
36
+ // This simulates a network delay
54
37
await Task . Delay ( 2000 , ct ) ;
55
38
throw new Exception ( "An error occurred while fetching the person details." ) ;
56
39
}
0 commit comments