66using GitHub . Extensions ;
77using GitHub . Factories ;
88using GitHub . Models ;
9- using GitHub . SampleData ;
9+ using GitHub . Primitives ;
1010using GitHub . Services ;
1111using ReactiveUI ;
1212
@@ -23,6 +23,8 @@ public class PullRequestPageViewModel : PullRequestViewModelBase, IPullRequestPa
2323 readonly IPullRequestService service ;
2424 readonly IPullRequestSessionManager sessionManager ;
2525 readonly ITeamExplorerServices teServices ;
26+ ActorModel currentUserModel ;
27+ ReactiveList < IViewModel > timeline ;
2628
2729 /// <summary>
2830 /// Initializes a new instance of the <see cref="PullRequestPageViewModel"/> class.
@@ -52,7 +54,7 @@ public PullRequestPageViewModel(
5254 public IActorViewModel CurrentUser { get ; private set ; }
5355
5456 /// <inheritdoc/>
55- public IReadOnlyList < IViewModel > Timeline { get ; private set ; }
57+ public IReadOnlyList < IViewModel > Timeline => timeline ;
5658
5759 /// <inheritdoc/>
5860 public ReactiveCommand < string , Unit > ShowCommit { get ; }
@@ -69,9 +71,10 @@ public async Task InitializeAsync(
6971 {
7072 await base . InitializeAsync ( repository , localRepository , model ) . ConfigureAwait ( true ) ;
7173
74+ currentUserModel = currentUser ;
7275 CurrentUser = new ActorViewModel ( currentUser ) ;
76+ timeline = new ReactiveList < IViewModel > ( ) ;
7377
74- var timeline = new ReactiveList < IViewModel > ( ) ;
7578 var commits = new List < CommitSummaryViewModel > ( ) ;
7679
7780 foreach ( var i in model . Timeline )
@@ -88,11 +91,7 @@ public async Task InitializeAsync(
8891 commits . Add ( new CommitSummaryViewModel ( commit ) ) ;
8992 break ;
9093 case CommentModel comment :
91- {
92- var vm = factory . CreateViewModel < IIssueishCommentViewModel > ( ) ;
93- await vm . InitializeAsync ( this , currentUser , comment , null ) . ConfigureAwait ( true ) ;
94- timeline . Add ( vm ) ;
95- }
94+ await AddComment ( comment ) . ConfigureAwait ( true ) ;
9695 break ;
9796 }
9897 }
@@ -109,8 +108,15 @@ await placeholder.InitializeAsync(
109108 null ,
110109 Resources . ClosePullRequest ) . ConfigureAwait ( true ) ;
111110 timeline . Add ( placeholder ) ;
111+ }
112112
113- Timeline = timeline ;
113+ /// <inheritdoc/>
114+ public async Task PostComment ( string body )
115+ {
116+ var address = HostAddress . Create ( Repository . CloneUrl ) ;
117+ var comment = await service . PostComment ( address , Id , body ) . ConfigureAwait ( true ) ;
118+ await AddComment ( comment ) . ConfigureAwait ( true ) ;
119+ ClearPlaceholder ( ) ;
114120 }
115121
116122 Task ICommentThreadViewModel . DeleteComment ( int pullRequestId , int commentId )
@@ -123,9 +129,41 @@ Task ICommentThreadViewModel.EditComment(string id, string body)
123129 throw new NotImplementedException ( ) ;
124130 }
125131
126- Task ICommentThreadViewModel . PostComment ( string body )
132+ async Task AddComment ( CommentModel comment )
127133 {
128- throw new NotImplementedException ( ) ;
134+ var vm = factory . CreateViewModel < IIssueishCommentViewModel > ( ) ;
135+ await vm . InitializeAsync ( this , currentUserModel , comment , null ) . ConfigureAwait ( true ) ;
136+
137+ if ( GetPlaceholder ( ) == null )
138+ {
139+ timeline . Add ( vm ) ;
140+ }
141+ else
142+ {
143+ timeline . Insert ( timeline . Count - 1 , vm ) ;
144+ }
145+ }
146+
147+ void ClearPlaceholder ( )
148+ {
149+ var placeholder = GetPlaceholder ( ) ;
150+
151+ if ( placeholder != null )
152+ {
153+ placeholder . Body = null ;
154+ }
155+ }
156+
157+ ICommentViewModel GetPlaceholder ( )
158+ {
159+ if ( timeline . Count > 0 &&
160+ timeline [ timeline . Count - 1 ] is ICommentViewModel comment &&
161+ comment . Id == null )
162+ {
163+ return comment ;
164+ }
165+
166+ return null ;
129167 }
130168
131169 async Task DoShowCommit ( string oid )
0 commit comments