Skip to content
This repository was archived by the owner on Jul 2, 2022. It is now read-only.

Commit d4168a9

Browse files
committed
Fixed crashes in events and notifications. Fixed caching issue for notification number in Menu
1 parent 89b17b4 commit d4168a9

3 files changed

Lines changed: 56 additions & 52 deletions

File tree

CodeHub.Core/ViewModels/App/MenuViewModel.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ public ICommand LoadCommand
156156
get { return new MvxCommand(Load);}
157157
}
158158

159-
private async void Load()
159+
private void Load()
160160
{
161-
this.GetApplication().Client.ExecuteAsync(this.GetApplication().Client.Notifications.GetAll()).ContinueWith(x =>
161+
var notificationRequest = this.GetApplication().Client.Notifications.GetAll();
162+
notificationRequest.RequestFromCache = false;
163+
this.GetApplication().Client.ExecuteAsync(notificationRequest).ContinueWith(x =>
162164
{
163165
Notifications = x.Result.Data.Count;
164166
}, TaskContinuationOptions.OnlyOnRanToCompletion).FireAndForget();

CodeHub.Core/ViewModels/Events/BaseEventsViewModel.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,29 @@ protected BaseEventsViewModel()
3737
ReportRepository = true;
3838
}
3939

40-
protected override Task Load(bool forceDataRefresh)
41-
{
42-
return Task.Run(() => this.RequestModel(CreateRequest(0, 100), forceDataRefresh, response => {
40+
protected override Task Load(bool forceCacheInvalidation)
41+
{
42+
return this.RequestModel(CreateRequest(0, 100), forceCacheInvalidation, response => {
4343
this.CreateMore(response, m => Events.MoreItems = m, d => Events.Items.AddRange(CreateDataFromLoad(d)));
4444
Events.Items.Reset(CreateDataFromLoad(response.Data));
45-
}));
45+
});
4646
}
4747

48-
private IEnumerable<Tuple<EventModel, EventBlock>> CreateDataFromLoad(IEnumerable<EventModel> events)
48+
private IEnumerable<Tuple<EventModel, EventBlock>> CreateDataFromLoad(List<EventModel> events)
4949
{
50-
return events.Select(x => new Tuple<EventModel, EventBlock>(x, CreateEventTextBlocks(x)));
50+
var transformedEvents = new List<Tuple<EventModel, EventBlock>>(events.Count);
51+
foreach (var e in events)
52+
{
53+
try
54+
{
55+
transformedEvents.Add(new Tuple<EventModel, EventBlock>(e, CreateEventTextBlocks(e)));
56+
}
57+
catch (Exception ex)
58+
{
59+
System.Diagnostics.Debug.WriteLine(ex.ToString());
60+
}
61+
}
62+
return transformedEvents;
5163
}
5264

5365
protected abstract GitHubRequest<List<EventModel>> CreateRequest(int page, int perPage);

CodeHub.Core/ViewModels/NotificationsViewModel.cs

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using CodeHub.Core.ViewModels.PullRequests;
1111
using GitHubSharp.Models;
1212
using CodeHub.Core.Messages;
13+
using System.Collections.Generic;
1314

1415
namespace CodeHub.Core.ViewModels
1516
{
@@ -54,12 +55,12 @@ public ICommand ReadCommand
5455

5556
public ICommand ReadRepositoriesCommand
5657
{
57-
get { return _readReposCommand ?? (_readReposCommand = new MvxCommand<string>(MarkRepoAsRead)); }
58+
get { return _readReposCommand ?? (_readReposCommand = new MvxCommand<string>(x => MarkRepoAsRead(x))); }
5859
}
5960

6061
public ICommand ReadAllCommand
6162
{
62-
get { return _readAllCommand ?? (_readAllCommand = new MvxCommand(MarkAllAsRead, () => ShownIndex != 2 && !IsLoading && !IsMarking && Notifications.Any())); }
63+
get { return _readAllCommand ?? (_readAllCommand = new MvxCommand(() => MarkAllAsRead(), () => ShownIndex != 2 && !IsLoading && !IsMarking && Notifications.Any())); }
6364
}
6465

6566
public ICommand GoToNotificationCommand
@@ -125,60 +126,45 @@ private async void Read(NotificationModel model)
125126
// If its already read, ignore it
126127
if (!model.Unread)
127128
return;
128-
129-
var response = await this.GetApplication().Client.ExecuteAsync(this.GetApplication().Client.Notifications[model.Id].MarkAsRead());
130-
if (response.Data)
131-
{
132-
//We just read it
133-
model.Unread = false;
134-
135-
//Update the notifications count on the account
136-
Notifications.Items.Remove(model);
137-
UpdateAccountNotificationsCount();
138-
}
139-
}
140-
141-
private async void MarkRepoAsRead(string repo)
142-
{
143-
var items = Notifications.Items.Where(x => string.Equals(x.Repository.FullName, repo, StringComparison.OrdinalIgnoreCase)).ToList();
144-
145129
try
146130
{
147-
IsMarking = true;
148-
149-
foreach (var notification in items)
150-
{
151-
try
152-
{
153-
await this.GetApplication().Client.ExecuteAsync(this.GetApplication().Client.Notifications[notification.Id].MarkAsRead());
154-
notification.Unread = false;
155-
}
156-
catch
157-
{
158-
//Ignore?
159-
}
160-
}
161-
162-
Notifications.Items.RemoveRange(items);
163-
UpdateAccountNotificationsCount();
131+
var response = await this.GetApplication().Client.ExecuteAsync(this.GetApplication().Client.Notifications[model.Id].MarkAsRead());
132+
if (response.Data)
133+
{
134+
//We just read it
135+
model.Unread = false;
136+
137+
//Update the notifications count on the account
138+
Notifications.Items.Remove(model);
139+
UpdateAccountNotificationsCount();
140+
}
164141
}
165-
finally
142+
catch (Exception e)
166143
{
167-
IsMarking = false;
168-
}
144+
ReportError(e);
145+
}
146+
}
147+
148+
private async Task MarkRepoAsRead(string repo)
149+
{
150+
var items = Notifications.Items.Where(x => string.Equals(x.Repository.FullName, repo, StringComparison.OrdinalIgnoreCase)).ToList();
151+
await MarkNotificationsAsRead(items);
169152
}
170153

171-
private async void MarkAllAsRead()
154+
private async Task MarkAllAsRead()
172155
{
173156
// Make sure theres some sort of notification
174-
if (!Notifications.Any())
175-
return;
157+
if (Notifications.Any())
158+
await MarkNotificationsAsRead(Notifications);
159+
}
176160

161+
private async Task MarkNotificationsAsRead(IEnumerable<NotificationModel> notifications)
162+
{
177163
try
178164
{
179165
IsMarking = true;
180166

181-
foreach (var notification in Notifications)
167+
foreach (var notification in notifications)
182168
{
183169
try
184170
{
@@ -194,11 +180,15 @@ private async void MarkAllAsRead()
194180
Notifications.Items.Clear();
195181
UpdateAccountNotificationsCount();
196182
}
183+
catch (Exception e)
184+
{
185+
ReportError(e);
186+
}
197187
finally
198188
{
199189
IsMarking = false;
200190
}
201-
}
191+
}
202192

203193
private void UpdateAccountNotificationsCount()
204194
{

0 commit comments

Comments
 (0)