Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 29897f3

Browse files
committed
Let's have a proper notification service!
1 parent 972ebd5 commit 29897f3

18 files changed

+265
-103
lines changed

src/GitHub.App/ViewModels/RepositoryCloneViewModel.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class RepositoryCloneViewModel : BaseViewModel, IRepositoryCloneViewModel
2727
readonly IRepositoryHost repositoryHost;
2828
readonly IRepositoryCloneService cloneService;
2929
readonly IOperatingSystem operatingSystem;
30-
readonly IVSServices vsServices;
30+
readonly INotificationService notificationService;
3131
readonly IReactiveCommand<IReadOnlyList<IRepositoryModel>> loadRepositoriesCommand;
3232
readonly ReactiveCommand<object> browseForDirectoryCommand = ReactiveCommand.Create();
3333
readonly ObservableAsPropertyHelper<bool> isLoading;
@@ -41,20 +41,20 @@ public class RepositoryCloneViewModel : BaseViewModel, IRepositoryCloneViewModel
4141
IConnectionRepositoryHostMap connectionRepositoryHostMap,
4242
IRepositoryCloneService repositoryCloneService,
4343
IOperatingSystem operatingSystem,
44-
IVSServices vsServices)
45-
: this(connectionRepositoryHostMap.CurrentRepositoryHost, repositoryCloneService, operatingSystem, vsServices)
44+
INotificationService notificationService)
45+
: this(connectionRepositoryHostMap.CurrentRepositoryHost, repositoryCloneService, operatingSystem, notificationService)
4646
{ }
4747

4848
public RepositoryCloneViewModel(
4949
IRepositoryHost repositoryHost,
5050
IRepositoryCloneService cloneService,
5151
IOperatingSystem operatingSystem,
52-
IVSServices vsServices)
52+
INotificationService notificationService)
5353
{
5454
this.repositoryHost = repositoryHost;
5555
this.cloneService = cloneService;
5656
this.operatingSystem = operatingSystem;
57-
this.vsServices = vsServices;
57+
this.notificationService = notificationService;
5858

5959
Title = string.Format(CultureInfo.CurrentCulture, Resources.CloneTitle, repositoryHost.Title);
6060
Repositories = new ReactiveList<IRepositoryModel>();
@@ -129,7 +129,7 @@ IObservable<Unit> OnCloneRepository(object state)
129129
{
130130
var repository = SelectedRepository;
131131
Debug.Assert(repository != null, "Should not be able to attempt to clone a repo when it's null");
132-
vsServices.ShowError(e.GetUserFriendlyErrorMessage(ErrorType.ClonedFailed, repository.Name));
132+
notificationService.ShowError(e.GetUserFriendlyErrorMessage(ErrorType.ClonedFailed, repository.Name));
133133
return Observable.Return(Unit.Default);
134134
});
135135
}

src/GitHub.App/ViewModels/RepositoryPublishViewModel.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.ComponentModel.Composition;
55
using System.Globalization;
66
using System.Linq;
7-
using System.Reactive;
87
using System.Reactive.Linq;
98
using GitHub.Exports;
109
using GitHub.Extensions;
@@ -27,7 +26,7 @@ public class RepositoryPublishViewModel : RepositoryFormViewModel, IRepositoryPu
2726

2827
readonly IRepositoryHosts hosts;
2928
readonly IRepositoryPublishService repositoryPublishService;
30-
readonly IVSServices vsServices;
29+
readonly INotificationService notificationService;
3130
readonly ObservableAsPropertyHelper<IReadOnlyList<IAccount>> accounts;
3231
readonly ObservableAsPropertyHelper<bool> isHostComboBoxVisible;
3332
readonly ObservableAsPropertyHelper<bool> canKeepPrivate;
@@ -38,10 +37,10 @@ public class RepositoryPublishViewModel : RepositoryFormViewModel, IRepositoryPu
3837
public RepositoryPublishViewModel(
3938
IRepositoryHosts hosts,
4039
IRepositoryPublishService repositoryPublishService,
41-
IVSServices vsServices,
40+
INotificationService notificationService,
4241
IConnectionManager connectionManager)
4342
{
44-
this.vsServices = vsServices;
43+
this.notificationService = notificationService;
4544
this.hosts = hosts;
4645

4746
title = this.WhenAny(
@@ -159,7 +158,7 @@ IObservable<ProgressState> OnPublishRepository(object arg)
159158
return repositoryPublishService.PublishRepository(newRepository, account, SelectedHost.ApiClient)
160159
.Select(_ =>
161160
{
162-
vsServices.ShowMessage("Repository published successfully.");
161+
notificationService.ShowMessage("Repository published successfully.");
163162
return ProgressState.Success;
164163
})
165164
.Catch<ProgressState, Exception>(ex =>
@@ -168,7 +167,7 @@ IObservable<ProgressState> OnPublishRepository(object arg)
168167
{
169168
log.Error(ex);
170169
var error = new PublishRepositoryUserError(ex.Message);
171-
vsServices.ShowError((error.ErrorMessage + Environment.NewLine + error.ErrorCauseOrResolution).TrimEnd());
170+
notificationService.ShowError((error.ErrorMessage + Environment.NewLine + error.ErrorCauseOrResolution).TrimEnd());
172171
}
173172
return Observable.Return(ProgressState.Fail);
174173
});
@@ -199,11 +198,11 @@ void InitializeValidation()
199198
{
200199
if (!string.IsNullOrEmpty(message))
201200
{
202-
vsServices.ShowWarning(message);
201+
notificationService.ShowWarning(message);
203202
}
204203
else
205204
{
206-
vsServices.ClearNotifications();
205+
notificationService.ClearNotifications();
207206
}
208207
});
209208
}

src/GitHub.Exports.Reactive/GitHub.Exports.Reactive.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<ItemGroup>
4949
<Reference Include="PresentationCore" />
5050
<Reference Include="System" />
51+
<Reference Include="System.ComponentModel.Composition" />
5152
<Reference Include="System.Core" />
5253
<Reference Include="System.Net.Http" />
5354
<Reference Include="Microsoft.TeamFoundation.Controls, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
@@ -91,6 +92,7 @@
9192
<Compile Include="Models\IConnectionRepositoryHostMap.cs" />
9293
<Compile Include="Models\IRepositoryHosts.cs" />
9394
<Compile Include="Services\IModelService.cs" />
95+
<Compile Include="Services\NotificationDispatcher.cs" />
9496
<Compile Include="ViewModels\ILoginControlViewModel.cs" />
9597
<Compile Include="ViewModels\ILoginToGitHubForEnterpriseViewModel.cs" />
9698
<Compile Include="ViewModels\ILoginToGitHubViewModel.cs" />
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.ComponentModel.Composition;
3+
using System.Reactive.Linq;
4+
using System.Reactive.Subjects;
5+
using System.Windows.Input;
6+
7+
namespace GitHub.Services
8+
{
9+
[Export(typeof(INotificationDispatcher))]
10+
[Export(typeof(INotificationService))]
11+
[PartCreationPolicy(CreationPolicy.Shared)]
12+
public sealed class NotificationDispatcher : INotificationDispatcher, IDisposable
13+
{
14+
Subject<Notification> notifications;
15+
16+
public NotificationDispatcher()
17+
{
18+
notifications = new Subject<Notification>();
19+
}
20+
21+
public IObservable<Notification> Listen()
22+
{
23+
return notifications;
24+
}
25+
26+
public void ShowMessage(string message)
27+
{
28+
notifications.OnNext(new Notification(message, Notification.NotificationType.Message));
29+
}
30+
31+
public void ShowMessage(string message, ICommand command)
32+
{
33+
notifications.OnNext(new Notification(message, Notification.NotificationType.Message, command));
34+
}
35+
36+
public void ShowWarning(string message)
37+
{
38+
notifications.OnNext(new Notification(message, Notification.NotificationType.Warning));
39+
}
40+
41+
public void ShowError(string message)
42+
{
43+
notifications.OnNext(new Notification(message, Notification.NotificationType.Error));
44+
}
45+
46+
public void ClearNotifications()
47+
{
48+
}
49+
50+
bool disposed; // To detect redundant calls
51+
void Dispose(bool disposing)
52+
{
53+
if (disposing)
54+
{
55+
if (disposed) return;
56+
disposed = true;
57+
notifications.Dispose();
58+
}
59+
}
60+
61+
public void Dispose()
62+
{
63+
Dispose(true);
64+
GC.SuppressFinalize(this);
65+
}
66+
}
67+
}

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<Compile Include="Collections\ICopyable.cs" />
100100
<Compile Include="ExceptionExtensions.cs" />
101101
<Compile Include="Extensions\VSExtensions.cs" />
102+
<Compile Include="GlobalSuppressions.cs" />
102103
<Compile Include="Helpers\INotifyPropertySource.cs" />
103104
<Compile Include="Extensions\PropertyNotifierExtensions.cs" />
104105
<Compile Include="Extensions\SimpleRepositoryModelExtensions.cs" />
@@ -114,9 +115,13 @@
114115
<Compile Include="Services\IGitService.cs" />
115116
<Compile Include="Services\IMenuHandler.cs" />
116117
<Compile Include="Services\IMenuProvider.cs" />
118+
<Compile Include="Services\INotificationDispatcher.cs" />
119+
<Compile Include="Services\ITeamExplorerServices.cs" />
117120
<Compile Include="Services\ITeamExplorerServiceHolder.cs" />
121+
<Compile Include="Services\INotificationService.cs" />
118122
<Compile Include="Services\Logger.cs" />
119123
<Compile Include="Services\Services.cs" />
124+
<Compile Include="Services\TeamExplorerServices.cs" />
120125
<Compile Include="Services\VSServices.cs" />
121126
<Compile Include="Models\IConnection.cs" />
122127
<Compile Include="Properties\AssemblyInfo.cs" />
1.68 KB
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Windows.Input;
3+
4+
namespace GitHub.Services
5+
{
6+
public struct Notification
7+
{
8+
public enum NotificationType
9+
{
10+
Message,
11+
MessageCommand,
12+
Warning,
13+
Error
14+
}
15+
public string Message { get; }
16+
public NotificationType Type { get; }
17+
public ICommand Command { get; }
18+
19+
public Notification(string message, NotificationType type, ICommand command = null)
20+
{
21+
Message = message;
22+
Type = type;
23+
Command = command;
24+
}
25+
}
26+
27+
public interface INotificationDispatcher : INotificationService
28+
{
29+
IObservable<Notification> Listen();
30+
}
31+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Windows.Input;
2+
3+
namespace GitHub.Services
4+
{
5+
public interface INotificationService
6+
{
7+
void ShowMessage(string message);
8+
void ShowMessage(string message, ICommand command);
9+
void ShowWarning(string message);
10+
void ShowError(string message);
11+
void ClearNotifications();
12+
}
13+
}

src/GitHub.Exports/Services/ITeamExplorerServiceHolder.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using GitHub.Primitives;
3-
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
43
using GitHub.Models;
54

65
namespace GitHub.Services
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Windows.Input;
2+
3+
namespace GitHub.Services
4+
{
5+
public interface ITeamExplorerServices : INotificationService
6+
{
7+
}
8+
}

0 commit comments

Comments
 (0)