8000 Removed `IConnection.Repositories`. by grokys · Pull Request #1279 · github/VisualStudio · GitHub
[go: up one dir, main page]

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

Removed IConnection.Repositories. #1279

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/GitHub.App/GitHub.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<Compile Include="Models\IssueCommentModel.cs" />
<Compile Include="Models\PullRequestReviewCommentModel.cs" />
<Compile Include="Models\PullRequestDetailArgument.cs" />
<Compile Include="Services\LocalRepositories.cs" />
<Compile Include="ViewModels\ViewModelBase.cs" />
<Compile Include="Caches\CacheIndex.cs" />
<Compile Include="Caches\CacheItem.cs" />
Expand Down
47 changes: 47 additions & 0 deletions src/GitHub.App/Services/LocalRepositories.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Helpers;
using GitHub.Models;
using GitHub.Services;

namespace GitHub.App.Services
{
/// <summary>
/// Stores a collection of known local repositories.
/// </summary>
/// <remarks>
/// The list of repositories exposed here is the list of local repositories known to Team
/// Explorer.
/// </remarks>
[Export(typeof(ILocalRepositories))]
public class LocalRepositories : ILocalRepositories
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't this this implementation should be here, this will bring all the Rx dependencies in when there's no need for it. The reason this was in ConnectionManager to begin with was meant to be light weight and require minimal dependencies. Perhaps GitHub.Exports or some other place would be better? Maybe even GitHub.VisualStudio?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the fact that its export is in GitHub.Exports good enough? If you look at some of our models, e.g. PullRequestModel - that is defined in GitHub.App with its interface in GitHub.Exports meaning that it's available to the non-rx parts of the extension. Rx shouldn't be loaded until a type that references rx is loaded, right?

{
readonly IVSGitServices vsGitServices;

[ImportingConstructor]
public LocalRepositories(IVSGitServices vsGitServices)
{
this.vsGitServices = vsGitServices;
}

/// <inheritdoc/>
public async Task Refresh()
{
await ThreadingHelper.SwitchToPoolThreadAsync();
var list = vsGitServices.GetKnownRepositories();
await ThreadingHelper.SwitchToMainThreadAsync();

repositories.Except(list).ToList().ForEach(x => repositories.Remove(x));
list.Except(repositories).ToList().ForEach(x => repositories.Add(x));
}

readonly ObservableCollectionEx<ILocalRepositoryModel> repositories
= new ObservableCollectionEx<ILocalRepositoryModel>();

/// <inheritdoc/>
public IReadOnlyObservableCollection<ILocalRepositoryModel> Repositories => repositories;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static void UpdateStickieItems<T>(
/// for T
/// </summary>
/// <typeparam name="T"></typeparam>
public class TrackingCollection<T> : ObservableCollection<T>, ITrackingCollection<T>, IDisposable
public class TrackingCollection<T> : ObservableCollection<T>, ITrackingCollection<T>, IReadOnlyObservableCollection<T>, IDisposable
where T : class, ICopyable<T>
{
enum TheAction
Expand Down
1 change: 1 addition & 0 deletions src/GitHub.Exports.Reactive/GitHub.Exports.Reactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<Compile Include="Services\IPullRequestSession.cs" />
<Compile Include="Services\IPullRequestService.cs" />
<Compile Include="Services\IPullRequestSessionManager.cs" />
<Compile Include="Services\LocalRepositoriesExtensions.cs" />
<Compile Include="Services\ModelServiceExtensions.cs" />
<Compile Include="ViewModels\IGistCreationViewModel.cs" />
<Compile Include="Services\NotificationDispatcher.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using GitHub.Models;
using GitHub.Primitives;
using ReactiveUI;

namespace GitHub.Services
{
/// <summary>
/// Implements extension methods for <see cref="ILocalRepositories"/>.
/// </summary>
public static class LocalRepositoriesExtensions
{
/// <summary>
/// Gets a derived collection that contains all known repositories with the specified
/// clone URL, ordered by name.
/// </summary>
/// <param name="repos">The local repositories object.</param>
/// <param name="address">The address.</param>
public static IReactiveDerivedList<ILocalRepositoryModel> GetRepositoriesForAddress(
this ILocalRepositories repos,
HostAddress address)
{
return repos.Repositories.CreateDerivedCollection(
x => x,
x => x.CloneUrl != null && address.Equals(HostAddress.Create(x.CloneUrl)),
OrderedComparer<ILocalRepositoryModel>.OrderBy(x => x.Name).Compare);
}
}
}
1 change: 1 addition & 2364 0 deletions src/GitHub.Exports/GitHub.Exports.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<Compile Include="Models\ICommentModel.cs" />
<Compile Include="Models\IInlineCommentModel.cs" />
<Compile Include="Models\IPullRequestReviewCommentModel.cs" />
<Compile Include="Services\ILocalRepositories.cs" />
<Compile Include="Settings\PkgCmdID.cs" />
<Compile Include="ViewModels\IHasErrorState.cs" />
<Compile Include="ViewModels\IHasLoading.cs" />
Expand Down
3 changes: 1 addition & 2 deletions src/GitHub.Exports/Models/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

namespace GitHub.Models
{
public interface IConnection : IDisposable
public interface IConnection
{
HostAddress HostAddress { get; }
string Username { get; }
IObservable<IConnection> Login();
void Logout();
ObservableCollection<ILocalRepositoryModel> Repositories { get; }
}
}
25 changes: 0 additions & 25 deletions src/GitHub.Exports/Services/Connection.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using GitHub.Models;
using GitHub.Primitives;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace GitHub.Services
{
Expand All @@ -16,12 +13,10 @@ public Connection(IConnectionManager cm, HostAddress hostAddress, string userNam
manager = cm;
HostAddress = hostAddress;
Username = userName;
Repositories = new ObservableCollection<ILocalRepositoryModel>();
}

public HostAddress HostAddress { get; private set; }
public string Username { get; private set; }
public ObservableCollection<ILocalRepositoryModel> Repositories { get; }

public IObservable<IConnection> Login()
{
Expand All @@ -32,25 +27,5 @@ public void Logout()
{
manager.RequestLogout(this);
}

#region IDisposable Support
private bool disposed = false; // To detect redundant calls

protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
Repositories.Clear();
disposed = true;
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
1 change: 0 additions & 1 deletion src/GitHub.Exports/Services/IConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ public interface IConnectionManager
// for telling IRepositoryHosts that we need to login from cache
[SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly")]
event Func<IConnection, IObservable<IConnection>> DoLogin;
Task RefreshRepositories();
}
}
23 changes: 23 additions & 0 deletions src/GitHub.Exports/Services/ILocalRepositories.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Models;

namespace GitHub.Services
{
/// <summary>
/// Stores a collection of known local repositories.
/// </summary>
public interface ILocalRepositories
{
/// <summary>
/// Gets the currently known local repositories.
/// </summary>
IReadOnlyObservableCollection<ILocalRepositoryModel> Repositories { get; }

/// <summary>
/// Updates <see cref="Repositories"/>.
/// </summary>
Task Refresh();
}
}
2 changes: 2 additions & 0 deletions src/GitHub.Extensions/GitHub.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IReadOnlyObservableCollection.cs" />
<Compile Include="LambdaComparer.cs" />
<Compile Include="ObservableCollectionEx.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\common\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
Expand Down
17 changes: 17 additions & 0 deletions src/GitHub.Extensions/IReadOnlyObservableCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace GitHub.Extensions
{
/// <summary>
/// Represents a read-only interface to an <see cref="ObservableCollection{T}"/>
/// </summary>
/// <typeparam name="T">The type of elements in the collection.</typeparam>
public interface IReadOnlyObservableCollection<T> : IReadOnlyList<T>,
INotifyCollectionChanged,
INotifyPropertyChanged
{
}
}
42 changes: 42 additions & 0 deletions src/GitHub.Extensions/ObservableCollectionEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace GitHub.Extensions
{
/// <summary>
/// A non-braindead way to expose a read-only view on an <see cref="ObservableCollection{T}"/>.
/// </summary>
/// <typeparam name="T">The type of elements in the collection.</typeparam>
/// <remarks>
/// <see cref="ReadOnlyObservableCollection{T}"/> fails in its only purpose by Not. Freaking.
/// Exposing. INotifyCollectionChanged. We define our own <see cref="IReadOnlyObservableCollection{T}"/>
/// type and use this class to expose it. Seriously.
/// </remarks>
public class ObservableCollectionEx<T> : ObservableCollection<T>, IReadOnlyObservableCollection<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="ObservableCollectionEx{T}"/> class.
/// </summary>
public ObservableCollectionEx()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ObservableCollectionEx{T}"/> class that
/// contains elements copied from the specified list.
/// </summary>
public ObservableCollectionEx(List<T> list)
: base(list)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ObservableCollectionEx{T}"/> class that
/// contains elements copied from the specified collection.
/// </summary>
public ObservableCollectionEx(IEnumerable<T> collection)
: base(collection)
{
}
}
}
10 changes: 7 additions & 3 deletions src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class GitHubConnectSection : TeamExplorerSectionBase, IGitHubConnectSecti
readonly int sectionIndex;
readonly IDialogService dialogService;
readonly IRepositoryCloneService cloneService;
readonly ILocalRepositories localRepositories;

bool isCloning;
bool isCreating;
Expand Down Expand Up @@ -98,6 +99,7 @@ public GitHubConnectSection(IGitHubServiceProvider serviceProvider,
IVSServices vsServices,
IRepositoryCloneService cloneService,
IDialogService dialogService,
ILocalRepositories localRepositories,
int index)
: base(serviceProvider, apiFactory, holder, manager)
{
Expand All @@ -108,6 +110,7 @@ public GitHubConnectSection(IGitHubServiceProvider serviceProvider,
Guard.ArgumentNotNull(vsServices, nameof(vsServices));
Guard.ArgumentNotNull(cloneService, nameof(cloneService));
Guard.ArgumentNotNull(dialogService, nameof(dialogService));
Guard.ArgumentNotNull(localRepositories, nameof(localRepositories));

Title = "GitHub";
IsEnabled = true;
Expand All @@ -119,6 +122,7 @@ public GitHubConnectSection(IGitHubServiceProvider serviceProvider,
this.vsServices = vsServices;
this.cloneService = cloneService;
this.dialogService = dialogService;
this.localRepositories = localRepositories;

Clone = CreateAsyncCommandHack(DoClone);

Expand Down Expand Up @@ -194,8 +198,8 @@ protected void Refresh(IConnection connection)
if (connection != SectionConnection)
{
SectionConnection = connection;
Repositories = SectionConnection.Repositories.CreateDerivedCollection(x => x,
orderer: OrderedComparer<ILocalRepositoryModel>.OrderBy(x => x.Name).Compare);
Repositories?.Dispose();
Repositories = localRepositories.GetRepositoriesForAddress(connection.HostAddress);
Repositories.CollectionChanged += UpdateRepositoryList;
Title = connection.HostAddress.Title;
IsVisible = true;
Expand Down Expand Up @@ -368,7 +372,7 @@ async Task RefreshRepositories()
{
// TODO: This is wasteful as we can be calling it multiple times for a single changed
// signal, once from each section. Needs refactoring.
await connectionManager.RefreshRepositories();
await localRepositories.Refresh();
RaisePropertyChanged("Repositories"); // trigger a re-check of the visibility of the listview based on item count
}

Expand Down
5 changes: 3 additions & 2 deletions src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public GitHubConnectSection0(IGitHubServiceProvider serviceProvider,
IPackageSettings settings,
IVSServices vsServices,
IRepositoryCloneService cloneService,
IDialogService dialogService)
: base(serviceProvider, apiFactory, holder, manager, settings, vsServices, cloneService, dialogService, 0)
IDialogService dialogService,
ILocalRepositories localRepositories)
: base(serviceProvider, apiFactory, holder, manager, settings, vsServices, cloneService, dialogService, localRepositories, 0)
{
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public GitHubConnectSection1(IGitHubServiceProvider serviceProvider,
IPackageSettings settings,
IVSServices vsServices,
IRepositoryCloneService cloneService,
IDialogService dialogService)
: base(serviceProvider, apiFactory, holder, manager, settings, vsServices, cloneService, dialogService, 1)
IDialogService dialogService,
ILocalRepositories localRepositories)
: base(serviceProvider, apiFactory, holder, manager, settings, vsServices, cloneService, dialogService, localRepositories, 1)
{
}
}
Expand Down
Loading
0