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

Commit a397f25

Browse files
committed
Expose the PR model from view model.
And bind to that where we don't need to do any transformation for the view.
1 parent c996266 commit a397f25

File tree

8 files changed

+49
-142
lines changed

8 files changed

+49
-142
lines changed

src/GitHub.App/Models/PullRequestModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace GitHub.Models
1010
{
1111
[DebuggerDisplay("{DebuggerDisplay,nq}")]
12+
[NullGuard(ValidationFlags.None)]
1213
public sealed class PullRequestModel : NotificationAwareObject, IPullRequestModel,
1314
IEquatable<PullRequestModel>,
1415
IComparable<PullRequestModel>

src/GitHub.App/SampleData/PullRequestDetailViewModelDesigner.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ public class PullRequestDetailViewModelDesigner : BaseViewModel, IPullRequestDet
1212
{
1313
public PullRequestDetailViewModelDesigner()
1414
{
15-
Title = "Error handling/bubbling from viewmodels to views to viewhosts";
16-
State = PullRequestStateEnum.Open;
15+
Model = new PullRequestModel(419,
16+
"Error handling/bubbling from viewmodels to views to viewhosts",
17+
new AccountDesigner { Login = "shana", IsUser = true },
18+
DateTime.Now.Subtract(TimeSpan.FromDays(3)))
19+
{
20+
State = PullRequestStateEnum.Open,
21+
CommitCount = 9,
22+
};
23+
1724
SourceBranchDisplayName = "shana/error-handling";
1825
TargetBranchDisplayName = "master";
19-
CommitCount = 9;
20-
Author = new AccountDesigner { Login = "shana", IsUser = true };
21-
CreatedAt = DateTime.Now.Subtract(TimeSpan.FromDays(3));
22-
Number = 419;
2326
Body = @"Adds a way to surface errors from the view model to the view so that view hosts can get to them.
2427
2528
ViewModels are responsible for handling the UI on the view they control, but they shouldn't be handling UI for things outside of the view. In this case, we're showing errors in VS outside the view, and that should be handled by the section that is hosting the view.
@@ -41,7 +44,6 @@ public PullRequestDetailViewModelDesigner()
4144
modelsDir.Files.Add(oldBranchModel);
4245
gitHubDir.Directories.Add(modelsDir);
4346

44-
ChangedFilesCount = 3;
4547
ChangedFilesTree = new ReactiveList<IPullRequestChangeNode>();
4648
ChangedFilesTree.Add(gitHubDir);
4749

@@ -53,16 +55,11 @@ public PullRequestDetailViewModelDesigner()
5355
CheckoutMode = CheckoutMode.Fetch;
5456
}
5557

56-
public PullRequestStateEnum State { get; }
58+
public IPullRequestModel Model { get; }
5759
public string SourceBranchDisplayName { get; }
5860
public string TargetBranchDisplayName { get; }
59-
public int CommitCount { get; }
60-
public IAccount Author { get; }
61-
public DateTimeOffset CreatedAt { get; }
62-
public int Number { get; }
6361
public string Body { get; }
64-
public int ChangedFilesCount { get; }
65-
public ChangedFilesView ChangedFilesView { get; set; }
62+
public ChangedFilesViewType ChangedFilesViewType { get; set; }
6663
public OpenChangedFileAction OpenChangedFileAction { get; set; }
6764
public IReactiveList<IPullRequestChangeNode> ChangedFilesTree { get; }
6865
public IReactiveList<IPullRequestFileViewModel> ChangedFilesList { get; }

src/GitHub.App/Services/ModelService.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,6 @@ public ITrackingCollection<IPullRequestModel> GetPullRequests(ILocalRepositoryMo
180180

181181
public IObservable<IPullRequestModel> GetPullRequest(ILocalRepositoryModel repo, int number)
182182
{
183-
var keyobs = GetUserFromCache()
184-
.Select(user => string.Format(CultureInfo.InvariantCulture, "{0}|{1}:{2}", CacheIndex.PRPrefix, user.Login, repo.Name));
185-
186183
return Observable.Defer(() =>
187184
{
188185
return hostCache.GetAndRefreshObject(PRPrefix + '|' + number, () =>

src/GitHub.App/ViewModels/PullRequestDetailViewModel.cs

Lines changed: 19 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,14 @@ namespace GitHub.ViewModels
2626
[NullGuard(ValidationFlags.None)]
2727
public class PullRequestDetailViewModel : BaseViewModel, IPullRequestDetailViewModel
2828
{
29-
readonly IRepositoryHost repositoryHost;
3029
readonly ILocalRepositoryModel repository;
3130
readonly IModelService modelService;
3231
readonly IPullRequestService pullRequestsService;
3332
IPullRequestModel model;
34-
PullRequestStateEnum state;
3533
string sourceBranchDisplayName;
3634
string targetBranchDisplayName;
37-
int commitCount;
38-
IAccount author;
39-
DateTimeOffset createdAt;
4035
string body;
41-
int number;
42-
int changeCount;
43-
ChangedFilesView changedFilesView;
36+
ChangedFilesViewType changedFilesViewType;
4437
OpenChangedFileAction openChangedFileAction;
4538
CheckoutMode checkoutMode;
4639
string checkoutError;
@@ -59,8 +52,7 @@ public class PullRequestDetailViewModel : BaseViewModel, IPullRequestDetailViewM
5952
IConnectionRepositoryHostMap connectionRepositoryHostMap,
6053
ITeamExplorerServiceHolder teservice,
6154
IPullRequestService pullRequestsService)
62-
: this(connectionRepositoryHostMap.CurrentRepositoryHost,
63-
teservice.ActiveRepo,
55+
: this(teservice.ActiveRepo,
6456
connectionRepositoryHostMap.CurrentRepositoryHost.ModelService,
6557
pullRequestsService)
6658
{
@@ -74,12 +66,10 @@ public class PullRequestDetailViewModel : BaseViewModel, IPullRequestDetailViewM
7466
/// <param name="pullRequestsService">The pull requests service.</param>
7567
/// <param name="avatarProvider">The avatar provider.</param>
7668
public PullRequestDetailViewModel(
77-
IRepositoryHost repositoryHost,
7869
ILocalRepositoryModel repository,
7970
IModelService modelService,
8071
IPullRequestService pullRequestsService)
8172
{
82-
this.repositoryHost = repositoryHost;
8373
this.repository = repository;
8474
this.modelService = modelService;
8575
this.pullRequestsService = pullRequestsService;
@@ -95,8 +85,8 @@ public PullRequestDetailViewModel(
9585
ToggleChangedFilesView = ReactiveCommand.Create();
9686
ToggleChangedFilesView.Subscribe(_ =>
9787
{
98-
ChangedFilesView = ChangedFilesView == ChangedFilesView.TreeView ?
99-
ChangedFilesView.ListView : ChangedFilesView.TreeView;
88+
ChangedFilesViewType = ChangedFilesViewType == ChangedFilesViewType.TreeView ?
89+
ChangedFilesViewType.ListView : ChangedFilesViewType.TreeView;
10090
});
10191

10292
ToggleOpenChangedFileAction = ReactiveCommand.Create();
@@ -111,12 +101,12 @@ public PullRequestDetailViewModel(
111101
}
112102

113103
/// <summary>
114-
/// Gets the state of the pull request, e.g. Open, Closed, Merged.
104+
/// Gets the underlying pull request model.
115105
/// </summary>
116-
public PullRequestStateEnum State
106+
public IPullRequestModel Model
117107
{
118-
get { return state; }
119-
private set { this.RaiseAndSetIfChanged(ref state, value); }
108+
get { return model; }
109+
private set { this.RaiseAndSetIfChanged(ref model, value); }
120110
}
121111

122112
/// <summary>
@@ -137,42 +127,6 @@ public string TargetBranchDisplayName
137127
private set { this.RaiseAndSetIfChanged(ref targetBranchDisplayName, value); }
138128
}
139129

140-
/// <summary>
141-
/// Gets the number of commits in the pull request.
142-
/// </summary>
143-
public int CommitCount
144-
{
145-
get { return commitCount; }
146-
private set { this.RaiseAndSetIfChanged(ref commitCount, value); }
147-
}
148-
149-
/// <summary>
150-
/// Gets the pull request number.
151-
/// </summary>
152-
public int Number
153-
{
154-
get { return number; }
155-
private set { this.RaiseAndSetIfChanged(ref number, value); }
156-
}
157-
158-
/// <summary>
159-
/// Gets the account that submitted the pull request.
160-
/// </summary>
161-
public IAccount Author
162-
{
163-
get { return author; }
164-
private set { this.RaiseAndSetIfChanged(ref author, value); }
165-
}
166-
167-
/// <summary>
168-
/// Gets the date and time at which the pull request was created.
169-
/// </summary>
170-
public DateTimeOffset CreatedAt
171-
{
172-
get { return createdAt; }
173-
private set { this.RaiseAndSetIfChanged(ref createdAt, value); }
174-
}
175-
176130
/// <summary>
177131
/// Gets the pull request body.
178132
/// </summary>
@@ -182,22 +136,13 @@ public string Body
182136
private set { this.RaiseAndSetIfChanged(ref body, value); }
183137
}
184138

185-
/// <summary>
186-
/// Gets the number of files that have been changed in the pull request.
187-
/// </summary>
188-
public int ChangedFilesCount
189-
{
190-
get { return changeCount; }
191-
private set { this.RaiseAndSetIfChanged(ref changeCount, value); }
192-
}
193-
194139
/// <summary>
195140
/// Gets or sets a value describing how changed files are displayed in a view.
196141
/// </summary>
197-
public ChangedFilesView ChangedFilesView
142+
public ChangedFilesViewType ChangedFilesViewType
198143
{
199-
get { return changedFilesView; }
200-
set { this.RaiseAndSetIfChanged(ref changedFilesView, value); }
144+
get { return changedFilesViewType; }
145+
set { this.RaiseAndSetIfChanged(ref changedFilesViewType, value); }
201146
}
202147

203148
/// <summary>
@@ -267,7 +212,7 @@ public string CheckoutDisabledMessage
267212
public ReactiveCommand<object> OpenOnGitHub { get; }
268213

269214
/// <summary>
270-
/// Gets a command that toggles the <see cref="ChangedFilesView"/> property.
215+
/// Gets a command that toggles the <see cref="ChangedFilesViewType"/> property.
271216
/// </summary>
272217
public ReactiveCommand<object> ToggleChangedFilesView { get; }
273218

@@ -308,17 +253,10 @@ public override void Initialize([AllowNull] ViewWithData data)
308253
/// <param name="files">The pull request's changed files.</param>
309254
public async Task Load(IPullRequestModel pullRequest)
310255
{
311-
model = pullRequest;
312-
State = pullRequest.State;
256+
Model = pullRequest;
313257
SourceBranchDisplayName = GetBranchDisplayName(pullRequest.Head.Label);
314258
TargetBranchDisplayName = GetBranchDisplayName(pullRequest.Base.Label);
315-
CommitCount = pullRequest.CommitCount;
316-
Title = pullRequest.Title;
317-
Number = pullRequest.Number;
318-
Author = pullRequest.Author;
319-
CreatedAt = pullRequest.CreatedAt;
320259
Body = !string.IsNullOrWhiteSpace(pullRequest.Body) ? pullRequest.Body : "*No description provided.*";
321-
ChangedFilesCount = pullRequest.ChangedFiles.Count;
322260

323261
ChangedFilesTree.Clear();
324262
ChangedFilesList.Clear();
@@ -338,7 +276,7 @@ public async Task Load(IPullRequestModel pullRequest)
338276

339277
if (localBranches.Contains(repository.CurrentBranch))
340278
{
341-
var divergence = await pullRequestsService.CalculateHistoryDivergence(repository, Number);
279+
var divergence = await pullRequestsService.CalculateHistoryDivergence(repository, Model.Number);
342280

343281
if (divergence.BehindBy == null)
344282
{
@@ -454,17 +392,17 @@ IObservable<Unit> DoCheckout(object unused)
454392
break;
455393
case CheckoutMode.Fetch:
456394
operation = pullRequestsService
457-
.GetDefaultLocalBranchName(repository, Number, Title)
458-
.SelectMany(x => pullRequestsService.FetchAndCheckout(repository, Number, x));
395+
.GetDefaultLocalBranchName(repository, Model.Number, Title)
396+
.SelectMany(x => pullRequestsService.FetchAndCheckout(repository, Model.Number, x));
459397
break;
460398
case CheckoutMode.Switch:
461-
operation = pullRequestsService.SwitchToBranch(repository, model);
399+
operation = pullRequestsService.SwitchToBranch(repository, Model);
462400
break;
463401
case CheckoutMode.InvalidState:
464402
operation = pullRequestsService
465403
.UnmarkLocalBranch(repository)
466-
.SelectMany(_ => pullRequestsService.GetDefaultLocalBranchName(repository, Number, Title))
467-
.SelectMany(x => pullRequestsService.FetchAndCheckout(repository, Number, x));
404+
.SelectMany(_ => pullRequestsService.GetDefaultLocalBranchName(repository, Model.Number, Title))
405+
.SelectMany(x => pullRequestsService.FetchAndCheckout(repository, Model.Number, x));
468406
break;
469407
default:
470408
Debug.Fail("Invalid CheckoutMode in PullRequestDetailViewModel.DoCheckout.");

src/GitHub.Exports.Reactive/ViewModels/IPullRequestDetailViewModel.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GitHub.ViewModels
88
/// <summary>
99
/// Describes how changed files are displayed in a the pull request details view.
1010
/// </summary>
11-
public enum ChangedFilesView
11+
public enum ChangedFilesViewType
1212
{
1313
/// <summary>
1414
/// The files are displayed as a tree.
@@ -81,9 +81,9 @@ public enum CheckoutMode
8181
public interface IPullRequestDetailViewModel : IViewModel, IHasBusy
8282
{
8383
/// <summary>
84-
/// Gets the state of the pull request, e.g. Open, Closed, Merged.
84+
/// Gets the underlying pull request model.
8585
/// </summary>
86-
PullRequestStateEnum State { get; }
86+
IPullRequestModel Model { get; }
8787

8888
/// <summary>
8989
/// Gets a string describing how to display the pull request's source branch.
@@ -95,40 +95,15 @@ public interface IPullRequestDetailViewModel : IViewModel, IHasBusy
9595
/// </summary>
9696
string TargetBranchDisplayName { get; }
9797

98-
/// <summary>
99-
/// Gets the number of commits in the pull request.
100-
/// </summary>
101-
int CommitCount { get; }
102-
103-
/// <summary>
104-
/// Gets the pull request number.
105-
/// </summary>
106-
int Number { get; }
107-
108-
/// <summary>
109-
/// Gets the account that submitted the pull request.
110-
/// </summary>
111-
IAccount Author { get; }
112-
113-
/// <summary>
114-
/// Gets the date and time at which the pull request was created.
115-
/// </summary>
116-
DateTimeOffset CreatedAt { get; }
117-
11898
/// <summary>
11999
/// Gets the pull request body.
120100
/// </summary>
121101
string Body { get; }
122102

123-
/// <summary>
124-
/// Gets the number of files that have been changed in the pull request.
125-
/// </summary>
126-
int ChangedFilesCount { get; }
127-
128103
/// <summary>
129104
/// Gets or sets a value describing how changed files are displayed in a view.
130105
/// </summary>
131-
ChangedFilesView ChangedFilesView { get; set; }
106+
ChangedFilesViewType ChangedFilesViewType { get; set; }
132107

133108
/// <summary>
134109
/// Gets or sets a value describing how files are opened when double clicked.
@@ -177,7 +152,7 @@ public interface IPullRequestDetailViewModel : IViewModel, IHasBusy
177152
ReactiveCommand<object> OpenOnGitHub { get; }
178153

179154
/// <summary>
180-
/// Gets a command that toggles the <see cref="ChangedFilesView"/> property.
155+
/// Gets a command that toggles the <see cref="ChangedFilesViewType"/> property.
181156
/// </summary>
182157
ReactiveCommand<object> ToggleChangedFilesView { get; }
183158

0 commit comments

Comments
 (0)