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

Commit e469169

Browse files
authored
Merge pull request #384 from github/shana/make-modelservice-return-observables
The indexed cache
2 parents ecae00f + c9921bd commit e469169

26 files changed

+638
-452
lines changed

src/GitHub.App/Api/ApiClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,9 @@ public IObservable<PullRequest> GetPullRequestsForRepository(string owner, strin
237237
SortDirection = SortDirection.Descending
238238
});
239239
}
240+
public IObservable<Repository> GetRepositories()
241+
{
242+
return gitHubClient.Repository.GetAllForCurrent();
243+
}
240244
}
241245
}

src/GitHub.App/Caches/CacheIndex.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ namespace GitHub.Caches
1010
{
1111
public class CacheIndex
1212
{
13+
public const string PRPrefix = "index:pr";
14+
public const string RepoPrefix = "index:repos";
15+
public const string GitIgnoresPrefix = "index:ignores";
16+
public const string LicensesPrefix = "index:licenses";
17+
1318
public static CacheIndex Create(string key)
1419
{
1520
return new CacheIndex { IndexKey = key };

src/GitHub.App/Models/PullRequestModel.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
namespace GitHub.Models
1010
{
1111
[DebuggerDisplay("{DebuggerDisplay,nq}")]
12-
public sealed class PullRequestModel : NotificationAwareObject, IPullRequestModel
12+
public sealed class PullRequestModel : NotificationAwareObject, IPullRequestModel,
13+
IEquatable<PullRequestModel>,
14+
IComparable<PullRequestModel>
1315
{
1416
public PullRequestModel(int number, string title,
1517
IAccount author, [AllowNull]IAccount assignee,
@@ -45,7 +47,7 @@ public override bool Equals([AllowNull]object obj)
4547

4648
public override int GetHashCode()
4749
{
48-
return Number;
50+
return Number.GetHashCode();
4951
}
5052

5153
bool IEquatable<IPullRequestModel>.Equals([AllowNull]IPullRequestModel other)
@@ -55,11 +57,23 @@ bool IEquatable<IPullRequestModel>.Equals([AllowNull]IPullRequestModel other)
5557
return other != null && Number == other.Number;
5658
}
5759

60+
bool IEquatable<PullRequestModel>.Equals([AllowNull]PullRequestModel other)
61+
{
62+
if (ReferenceEquals(this, other))
63+
return true;
64+
return other != null && Number == other.Number;
65+
}
66+
5867
public int CompareTo([AllowNull]IPullRequestModel other)
5968
{
6069
return other != null ? UpdatedAt.CompareTo(other.UpdatedAt) : 1;
6170
}
6271

72+
public int CompareTo([AllowNull]PullRequestModel other)
73+
{
74+
return other != null ? UpdatedAt.CompareTo(other.UpdatedAt) : 1;
75+
}
76+
6377
public static bool operator >([AllowNull]PullRequestModel lhs, [AllowNull]PullRequestModel rhs)
6478
{
6579
if (ReferenceEquals(lhs, rhs))
@@ -76,7 +90,7 @@ public int CompareTo([AllowNull]IPullRequestModel other)
7690

7791
public static bool operator ==([AllowNull]PullRequestModel lhs, [AllowNull]PullRequestModel rhs)
7892
{
79-
return Equals(lhs, rhs) && ((object)lhs == null || lhs.CompareTo(rhs) == 0);
93+
return ReferenceEquals(lhs, rhs);
8094
}
8195

8296
public static bool operator !=([AllowNull]PullRequestModel lhs, [AllowNull]PullRequestModel rhs)

src/GitHub.App/Models/RepositoryModel.cs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,96 @@
55

66
namespace GitHub.Models
77
{
8-
public class RepositoryModel : SimpleRepositoryModel, IRepositoryModel, IEquatable<RepositoryModel>
8+
public class RepositoryModel : SimpleRepositoryModel, IRepositoryModel,
9+
IEquatable<RepositoryModel>, IComparable<RepositoryModel>
910
{
10-
public RepositoryModel(string name, UriString cloneUrl, bool isPrivate, bool isFork, IAccount ownerAccount)
11+
public RepositoryModel(long id, string name, UriString cloneUrl, bool isPrivate, bool isFork, IAccount ownerAccount)
1112
: base(name, cloneUrl)
1213
{
14+
Id = id;
1315
Owner = ownerAccount;
1416
SetIcon(isPrivate, isFork);
1517
}
1618

17-
public IAccount Owner { get; private set; }
19+
public void CopyFrom(IRepositoryModel other)
20+
{
21+
if (!Equals(other))
22+
throw new ArgumentException("Instance to copy from doesn't match this instance. this:(" + this + ") other:(" + other + ")", nameof(other));
23+
Icon = other.Icon;
24+
}
25+
1826
public override int GetHashCode()
1927
{
20-
return (Owner?.GetHashCode() ?? 0) ^ base.GetHashCode();
28+
return Id.GetHashCode();
2129
}
2230

2331
public override bool Equals([AllowNull]object obj)
2432
{
2533
if (ReferenceEquals(this, obj))
2634
return true;
2735
var other = obj as RepositoryModel;
28-
return other != null && Equals(Owner, other.Owner) && base.Equals(obj);
36+
return other != null && Id == other.Id;
37+
}
38+
39+
bool IEquatable<IRepositoryModel>.Equals([AllowNull]IRepositoryModel other)
40+
{
41+
if (ReferenceEquals(this, other))
42+
return true;
43+
return other != null && Id == other.Id;
2944
}
3045

3146
bool IEquatable<RepositoryModel>.Equals([AllowNull]RepositoryModel other)
3247
{
3348
if (ReferenceEquals(this, other))
3449
return true;
35-
return other != null && Equals(Owner, other.Owner) && base.Equals(other as SimpleRepositoryModel);
50+
return other != null && Id == other.Id;
51+
}
52+
53+
public int CompareTo([AllowNull]IRepositoryModel other)
54+
{
55+
return other != null ? UpdatedAt.CompareTo(other.UpdatedAt) : 1;
56+
}
57+
58+
public int CompareTo([AllowNull]RepositoryModel other)
59+
{
60+
return other != null ? UpdatedAt.CompareTo(other.UpdatedAt) : 1;
3661
}
3762

63+
public static bool operator >([AllowNull]RepositoryModel lhs, [AllowNull]RepositoryModel rhs)
64+
{
65+
if (ReferenceEquals(lhs, rhs))
66+
return false;
67+
return lhs?.CompareTo(rhs) > 0;
68+
}
69+
70+
public static bool operator <([AllowNull]RepositoryModel lhs, [AllowNull]RepositoryModel rhs)
71+
{
72+
if (ReferenceEquals(lhs, rhs))
73+
return false;
74+
return (object)lhs == null || lhs.CompareTo(rhs) < 0;
75+
}
76+
77+
public static bool operator ==([AllowNull]RepositoryModel lhs, [AllowNull]RepositoryModel rhs)
78+
{
79+
return ReferenceEquals(lhs, rhs);
80+
}
81+
82+
public static bool operator !=([AllowNull]RepositoryModel lhs, [AllowNull]RepositoryModel rhs)
83+
{
84+
return !(lhs == rhs);
85+
}
86+
87+
public IAccount Owner { get; }
88+
public long Id { get; }
89+
public DateTimeOffset CreatedAt { get; set; }
90+
public DateTimeOffset UpdatedAt { get; set; }
91+
3892
internal string DebuggerDisplay
3993
{
4094
get
4195
{
4296
return String.Format(CultureInfo.InvariantCulture,
43-
"{4}\tName: {0} CloneUrl: {1} LocalPath: {2} Account: {3}", Name, CloneUrl, LocalPath, Owner, GetHashCode());
97+
"{5}\tId: {0} Name: {1} CloneUrl: {2} LocalPath: {3} Account: {4}", Id, Name, CloneUrl, LocalPath, Owner, GetHashCode());
4498
}
4599
}
46100
}

src/GitHub.App/SampleData/SampleViewModels.cs

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -318,65 +318,34 @@ public IObservable<Unit> LogOut()
318318
}
319319

320320
[ExcludeFromCodeCoverage]
321-
public class RepositoryModelDesigner : NotificationAwareObject, IRepositoryModel
321+
public static class RepositoryModelDesigner
322322
{
323-
public RepositoryModelDesigner() : this("repo")
323+
public static IRepositoryModel Create(string name = null, string owner = null)
324324
{
325+
name = name ?? "octocat";
326+
owner = owner ?? "github";
327+
return new RepositoryModel(0, name, new UriString("http://github.com/" + name + "/" + owner), false, false, new AccountDesigner() { Login = owner });
325328
}
326-
327-
public RepositoryModelDesigner(string name) : this("repo", "github")
328-
{
329-
Name = name;
330-
}
331-
332-
public RepositoryModelDesigner(string name, string owner)
333-
{
334-
Name = name;
335-
Owner = new AccountDesigner { Login = owner };
336-
}
337-
338-
public void SetIcon(bool isPrivate, bool isFork)
339-
{
340-
}
341-
342-
public UriString GenerateUrl(string path = null, int startLine = -1, int endLine = -1)
343-
{
344-
return null;
345-
}
346-
347-
public string Name { get; set; }
348-
public UriString CloneUrl { get; set; }
349-
public string LocalPath { get; set; }
350-
351-
public Octicon Icon { get; set; }
352-
353-
public IAccount Owner { get; set; }
354-
355-
public void Refresh() { }
356329
}
357330

358331
public class RepositoryCloneViewModelDesigner : BaseViewModel, IRepositoryCloneViewModel
359332
{
360333
public RepositoryCloneViewModelDesigner()
361334
{
362-
var repositories = new ReactiveList<IRepositoryModel>
335+
Repositories = new ObservableCollection<IRepositoryModel>
363336
{
364-
new RepositoryModelDesigner("encourage", "haacked"),
365-
new RepositoryModelDesigner("haacked.com", "haacked"),
366-
new RepositoryModelDesigner("octokit.net", "octokit"),
367-
new RepositoryModelDesigner("octokit.rb", "octokit"),
368-
new RepositoryModelDesigner("octokit.objc", "octokit"),
369-
new RepositoryModelDesigner("windows", "github"),
370-
new RepositoryModelDesigner("mac", "github"),
371-
new RepositoryModelDesigner("github", "github")
337+
RepositoryModelDesigner.Create("encourage", "haacked"),
338+
RepositoryModelDesigner.Create("haacked.com", "haacked"),
339+
RepositoryModelDesigner.Create("octokit.net", "octokit"),
340+
RepositoryModelDesigner.Create("octokit.rb", "octokit"),
341+
RepositoryModelDesigner.Create("octokit.objc", "octokit"),
342+
RepositoryModelDesigner.Create("windows", "github"),
343+
RepositoryModelDesigner.Create("mac", "github"),
344+
RepositoryModelDesigner.Create("github", "github")
372345
};
373346

374347
BrowseForDirectory = ReactiveCommand.Create();
375348

376-
FilteredRepositories = repositories.CreateDerivedCollection(
377-
x => x
378-
);
379-
380349
BaseRepositoryPathValidator = ReactivePropertyValidator.ForObservable(this.WhenAny(x => x.BaseRepositoryPath, x => x.Value))
381350
.IfNullOrEmpty("Please enter a repository path")
382351
.IfTrue(x => x.Length > 200, "Path too long")
@@ -392,7 +361,7 @@ public IReactiveCommand<Unit> CloneCommand
392361

393362
public IRepositoryModel SelectedRepository { get; set; }
394363

395-
public IReactiveDerivedList<IRepositoryModel> FilteredRepositories
364+
public ObservableCollection<IRepositoryModel> Repositories
396365
{
397366
get;
398367
private set;

0 commit comments

Comments
 (0)