use of com.gh4a.utils.Optional in project gh4a by slapperwan.
the class ReviewFragment method onCreateDataSingle.
@Override
protected Single<List<TimelineItem>> onCreateDataSingle(boolean bypassCache) {
final PullRequestService prService = ServiceFactory.get(PullRequestService.class, bypassCache);
final PullRequestReviewService reviewService = ServiceFactory.get(PullRequestReviewService.class, bypassCache);
final PullRequestReviewCommentService commentService = ServiceFactory.get(PullRequestReviewCommentService.class, bypassCache);
Single<TimelineItem.TimelineReview> reviewItemSingle = reviewService.getReview(mRepoOwner, mRepoName, mIssueNumber, mReview.id()).map(ApiHelpers::throwOnFailure).map(TimelineItem.TimelineReview::new);
Single<List<ReviewComment>> reviewCommentsSingle = ApiHelpers.PageIterator.toSingle(page -> reviewService.getReviewComments(mRepoOwner, mRepoName, mIssueNumber, mReview.id())).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).cache();
Single<Boolean> hasCommentsSingle = reviewCommentsSingle.map(comments -> !comments.isEmpty());
Single<Optional<List<GitHubFile>>> filesSingle = hasCommentsSingle.flatMap(hasComments -> {
if (!hasComments) {
return Single.just(Optional.absent());
}
return ApiHelpers.PageIterator.toSingle(page -> prService.getPullRequestFiles(mRepoOwner, mRepoName, mIssueNumber, page)).map(Optional::of);
});
Single<Optional<List<ReviewComment>>> commentsSingle = hasCommentsSingle.flatMap(hasComments -> {
if (!hasComments) {
return Single.just(Optional.absent());
}
return ApiHelpers.PageIterator.toSingle(page -> commentService.getPullRequestComments(mRepoOwner, mRepoName, mIssueNumber, page)).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).map(Optional::of);
});
return Single.zip(reviewItemSingle, reviewCommentsSingle, filesSingle, commentsSingle, (reviewItem, reviewComments, filesOpt, commentsOpt) -> {
if (!reviewComments.isEmpty()) {
HashMap<String, GitHubFile> filesByName = new HashMap<>();
if (filesOpt.isPresent()) {
for (GitHubFile file : filesOpt.get()) {
filesByName.put(file.filename(), file);
}
}
// Add all of the review comments to the review item creating necessary diff hunks
for (ReviewComment reviewComment : reviewComments) {
GitHubFile file = filesByName.get(reviewComment.path());
reviewItem.addComment(reviewComment, file, true);
}
if (commentsOpt.isPresent()) {
for (ReviewComment commitComment : commentsOpt.get()) {
if (reviewComments.contains(commitComment)) {
continue;
}
// Rest of the comments should be added only if they are under the same
// diff hunks as the original review comments.
GitHubFile file = filesByName.get(commitComment.path());
reviewItem.addComment(commitComment, file, false);
}
}
}
List<TimelineItem> items = new ArrayList<>();
items.add(reviewItem);
List<TimelineItem.Diff> diffHunks = new ArrayList<>(reviewItem.getDiffHunks());
Collections.sort(diffHunks);
for (TimelineItem.Diff diffHunk : diffHunks) {
items.add(diffHunk);
items.addAll(diffHunk.comments);
if (!diffHunk.isReply()) {
items.add(new TimelineItem.Reply(diffHunk.getInitialTimelineComment()));
}
}
return items;
});
}
use of com.gh4a.utils.Optional in project gh4a by slapperwan.
the class PullRequestFragment method loadHeadReference.
private void loadHeadReference(boolean force) {
GitService service = ServiceFactory.get(GitService.class, force);
PullRequestMarker head = mPullRequest.head();
Repository repo = head.repo();
Single<Optional<GitReference>> refSingle = repo == null ? Single.just(Optional.absent()) : service.getGitReference(repo.owner().login(), repo.name(), head.ref()).map(ApiHelpers::throwOnFailure).map(Optional::of).compose(RxUtils.mapFailureToValue(HttpURLConnection.HTTP_NOT_FOUND, Optional.<GitReference>absent())).compose(makeLoaderSingle(ID_LOADER_HEAD_REF, force));
refSingle.subscribe(refOpt -> {
mHeadReference = refOpt.orNull();
mHasLoadedHeadReference = true;
getActivity().invalidateOptionsMenu();
bindSpecialViews(mListHeaderView);
}, this::handleLoadFailure);
}
use of com.gh4a.utils.Optional in project gh4a by slapperwan.
the class EventListFragment method onItemClick.
@Override
public void onItemClick(GitHubEvent event) {
if (EventAdapter.hasInvalidPayload(event)) {
return;
}
GitHubEvent.RepoIdentifier eventRepo = event.repo();
String repoOwner = "";
String repoName = "";
Intent intent = null;
Single<Optional<Intent>> intentSingle = null;
if (eventRepo != null) {
String[] repoNamePart = eventRepo.repoWithUserName().split("/");
if (repoNamePart.length == 2) {
repoOwner = repoNamePart[0];
repoName = repoNamePart[1];
}
}
if (Arrays.binarySearch(REPO_EVENTS, event.type()) >= 0 && eventRepo == null) {
Toast.makeText(getActivity(), R.string.repo_not_found_toast, Toast.LENGTH_LONG).show();
return;
}
switch(event.type()) {
case CommitCommentEvent:
{
CommitCommentPayload payload = (CommitCommentPayload) event.payload();
GitComment comment = payload.comment();
if (comment != null) {
intentSingle = CommitCommentLoadTask.load(getActivity(), repoOwner, repoName, comment.commitId(), new IntentUtils.InitialCommentMarker(comment.id()));
}
break;
}
case CreateEvent:
{
CreatePayload payload = (CreatePayload) event.payload();
String ref = null;
if (payload.refType() == ReferenceType.Branch || payload.refType() == ReferenceType.Tag) {
ref = payload.ref();
}
intent = RepositoryActivity.makeIntent(getActivity(), repoOwner, repoName, ref);
break;
}
case DeleteEvent:
intent = RepositoryActivity.makeIntent(getActivity(), repoOwner, repoName);
break;
case DownloadEvent:
{
DownloadPayload payload = (DownloadPayload) event.payload();
Download download = payload.download();
UiUtils.enqueueDownloadWithPermissionCheck((BaseActivity) getActivity(), download.htmlUrl(), download.contentType(), download.name(), download.description(), null);
break;
}
case FollowEvent:
{
FollowPayload payload = (FollowPayload) event.payload();
intent = UserActivity.makeIntent(getActivity(), payload.target());
break;
}
case ForkEvent:
{
ForkPayload payload = (ForkPayload) event.payload();
Repository forkee = payload.forkee();
if (forkee != null) {
intent = RepositoryActivity.makeIntent(getActivity(), forkee);
} else {
Toast.makeText(getActivity(), R.string.repo_not_found_toast, Toast.LENGTH_LONG).show();
}
break;
}
case ForkApplyEvent:
intent = RepositoryActivity.makeIntent(getActivity(), repoOwner, repoName);
break;
case GistEvent:
{
GistPayload payload = (GistPayload) event.payload();
intent = GistActivity.makeIntent(getActivity(), payload.gist().id());
break;
}
case GollumEvent:
{
GollumPayload payload = (GollumPayload) event.payload();
intent = WikiListActivity.makeIntent(getActivity(), repoOwner, repoName, payload.pages().isEmpty() ? null : payload.pages().get(0));
break;
}
case IssueCommentEvent:
{
IssueCommentPayload payload = (IssueCommentPayload) event.payload();
Issue issue = payload.issue();
PullRequest request = issue != null ? issue.pullRequest() : null;
IntentUtils.InitialCommentMarker initialComment = payload.comment() != null ? new IntentUtils.InitialCommentMarker(payload.comment().id()) : null;
if (request != null && request.htmlUrl() != null) {
intent = PullRequestActivity.makeIntent(getActivity(), repoOwner, repoName, issue.number(), initialComment != null ? PullRequestActivity.PAGE_CONVERSATION : -1, initialComment);
} else if (issue != null) {
intent = IssueActivity.makeIntent(getActivity(), repoOwner, repoName, issue.number(), initialComment);
}
break;
}
case IssuesEvent:
{
IssuesPayload payload = (IssuesPayload) event.payload();
startActivity(IssueActivity.makeIntent(getActivity(), repoOwner, repoName, payload.issue().number()));
break;
}
case MemberEvent:
intent = RepositoryActivity.makeIntent(getActivity(), repoOwner, repoName);
break;
case PublicEvent:
intent = RepositoryActivity.makeIntent(getActivity(), repoOwner, repoName);
break;
case PullRequestEvent:
{
PullRequestPayload payload = (PullRequestPayload) event.payload();
intent = PullRequestActivity.makeIntent(getActivity(), repoOwner, repoName, payload.number());
break;
}
case PullRequestReviewCommentEvent:
{
PullRequestReviewCommentPayload payload = (PullRequestReviewCommentPayload) event.payload();
PullRequest pr = payload.pullRequest();
ReviewComment comment = payload.comment();
IntentUtils.InitialCommentMarker initialComment = comment != null ? new IntentUtils.InitialCommentMarker(comment.id()) : null;
if (pr != null) {
if (initialComment != null) {
intentSingle = PullRequestReviewCommentLoadTask.load(getActivity(), repoOwner, repoName, pr.number(), initialComment);
} else {
intent = PullRequestActivity.makeIntent(getActivity(), repoOwner, repoName, pr.number(), -1, null);
}
} else if (comment != null) {
intent = CommitActivity.makeIntent(getActivity(), repoOwner, repoName, comment.commitId(), initialComment);
}
break;
}
case PushEvent:
{
PushPayload payload = (PushPayload) event.payload();
List<GitCommit> commits = payload.commits();
if (commits != null && !commits.isEmpty()) {
if (commits.size() > 1) {
// if commit > 1, then show compare activity
intent = CompareActivity.makeIntent(getActivity(), repoOwner, repoName, payload.before(), payload.head());
} else {
// only 1 commit, then show the commit details
intent = CommitActivity.makeIntent(getActivity(), repoOwner, repoName, commits.get(0).sha());
}
} else {
intent = RepositoryActivity.makeIntent(getActivity(), repoOwner, repoName);
}
break;
}
case ReleaseEvent:
{
ReleasePayload payload = (ReleasePayload) event.payload();
Release release = payload.release();
if (release != null) {
intent = ReleaseInfoActivity.makeIntent(getActivity(), repoOwner, repoName, release.id());
}
break;
}
case WatchEvent:
intent = RepositoryActivity.makeIntent(getActivity(), repoOwner, repoName);
break;
}
if (intent != null) {
startActivity(intent);
} else if (intentSingle != null) {
intentSingle.compose(RxUtils::doInBackground).compose(RxUtils.wrapWithProgressDialog(getActivity(), R.string.loading_msg)).subscribe(result -> {
if (result.isPresent() && isAdded()) {
startActivity(result.get());
}
}, error -> Log.d(Gh4Application.LOG_TAG, "Loading click intent failed", error));
}
}
use of com.gh4a.utils.Optional in project gh4a by slapperwan.
the class PullRequestDiffCommentLoadTask method getSingle.
@Override
protected Single<Optional<Intent>> getSingle() {
PullRequestService service = ServiceFactory.get(PullRequestService.class, false);
Single<PullRequest> pullRequestSingle = service.getPullRequest(mRepoOwner, mRepoName, mPullRequestNumber).map(ApiHelpers::throwOnFailure);
final PullRequestReviewCommentService commentService = ServiceFactory.get(PullRequestReviewCommentService.class, false);
Single<List<ReviewComment>> commentsSingle = ApiHelpers.PageIterator.toSingle(page -> commentService.getPullRequestComments(mRepoOwner, mRepoName, mPullRequestNumber, page)).compose(RxUtils.filter(c -> c.position() != null)).cache();
Single<List<GitHubFile>> filesSingle = ApiHelpers.PageIterator.toSingle(page -> service.getPullRequestFiles(mRepoOwner, mRepoName, mPullRequestNumber, page));
return commentsSingle.compose(RxUtils.filterAndMapToFirst(c -> mMarker.matches(c.id(), c.createdAt()))).zipWith(filesSingle, (commentOpt, files) -> commentOpt.map(comment -> {
for (GitHubFile commitFile : files) {
if (commitFile.filename().equals(comment.path())) {
return Pair.create(true, commitFile);
}
}
return Pair.create(comment != null, (GitHubFile) null);
})).flatMap(result -> {
if (result.isPresent()) {
boolean foundComment = result.get().first;
GitHubFile file = result.get().second;
if (foundComment && file != null && !FileUtils.isImage(file.filename())) {
return Single.zip(pullRequestSingle, commentsSingle, (pr, comments) -> {
// noinspection CodeBlock2Expr
return Optional.of(PullRequestDiffViewerActivity.makeIntent(mActivity, mRepoOwner, mRepoName, mPullRequestNumber, pr.head().sha(), file.filename(), file.patch(), comments, -1, -1, -1, false, mMarker));
});
}
if (foundComment && file == null) {
return Single.just(Optional.of(PullRequestActivity.makeIntent(mActivity, mRepoOwner, mRepoName, mPullRequestNumber, mPage, mMarker)));
}
}
return Single.just(Optional.absent());
});
}
use of com.gh4a.utils.Optional in project gh4a by slapperwan.
the class PullRequestReviewCommentLoadTask method load.
public static Single<Optional<Intent>> load(Context context, String repoOwner, String repoName, int pullRequestNumber, IntentUtils.InitialCommentMarker marker) {
final PullRequestReviewService reviewService = ServiceFactory.get(PullRequestReviewService.class, false);
final PullRequestReviewCommentService commentService = ServiceFactory.get(PullRequestReviewCommentService.class, false);
return ApiHelpers.PageIterator.toSingle(page -> commentService.getPullRequestComments(repoOwner, repoName, pullRequestNumber, page)).compose(RxUtils.sortList(ApiHelpers.COMMENT_COMPARATOR)).flatMap(comments -> {
Map<String, ReviewComment> commentsByDiffHunkId = new HashMap<>();
for (ReviewComment comment : comments) {
String id = TimelineItem.Diff.getDiffHunkId(comment);
if (!commentsByDiffHunkId.containsKey(id)) {
// Because the comment we are looking for could be a reply to another
// review we have to keep track of initial comments for each diff hunk
commentsByDiffHunkId.put(id, comment);
}
if (marker.matches(comment.id(), null)) {
// Once found the comment we are looking for get a correct review id from
// the initial diff hunk comment
ReviewComment initialComment = commentsByDiffHunkId.get(id);
long reviewId = initialComment.pullRequestReviewId();
return reviewService.getReview(repoOwner, repoName, pullRequestNumber, reviewId).map(ApiHelpers::throwOnFailure).map(Optional::of);
}
}
return Single.just(Optional.<Review>absent());
}).map(reviewOpt -> reviewOpt.map(review -> ReviewActivity.makeIntent(context, repoOwner, repoName, pullRequestNumber, review, marker)));
}
Aggregations