/
CommentsPageViewModel.cs
129 lines (105 loc) · 3.56 KB
/
CommentsPageViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using Caliburn.Micro;
using Sheepsteak.EchoesJS.Core;
using Sheepsteak.EchoesJS.UI.Framework;
using System.Collections.Generic;
using System.Net.Http;
using System.Linq;
using System.Threading.Tasks;
using Sheepsteak.EchoesJS.UI.Resources;
namespace Sheepsteak.EchoesJS.UI.Features.Articles
{
public class CommentsPageViewModel : Screen
{
private bool isBusy;
private readonly EchoJsClient echoJsClient;
private readonly IEventAggregator eventAggregator;
public CommentsPageViewModel(
EchoJsClient echoJsClient,
IEventAggregator eventAggregator)
{
this.echoJsClient = echoJsClient;
this.eventAggregator = eventAggregator;
this.Comments = new BindableCollection<Comment>();
}
public int ArticleId { get; set; }
public string ArticleTitle { get; set; }
public BindableCollection<Comment> Comments { get; private set; }
public bool IsBusy
{
get { return this.isBusy; }
private set
{
this.isBusy = value;
this.NotifyOfPropertyChange(() => this.IsBusy);
this.NotifyOfPropertyChange(() => this.ShowNoCommentsMessage);
}
}
public bool ShowComments
{
get { return this.Comments.Any(); }
}
public bool ShowNoCommentsMessage
{
get { return !this.IsBusy && !this.Comments.Any(); }
}
public void OpenInBrowser()
{
//this.eventAggregator.RequestTask<WebBrowserTask>(task =>
//{
// task.Uri = new Uri(this.Article.Url);
//});
}
protected async override void OnInitialize()
{
base.OnInitialize();
await this.GetComments();
}
private async Task GetComments()
{
this.IsBusy = true;
IEnumerable<Comment> comments = null;
bool showFailMessage = false;
try
{
comments = await this.echoJsClient.GetCommentsForArticle(this.ArticleId);
comments = this.FlattenComments(comments);
}
catch (HttpRequestException)
{
showFailMessage = true;
}
catch (UnsupportedMediaTypeException)
{
showFailMessage = true;
}
this.IsBusy = false;
if (showFailMessage)
{
await Task.Delay(50);
var showMessageBoxResult = new ShowMessageBoxResult(
"There was an error trying to get the comments.",
AppResources.NetworkErrorMessageBoxCaption);
await showMessageBoxResult.ExecuteAsync();
}
else
{
this.Comments.AddRange(comments);
}
this.NotifyOfPropertyChange(() => this.ShowNoCommentsMessage);
this.NotifyOfPropertyChange(() => this.ShowComments);
}
private IEnumerable<Comment> FlattenComments(IEnumerable<Comment> comments)
{
var flattenedComments = new List<Comment>();
foreach (var comment in comments)
{
flattenedComments.Add(comment);
if (comment.Replies != null && comment.Replies.Any())
{
flattenedComments.AddRange(this.FlattenComments(comment.Replies));
}
}
return flattenedComments;
}
}
}