Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike of Open from GitHub functionality #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions src/GitHub.VisualStudio.Contrib/GitHubCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ internal override void MenuItemCallback(object sender, EventArgs e)
async Task ShowOpenFromGitHubAsync()
{
var pane = await toolWindowManager.Value.ShowGitHubPane();
await pane.ShowPullRequests();

if (pane.Content is INavigationViewModel navigationViewModel)
{
var viewModel = factory.CreateViewModel<IHelloWorldViewModel>();
Expand Down
124 changes: 117 additions & 7 deletions src/GitHub.VisualStudio.Contrib/UI/ViewModels/HelloWorldViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,147 @@
using System;
using System.IO;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
using ReactiveUI;
using GitHub.Services;
using GitHub.Primitives;
using GitHub.ViewModels.GitHubPane;
using GitHub.VisualStudio.Contrib.Console;

namespace GitHub.VisualStudio.Contrib.UI.ViewModels
{
[Export(typeof(IHelloWorldViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class HelloWorldViewModel : PanePageViewModelBase, IHelloWorldViewModel
{
readonly IGitHubContextService contextService;
readonly ITeamExplorerContext teamExplorerContext;
readonly IRepositoryCloneService repositoryCloneService;

GitHubContext context;
string targetUrl;
string blobName;
string defaultPath;
Uri repositoryUrl;
Uri webUrl;

[ImportingConstructor]
public HelloWorldViewModel(IConsoleContext console)
public HelloWorldViewModel(
IGitHubContextService contextService,
ITeamExplorerContext teamExplorerContext,
IRepositoryCloneService repositoryCloneService,
IGitHubServiceProvider serviceProvider)
{
SayHello = ReactiveCommand.Create();
SayHello.Subscribe(_ => console.WriteLine("Hello, World!"));
this.contextService = contextService;
this.teamExplorerContext = teamExplorerContext;
this.repositoryCloneService = repositoryCloneService;

Title = "GitHub URL";

// Is the target URL pointing at the active repository
var isActiveRepositoryObservable =
this.WhenAnyValue(x => x.RepositoryUrl).Select(r => r is Uri targetUrl &&
teamExplorerContext.ActiveRepository?.CloneUrl is UriString activeUrl &&
UriString.RepositoryUrlsAreEqual(activeUrl, targetUrl.ToString()));

GoTo = ReactiveCommand.Create(
this.WhenAnyValue(x => x.BlobName).Select(b => b != null)
.CombineLatest(isActiveRepositoryObservable, (a, b) => a && b));
GoTo.Subscribe(_ =>
{
var localPath = teamExplorerContext.ActiveRepository?.LocalPath;
contextService.TryOpenFile(localPath, Context);
});

Clone = ReactiveCommand.CreateAsyncTask<object>(
(this).WhenAnyValue(x => x.DefaultPath).Select(d => d is string dir && !Directory.Exists(d))
.CombineLatest(isActiveRepositoryObservable, (a, b) => a && !b), DoCloneAsync);

Open = ReactiveCommand.Create(
this.WhenAnyValue(x => x.DefaultPath).Select(d => d is string dir && Directory.Exists(d))
.CombineLatest(isActiveRepositoryObservable, (a, b) => a && !b));
Open.Subscribe(_ =>
{
var dte = serviceProvider.GetService<EnvDTE.DTE>();
dte.ExecuteCommand("File.OpenFolder", DefaultPath);
dte.ExecuteCommand("View.TfsTeamExplorer");
contextService.TryOpenFile(DefaultPath, Context);
});

Context = contextService.FindContextFromClipboard();
TargetUrl = Context?.Url;

this.WhenAnyValue(x => x.TargetUrl).Subscribe(u =>
{
Context = contextService.FindContextFromUrl(u);
});

WebUrl = new Uri("https://github.com/editor-tools/GitHub.VisualStudio.Contrib");
this.WhenAnyValue(x => x.Context).Subscribe(c =>
{
BlobName = c?.BlobName;
RepositoryUrl = c?.Url?.ToRepositoryUrl();

DefaultPath =
repositoryCloneService.DefaultClonePath is string home &&
context?.Owner is string owner &&
context?.RepositoryName is string repositoryName ?
Path.Combine(home, owner, repositoryName) : null;
});

Done = ReactiveCommand.Create();
}

public IReactiveCommand<object> SayHello { get; }
async Task<object> DoCloneAsync(object args)
{
var repositoryName = Path.GetFileName(DefaultPath);
var repositoryPath = Path.GetDirectoryName(DefaultPath);
await repositoryCloneService.CloneRepository(RepositoryUrl.ToString(), repositoryName, repositoryPath);
Open.Execute(null);
return null;
}

public IObservable<object> Done { get; }
public string TargetUrl
{
get { return targetUrl; }
set { this.RaiseAndSetIfChanged(ref targetUrl, value); }
}

public GitHubContext Context
{
get { return context; }
private set { this.RaiseAndSetIfChanged(ref context, value); }
}

public string BlobName
{
get { return blobName; }
private set { this.RaiseAndSetIfChanged(ref blobName, value); }
}

public string DefaultPath
{
get { return defaultPath; }
private set { this.RaiseAndSetIfChanged(ref defaultPath, value); }
}

public Uri WebUrl
{
get { return webUrl; }
private set { this.RaiseAndSetIfChanged(ref webUrl, value); }
}

public Uri RepositoryUrl
{
get { return repositoryUrl; }
private set { this.RaiseAndSetIfChanged(ref repositoryUrl, value); }
}

public IReactiveCommand<object> GoTo { get; }

public IReactiveCommand<object> Clone { get; }

public IReactiveCommand<object> Open { get; }

public IObservable<object> Done { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GitHub.ViewModels;
using GitHub.Services;
using GitHub.ViewModels;
using GitHub.ViewModels.Dialog;
using GitHub.ViewModels.GitHubPane;
using ReactiveUI;
Expand All @@ -7,6 +8,18 @@ namespace GitHub.VisualStudio.Contrib.UI.ViewModels
{
public interface IHelloWorldViewModel : IViewModel, IDialogContentViewModel, IPanePageViewModel, IOpenInBrowser
{
IReactiveCommand<object> SayHello { get; }
IReactiveCommand<object> GoTo { get; }

IReactiveCommand<object> Clone { get; }

IReactiveCommand<object> Open { get; }

string TargetUrl { get; set; }

string BlobName { get; }

string DefaultPath { get; }

GitHubContext Context { get; }
}
}
68 changes: 43 additions & 25 deletions src/GitHub.VisualStudio.Contrib/UI/Views/HelloWorldView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,56 @@
xmlns:ui="clr-namespace:GitHub.UI;assembly=GitHub.UI"
DataContext="{Binding ViewModel}"
mc:Ignorable="d">

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<cache:SharedDictionaryManager Source="pack://application:,,,/GitHub.VisualStudio.UI;component/SharedDictionary.xaml" />
<cache:SharedDictionaryManager Source="pack://application:,,,/GitHub.UI;component/SharedDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

<DockPanel>
<StackPanel Margin="10" Orientation="Vertical">
<ui:OcticonImage Icon="mark_github"
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<cache:SharedDictionaryManager Source="pack://application:,,,/GitHub.VisualStudio.UI;component/SharedDictionary.xaml" />
<cache:SharedDictionaryManager Source="pack://application:,,,/GitHub.UI;component/SharedDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

<DockPanel>
<StackPanel Margin="10" Orientation="Vertical">
<ui:OcticonImage Icon="mark_github"
Foreground="{DynamicResource GitHubVsWindowText}"
Margin="0,5"
Width="48"
Height="48" />
<Label
<Label
Foreground="{DynamicResource GitHubVsWindowText}"
HorizontalAlignment="Center"
FontSize="16"
Content="Look an Octocat!" />
<StackPanel
Margin="0,5"
HorizontalAlignment="Center"
Orientation="Horizontal">
<TextBlock
Margin="10,0"
HorizontalAlignment="Center">
<Hyperlink Command="{Binding SayHello}"><TextBlock Text="Say, Hello" /></Hyperlink>
</TextBlock>
Content="Open from GitHub" />
<TextBox Text="{Binding TargetUrl, UpdateSourceTrigger=PropertyChanged}" />
<StackPanel
x:Name="GoToPanel"
Margin="0,5"
HorizontalAlignment="Center"
Orientation="Horizontal">
<TextBlock Margin="10,0" HorizontalAlignment="Center">
<Hyperlink Command="{Binding GoTo}">Go To</Hyperlink> <TextBlock Text="{Binding BlobName}" />
</TextBlock>
</StackPanel>
<StackPanel
x:Name="ClonePanel"
Margin="0,5"
HorizontalAlignment="Center"
Orientation="Horizontal">
<TextBlock Margin="10,0" HorizontalAlignment="Center">
<Hyperlink Command="{Binding Clone}">Clone</Hyperlink> <TextBlock Text="{Binding DefaultPath}" />
</TextBlock>
</StackPanel>
<StackPanel
x:Name="OpenPanel"
Margin="0,5"
HorizontalAlignment="Center"
Orientation="Horizontal">
<TextBlock Margin="10,0" HorizontalAlignment="Center">
<Hyperlink Command="{Binding Open}">Open</Hyperlink> <TextBlock Text="{Binding DefaultPath}" />
</TextBlock>
</StackPanel>
</StackPanel>
</StackPanel>
</DockPanel>
</DockPanel>
</local:GenericHelloWorldView>
10 changes: 9 additions & 1 deletion src/GitHub.VisualStudio.Contrib/UI/Views/HelloWorldView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ public partial class HelloWorldView : GenericHelloWorldView
{
public HelloWorldView()
{
this.InitializeComponent();
InitializeComponent();

this.WhenActivated(d =>
{
this.WhenAnyObservable(x => x.ViewModel.GoTo.CanExecuteObservable)
.BindTo(this, x => x.GoToPanel.Visibility);

this.WhenAnyObservable(x => x.ViewModel.Clone.CanExecuteObservable)
.BindTo(this, x => x.ClonePanel.Visibility);

this.WhenAnyObservable(x => x.ViewModel.Open.CanExecuteObservable)
.BindTo(this, x => x.OpenPanel.Visibility);
});
}
}
Expand Down