This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathPullRequestDiffViewController.cs
More file actions
220 lines (193 loc) · 7.73 KB
/
PullRequestDiffViewController.cs
File metadata and controls
220 lines (193 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using CodeHub.Core;
using CodeHub.Core.Services;
using CodeHub.iOS.Services;
using CodeHub.iOS.Utilities;
using CodeHub.WebViews;
using Humanizer;
using Newtonsoft.Json;
using ReactiveUI;
using Splat;
using UIKit;
using WebKit;
namespace CodeHub.iOS.ViewControllers.PullRequests
{
public class PullRequestDiffViewController : BaseWebViewController
{
private readonly IApplicationService _applicationService;
private readonly INetworkActivityService _networkActivityService;
private readonly IMarkdownService _markdownService;
private readonly string _username;
private readonly string _repository;
private readonly int _pullRequestId;
private readonly string _path;
private readonly string _patch;
private readonly string _commit;
private readonly ReactiveList<Octokit.PullRequestReviewComment> _comments
= new ReactiveList<Octokit.PullRequestReviewComment>();
public PullRequestDiffViewController(
string username,
string repository,
int pullRequestId,
string path,
string patch,
string commit,
IApplicationService applicationService = null,
INetworkActivityService networkActivityService = null,
IMarkdownService markdownService = null)
: base(false)
{
_applicationService = applicationService ?? Locator.Current.GetService<IApplicationService>();
_networkActivityService = networkActivityService ?? Locator.Current.GetService<INetworkActivityService>();
_markdownService = markdownService ?? Locator.Current.GetService<IMarkdownService>();
_username = username;
_repository = repository;
_pullRequestId = pullRequestId;
_path = path;
_patch = patch;
_commit = commit;
Title = string.IsNullOrEmpty(_path) ? "Diff" : System.IO.Path.GetFileName(_path);
var loadComments = ReactiveCommand.CreateFromTask(
_ => _applicationService.GitHubClient.PullRequest.ReviewComment.GetAll(_username, _repository, _pullRequestId));
loadComments
.ThrownExceptions
.Select(error => new UserError("Unable to load comments.", error))
.SelectMany(Interactions.Errors.Handle)
.Subscribe();
loadComments
.Subscribe(comments => _comments.Reset(comments));
var loadAll = ReactiveCommand.CreateCombined(new[] { loadComments });
Appearing
.Take(1)
.Select(_ => Unit.Default)
.InvokeReactiveCommand(loadAll);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
Observable
.Return(Unit.Default)
.Merge(_comments.Changed.Select(_ => Unit.Default))
.Do(_ => Render().ToBackground())
.Subscribe();
}
private async Task Render()
{
var comments = new List<DiffCommentModel>();
foreach (var comment in _comments.Where(x => string.Equals(x.Path, _path)))
{
comments.Add(new DiffCommentModel
{
Id = comment.Id,
GroupId = comment.Id,
Username = comment.User.Login,
AvatarUrl = comment.User.AvatarUrl,
LineTo = comment.Position,
LineFrom = comment.Position,
Body = await _markdownService.Convert(comment.Body),
Date = comment.CreatedAt.Humanize()
});
}
var diffModel = new DiffModel(
_patch.Split('\n'),
comments,
(int)UIFont.PreferredSubheadline.PointSize);
var diffView = new DiffWebView { Model = diffModel };
LoadContent(diffView.GenerateString());
}
private class JavascriptComment
{
public int PatchLine { get; set; }
public int FileLine { get; set; }
}
private class JavascriptReplyComment
{
public int Id { get; set; }
}
protected override bool ShouldStartLoad(WKWebView webView, WKNavigationAction navigationAction)
{
var url = navigationAction.Request.Url;
if (url.Scheme.Equals("app"))
{
var func = url.Host;
if (func.Equals("comment"))
{
var commentModel = JsonConvert.DeserializeObject<JavascriptComment>(UrlDecode(url.Fragment));
PromptForComment(commentModel);
}
else if (func.Equals("reply-to"))
{
var commentModel = JsonConvert.DeserializeObject<JavascriptReplyComment>(UrlDecode(url.Fragment));
ShowReplyCommentComposer(commentModel.Id);
}
return false;
}
return base.ShouldStartLoad(webView, navigationAction);
}
private void PromptForComment(JavascriptComment model)
{
var title = "Line " + model.PatchLine;
var sheet = new UIActionSheet(title);
var addButton = sheet.AddButton("Add Comment");
var cancelButton = sheet.AddButton("Cancel");
sheet.CancelButtonIndex = cancelButton;
sheet.Dismissed += (sender, e) =>
{
BeginInvokeOnMainThread(() =>
{
if (e.ButtonIndex == addButton)
ShowCommentComposer(model.FileLine);
});
sheet.Dispose();
};
sheet.ShowInView(this.View);
}
private void ShowCommentComposer(int line)
{
ShowComposer(async text =>
{
var commentOptions = new Octokit.PullRequestReviewCommentCreate(text, _commit, _path, line);
var comment = await _applicationService.GitHubClient.PullRequest.ReviewComment.Create(
_username, _repository, _pullRequestId, commentOptions);
_comments.Add(comment);
});
}
private void ShowReplyCommentComposer(int replyToId)
{
ShowComposer(async text =>
{
var commentOptions = new Octokit.PullRequestReviewCommentReplyCreate(text, replyToId);
var comment = await _applicationService.GitHubClient.PullRequest.ReviewComment.CreateReply(
_username, _repository, _pullRequestId, commentOptions);
_comments.Add(comment);
});
}
private void ShowComposer(Func<string, Task> workFn)
{
var composer = new MarkdownComposerViewController();
composer.PresentAsModal(this, async text =>
{
var hud = composer.CreateHud();
using (UIApplication.SharedApplication.DisableInteraction())
using (_networkActivityService.ActivateNetwork())
using (hud.Activate("Commenting..."))
{
try
{
await workFn(text);
composer.DismissViewController(true, null);
}
catch (Exception e)
{
AlertDialogService.ShowAlert("Unable to Comment", e.Message);
}
}
});
}
}
}