Populate license drop down from API

This commit is contained in:
Haacked 2015-03-17 23:23:00 -07:00
Родитель a1faf2cd5b
Коммит 5ef0e0f194
10 изменённых файлов: 130 добавлений и 4 удалений

Просмотреть файл

@ -219,6 +219,11 @@ namespace GitHub.Api
return gitHubClient.Miscellaneous.GetGitIgnoreTemplates();
}
public IObservable<LicenseMetadata> GetLicenses()
{
return gitHubClient.Miscellaneous.GetLicenses();
}
public ITwoFactorChallengeHandler TwoFactorChallengeHandler { get; private set; }
}
}

Просмотреть файл

@ -33,6 +33,14 @@ namespace GitHub.SampleData
new GitIgnoreItem("Wap"),
new GitIgnoreItem("WordPress")
};
Licenses = new ReactiveList<LicenseItem>
{
new LicenseItem(new LicenseMetadata("agpl-3.0", "GNU Affero GPL v3.0", new Uri("https://whatever"))),
new LicenseItem(new LicenseMetadata("apache-2.0", "Apache License 2.0", new Uri("https://whatever"))),
new LicenseItem(new LicenseMetadata("artistic-2.0", "Artistic License 2.0", new Uri("https://whatever"))),
new LicenseItem(new LicenseMetadata("mit", "MIT License", new Uri("https://whatever")))
};
}
public string Title { get { return "Create a GitHub Repository"; } } // TODO: this needs to be contextual
@ -161,6 +169,11 @@ namespace GitHub.SampleData
{
get; private set;
}
public ReactiveList<LicenseItem> Licenses
{
get; private set;
}
}
[ExcludeFromCodeCoverage]

Просмотреть файл

@ -76,6 +76,16 @@ namespace GitHub.ViewModels
.ToList()
.Subscribe(templates =>
GitIgnoreTemplates.AddRange(templates.OrderByDescending(template => template.Recommended)));
Licenses = new ReactiveList<LicenseItem>();
Observable.Return(LicenseItem.None).Concat(
hosts.GitHubHost.ApiClient
.GetLicenses()
.ObserveOn(RxApp.MainThreadScheduler)
.Select(license => new LicenseItem(license)))
.ToList()
.Subscribe(licenses =>
Licenses.AddRange(licenses.OrderByDescending(lic => lic.Recommended)));
}
public string Title { get { return "Create a GitHub Repository"; } } // TODO: this needs to be contextual
@ -92,6 +102,12 @@ namespace GitHub.ViewModels
private set;
}
public ReactiveList<LicenseItem> Licenses
{
get;
private set;
}
string baseRepositoryPath;
public string BaseRepositoryPath
{

Просмотреть файл

@ -30,5 +30,6 @@ namespace GitHub.Api
IObservable<IReadOnlyList<EmailAddress>> GetEmails();
ITwoFactorChallengeHandler TwoFactorChallengeHandler { get; }
IObservable<string> GetGitIgnoreTemplates();
IObservable<LicenseMetadata> GetLicenses();
}
}

Просмотреть файл

@ -80,6 +80,7 @@
<Compile Include="Helpers\ExceptionHelper.cs" />
<Compile Include="Helpers\Guard.cs" />
<Compile Include="Models\GitIgnoreItem.cs" />
<Compile Include="Models\LicenseItem.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\..\script\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>

Просмотреть файл

@ -0,0 +1,38 @@
using System.Globalization;
using Octokit;
namespace GitHub.Models
{
public class LicenseItem
{
public static LicenseItem None = new LicenseItem();
public LicenseItem(LicenseMetadata license)
{
Key = license.Key;
Name = license.Name;
Recommended = license.Key == "mit" || license.Key == "gpl-2.0" || license.Key == "apache-2.0";
}
LicenseItem()
{
Key = "";
Name = "None";
Recommended = true;
}
public string Key { get; private set; }
public string Name { get; private set; }
public bool Recommended { get; private set; }
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "License: {0} Recommended: {1}", Key, Recommended);
}
}
}
}

Просмотреть файл

@ -36,6 +36,7 @@ namespace GitHub.ViewModels
/// The list of GitIgnore templates supported by repository creation
/// </summary>
ReactiveList<GitIgnoreItem> GitIgnoreTemplates { get; }
ReactiveList<LicenseItem> Licenses { get; }
/// <summary>
/// Indicates whether the created repository should be private or not.

Просмотреть файл

@ -140,8 +140,23 @@
</uirx:FilteredComboBox>
<Label Grid.Column="0" Grid.Row="6" Target="{Binding ElementName=licenseList}">License</Label>
<uirx:FilteredComboBox x:Name="licenseList" Grid.Column="1" Grid.Row="6" Style="{StaticResource GitHubFilterComboBox}" />
<uirx:FilteredComboBox
x:Name="licenseList"
Grid.Column="1"
Grid.Row="6"
Style="{StaticResource GitHubFilterComboBox}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="itemName" Text="{Binding Name}" FontWeight="{Binding Recommended, Converter={ui:BooleanToFontWeightConverter}}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBoxItem}}" Value="{x:Null}">
<Setter TargetName="itemName" Property="FontWeight" Value="Normal" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</uirx:FilteredComboBox>
<!-- uirx:UserErrorMessages x:Name="userErrorMessages" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="7" / -->
<ui:HorizontalShadowDivider Width="120" Grid.Row="8" Grid.ColumnSpan="2" Margin="0,12,0,12"/>

Просмотреть файл

@ -36,6 +36,7 @@ namespace GitHub.VisualStudio.UI.Views.Controls
this.WhenActivated(d =>
{
d(this.OneWayBind(ViewModel, vm => vm.GitIgnoreTemplates, v => v.ignoreTemplateList.ItemsSource));
d(this.OneWayBind(ViewModel, vm => vm.Licenses, v => v.licenseList.ItemsSource));
//d(this.Bind(ViewModel, vm => vm.RepositoryName, v => v.nameText.Text));
//d(this.OneWayBind(ViewModel, vm => vm.RepositoryNameValidator, v => v.nameValidationMessage.ReactiveValidator));

Просмотреть файл

@ -1,8 +1,10 @@
using System.Reactive.Linq;
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GitHub.Models;
using GitHub.ViewModels;
using NSubstitute;
using Octokit;
using Rothko;
using Xunit;
@ -326,5 +328,38 @@ public class RepositoryCreationViewModelTests
Assert.Equal("WordPress", result[5].Name);
Assert.False(result[5].Recommended);
}
public class TheLicensesProperty
{
[Fact]
public void IsPopulatedByTheApiAndSortedWithRecommendedFirst()
{
var licenses = new[]
{
new LicenseMetadata("agpl-3.0", "GNU Affero GPL v3.0", new Uri("https://whatever")),
new LicenseMetadata("apache-2.0", "Apache License 2.0", new Uri("https://whatever")),
new LicenseMetadata("artistic-2.0", "Artistic License 2.0", new Uri("https://whatever")),
new LicenseMetadata("mit", "MIT License", new Uri("https://whatever"))
};
var hosts = Substitute.For<IRepositoryHosts>();
hosts.GitHubHost.ApiClient
.GetLicenses()
.Returns(licenses.ToObservable());
var vm = new RepositoryCreationViewModel(Substitute.For<IOperatingSystem>(), hosts);
var result = vm.Licenses;
Assert.Equal(5, result.Count);
Assert.Equal("", result[0].Key);
Assert.Equal("None", result[0].Name);
Assert.True(result[0].Recommended);
Assert.Equal("apache-2.0", result[1].Key);
Assert.True(result[1].Recommended);
Assert.Equal("mit", result[2].Key);
Assert.True(result[2].Recommended);
Assert.Equal("artistic-2.0", result[3].Key);
Assert.False(result[3].Recommended);
}
}
}
}
}