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

Commit 88a49bd

Browse files
committed
Don't display duplicate gitignore and licences.
When akavache refreshes its cache, duplicate items are produced. This should really be fixed at a lower level, but for the moment just remove duplicates in the view model. Also fix sorting.
1 parent 7bfdb28 commit 88a49bd

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

src/GitHub.App/ViewModels/Dialog/RepositoryCreationViewModel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ public async Task InitializeAsync(IConnection connection)
197197
.Subscribe(x =>
198198
{
199199
var sorted = x
200-
.OrderByDescending(item => GitIgnoreItem.IsRecommended(item.Name))
200+
.Distinct()
201+
.OrderByDescending(item => item.Recommended)
201202
.ThenBy(item => item.Name);
202203
GitIgnoreTemplates = new[] { GitIgnoreItem.None }.Concat(sorted).ToList();
203204

@@ -212,8 +213,9 @@ public async Task InitializeAsync(IConnection connection)
212213
.Subscribe(x =>
213214
{
214215
var sorted = x
215-
.OrderByDescending(item => LicenseItem.IsRecommended(item.Name))
216-
.ThenBy(item => item.Name);
216+
.Distinct()
217+
.OrderByDescending(item => item.Recommended)
218+
.ThenBy(item => item.Key);
217219
Licenses = new[] { LicenseItem.None }.Concat(sorted).ToList();
218220
});
219221
}

src/GitHub.Exports.Reactive/Models/GitIgnoreItem.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using GitHub.Collections;
22
using System;
3+
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Globalization;
56
using System.Linq;
67

78
namespace GitHub.Models
89
{
910
[DebuggerDisplay("{DebuggerDisplay,nq}")]
10-
public sealed class GitIgnoreItem : ICopyable<GitIgnoreItem>
11+
public sealed class GitIgnoreItem : ICopyable<GitIgnoreItem>, IEquatable<GitIgnoreItem>
1112
{
1213
static readonly string[] recommendedIgnoreFiles = { "None", "VisualStudio", "Node", "Eclipse", "C++", "Windows" };
1314

@@ -40,6 +41,16 @@ public static bool IsRecommended(string name)
4041
return recommendedIgnoreFiles.Any(item => item.Equals(name, StringComparison.OrdinalIgnoreCase));
4142
}
4243

44+
bool IEquatable<GitIgnoreItem>.Equals(GitIgnoreItem other) => Name == other.Name;
45+
46+
public override bool Equals(object obj)
47+
{
48+
var item = obj as GitIgnoreItem;
49+
return item != null && Name == item.Name;
50+
}
51+
52+
public override int GetHashCode() => 539060726 + EqualityComparer<string>.Default.GetHashCode(Name);
53+
4354
internal string DebuggerDisplay
4455
{
4556
get

src/GitHub.Exports.Reactive/Models/LicenseItem.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
using Octokit;
33
using GitHub.Collections;
44
using System;
5+
using System.Collections.Generic;
56

67
namespace GitHub.Models
78
{
8-
public class LicenseItem : ICopyable<LicenseItem>
9+
public class LicenseItem : ICopyable<LicenseItem>, IEquatable<LicenseItem>
910
{
1011
static readonly LicenseItem none = new LicenseItem();
1112
public static LicenseItem None { get { return none; } }
@@ -36,6 +37,19 @@ public void CopyFrom(LicenseItem other)
3637
Recommended = other.Recommended;
3738
}
3839

40+
bool IEquatable<LicenseItem>.Equals(LicenseItem other) => Key == other.Key;
41+
42+
public override bool Equals(object obj)
43+
{
44+
var item = obj as LicenseItem;
45+
return item != null && Key == item.Key;
46+
}
47+
48+
public override int GetHashCode()
49+
{
50+
return 539060726 + EqualityComparer<string>.Default.GetHashCode(Key);
51+
}
52+
3953
public string Key { get; private set; }
4054

4155
public string Name { get; private set; }

test/GitHub.App.UnitTests/ViewModels/Dialog/RepositoryCreationViewModelTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,11 @@ public async Task IsPopulatedByTheModelServiceAsync()
419419
Assert.True(result[0].Recommended);
420420
Assert.That("apache-2.0", Is.EqualTo(result[1].Key));
421421
Assert.True(result[1].Recommended);
422-
Assert.That("artistic-2.0", Is.EqualTo(result[2].Key));
422+
Assert.That("mit", Is.EqualTo(result[2].Key));
423423
Assert.True(result[2].Recommended);
424424
Assert.That("agpl-3.0", Is.EqualTo(result[3].Key));
425425
Assert.False(result[3].Recommended);
426-
Assert.That("mit", Is.EqualTo(result[4].Key));
426+
Assert.That("artistic-2.0", Is.EqualTo(result[4].Key));
427427
Assert.False(result[4].Recommended);
428428
Assert.That(result[0], Is.EqualTo(vm.SelectedLicense));
429429
}

0 commit comments

Comments
 (0)