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

Commit 2a2d404

Browse files
committed
Refactor Notifications Data Model
1 parent de16051 commit 2a2d404

2 files changed

Lines changed: 41 additions & 39 deletions

File tree

CodeHub.Core/ViewModels/Notifications/NotificationsViewModel.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
using CodeHub.Core.Filters;
77
using CodeHub.Core.ViewModels.Issues;
88
using CodeHub.Core.ViewModels.PullRequests;
9-
using GitHubSharp.Models;
109
using CodeHub.Core.Messages;
11-
using CodeHub.Core.ViewModels.Source;
1210
using CodeHub.Core.ViewModels.Changesets;
1311
using CodeHub.Core.Utils;
1412
using CodeHub.Core.Services;
@@ -18,15 +16,15 @@ namespace CodeHub.Core.ViewModels.Notifications
1816
{
1917
public class NotificationsViewModel : LoadableViewModel
2018
{
19+
private readonly IApplicationService _applicationService;
2120
private readonly IMessageService _messageService;
22-
private readonly FilterableCollectionViewModel<NotificationModel, NotificationsFilterModel> _notifications;
21+
private readonly FilterableCollectionViewModel<Octokit.Notification, NotificationsFilterModel> _notifications;
2322
private ICommand _readAllCommand;
24-
private ICommand _readCommand;
2523
private ICommand _readReposCommand;
2624
private int _shownIndex;
2725
private bool _isMarking;
2826

29-
public FilterableCollectionViewModel<NotificationModel, NotificationsFilterModel> Notifications
27+
public FilterableCollectionViewModel<Octokit.Notification, NotificationsFilterModel> Notifications
3028
{
3129
get { return _notifications; }
3230
}
@@ -43,58 +41,56 @@ public bool IsMarking
4341
set { this.RaiseAndSetIfChanged(ref _isMarking, value); }
4442
}
4543

46-
public ICommand ReadCommand
47-
{
48-
get { return _readCommand ?? (_readCommand = new MvxCommand<NotificationModel>(x => Read(x)));}
49-
}
50-
5144
public ICommand ReadRepositoriesCommand
5245
{
53-
get { return _readReposCommand ?? (_readReposCommand = new MvxCommand<string>(x => MarkRepoAsRead(x))); }
46+
get { return _readReposCommand ?? (_readReposCommand = new MvxAsyncCommand<string>(x => MarkRepoAsRead(x))); }
5447
}
5548

5649
public ICommand ReadAllCommand
5750
{
58-
get { return _readAllCommand ?? (_readAllCommand = new MvxCommand(() => MarkAllAsRead(), () => ShownIndex != 2 && !IsLoading && !IsMarking && Notifications.Any())); }
51+
get { return _readAllCommand ?? (_readAllCommand = new MvxAsyncCommand(() => MarkAllAsRead(), () => ShownIndex != 2 && !IsLoading && !IsMarking && Notifications.Any())); }
5952
}
6053

6154
public ICommand GoToNotificationCommand
6255
{
63-
get { return new MvxCommand<NotificationModel>(GoToNotification); }
56+
get { return new MvxCommand<Octokit.Notification>(GoToNotification); }
6457
}
6558

66-
private void GoToNotification(NotificationModel x)
59+
private void GoToNotification(Octokit.Notification x)
6760
{
6861
var subject = x.Subject.Type.ToLower();
6962
if (subject.Equals("issue"))
7063
{
71-
ReadCommand.Execute(x);
64+
Read(x).ToBackground();
7265
var node = x.Subject.Url.Substring(x.Subject.Url.LastIndexOf('/') + 1);
7366
ShowViewModel<IssueViewModel>(new IssueViewModel.NavObject { Username = x.Repository.Owner.Login,Repository = x.Repository.Name, Id = long.Parse(node) });
7467
}
7568
else if (subject.Equals("pullrequest"))
7669
{
77-
ReadCommand.Execute(x);
70+
Read(x).ToBackground();
7871
var node = x.Subject.Url.Substring(x.Subject.Url.LastIndexOf('/') + 1);
7972
ShowViewModel<PullRequestViewModel>(new PullRequestViewModel.NavObject { Username = x.Repository.Owner.Login, Repository = x.Repository.Name, Id = long.Parse(node) });
8073
}
8174
else if (subject.Equals("commit"))
8275
{
83-
ReadCommand.Execute(x);
76+
Read(x).ToBackground();
8477
var node = x.Subject.Url.Substring(x.Subject.Url.LastIndexOf('/') + 1);
8578
ShowViewModel<ChangesetViewModel>(new ChangesetViewModel.NavObject { Username = x.Repository.Owner.Login, Repository = x.Repository.Name, Node = node });
8679
}
8780
else if (subject.Equals("release"))
8881
{
89-
ReadCommand.Execute(x);
82+
Read(x).ToBackground();
9083
ShowViewModel<RepositoryViewModel>(new RepositoryViewModel.NavObject { Username = x.Repository.Owner.Login, Repository = x.Repository.Name });
9184
}
9285
}
9386

94-
public NotificationsViewModel(IMessageService messageService = null)
87+
public NotificationsViewModel(
88+
IMessageService messageService = null,
89+
IApplicationService applicationService = null)
9590
{
9691
_messageService = messageService ?? GetService<IMessageService>();
97-
_notifications = new FilterableCollectionViewModel<NotificationModel, NotificationsFilterModel>("Notifications");
92+
_applicationService = applicationService ?? GetService<IApplicationService>();
93+
_notifications = new FilterableCollectionViewModel<Octokit.Notification, NotificationsFilterModel>("Notifications");
9894
_notifications.GroupingFunction = (n) => n.GroupBy(x => x.Repository.FullName);
9995
_notifications.Bind(x => x.Filter).Subscribe(_ => LoadCommand.Execute(false));
10096

@@ -115,32 +111,36 @@ public NotificationsViewModel(IMessageService messageService = null)
115111

116112
}
117113

118-
protected override Task Load()
114+
protected override async Task Load()
119115
{
120-
return this.RequestModel(this.GetApplication().Client.Notifications.GetAll(all: Notifications.Filter.All, participating: Notifications.Filter.Participating), response => {
121-
Notifications.Items.Reset(response.Data);
122-
UpdateAccountNotificationsCount();
123-
});
116+
var req = new Octokit.NotificationsRequest
117+
{
118+
All = Notifications.Filter.All,
119+
Participating = Notifications.Filter.Participating
120+
};
121+
122+
var notifications = await _applicationService.GitHubClient.Activity.Notifications.GetAllForCurrent(req);
123+
Notifications.Items.Reset(notifications);
124+
UpdateAccountNotificationsCount();
124125
}
125126

126-
private async Task Read(NotificationModel model)
127+
private async Task Read(Octokit.Notification model)
127128
{
128129
// If its already read, ignore it
129130
if (!model.Unread)
130131
return;
131132

132133
try
133134
{
134-
var response = await this.GetApplication().Client.ExecuteAsync(this.GetApplication().Client.Notifications[model.Id].MarkAsRead());
135-
if (response.Data)
136-
{
137-
//We just read it
138-
model.Unread = false;
139-
140-
//Update the notifications count on the account
135+
if (!int.TryParse(model.Id, out int id))
136+
return;
137+
138+
await _applicationService.GitHubClient.Activity.Notifications.MarkAsRead(id);
139+
140+
if (_shownIndex != 2)
141141
Notifications.Items.Remove(model);
142-
UpdateAccountNotificationsCount();
143-
}
142+
143+
UpdateAccountNotificationsCount();
144144
}
145145
catch
146146
{
@@ -198,7 +198,7 @@ private void UpdateAccountNotificationsCount()
198198
if (!Notifications.Filter.All && !Notifications.Filter.Participating)
199199
{
200200
var count = Notifications.Items.Sum(x => x.Unread ? 1 : 0);
201-
_messageService.Send<NotificationCountMessage>(new NotificationCountMessage(count));
201+
_messageService.Send(new NotificationCountMessage(count));
202202
}
203203
}
204204
}

CodeHub.iOS/Views/NotificationsView.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using CodeHub.Core.ViewModels.Notifications;
66
using Humanizer;
77
using CodeHub.iOS.DialogElements;
8-
using GitHubSharp.Models;
98
using System.Reactive.Linq;
109

1110
namespace CodeHub.iOS.Views
@@ -44,7 +43,10 @@ public override void ViewDidLoad()
4443

4544
BindCollection(vm.Notifications, x =>
4645
{
47-
var el = new StringElement(x.Subject.Title, x.UpdatedAt.Humanize(), UITableViewCellStyle.Subtitle) { Accessory = UITableViewCellAccessory.DisclosureIndicator };
46+
if (!DateTimeOffset.TryParse(x.UpdatedAt, out DateTimeOffset dt))
47+
dt = DateTimeOffset.Now;
48+
49+
var el = new StringElement(x.Subject.Title, dt.Humanize(), UITableViewCellStyle.Subtitle) { Accessory = UITableViewCellAccessory.DisclosureIndicator };
4850

4951
var subject = x.Subject.Type.ToLower();
5052
if (subject.Equals("issue"))
@@ -74,7 +76,7 @@ public override void ViewDidLoad()
7476
});
7577
}
7678

77-
private static Action<object> MakeCallback(WeakReference<NotificationsViewModel> weakVm, NotificationModel model)
79+
private static Action<object> MakeCallback(WeakReference<NotificationsViewModel> weakVm, Octokit.Notification model)
7880
{
7981
return new Action<object>(_ => weakVm.Get()?.GoToNotificationCommand.Execute(model));
8082
}

0 commit comments

Comments
 (0)