Search in sources :

Example 1 with CreateIssueModel

use of com.fastaccess.data.dao.CreateIssueModel in project FastHub by k0shk0sh.

the class CreateIssuePresenter method onSubmit.

@Override
public void onSubmit(@NonNull String title, @NonNull CharSequence description, @NonNull String login, @NonNull String repo, @Nullable Issue issue, @Nullable PullRequest pullRequestModel, @Nullable ArrayList<LabelModel> labels, @Nullable MilestoneModel milestoneModel, @Nullable ArrayList<User> users) {
    boolean isEmptyTitle = InputHelper.isEmpty(title);
    if (getView() != null) {
        getView().onTitleError(isEmptyTitle);
    }
    if (!isEmptyTitle) {
        if (issue == null && pullRequestModel == null) {
            CreateIssueModel createIssue = new CreateIssueModel();
            createIssue.setBody(InputHelper.toString(description));
            createIssue.setTitle(title);
            if (isCollaborator) {
                if (labels != null && !labels.isEmpty()) {
                    createIssue.setLabels(Stream.of(labels).map(LabelModel::getName).collect(Collectors.toCollection(ArrayList::new)));
                }
                if (users != null && !users.isEmpty()) {
                    createIssue.setAssignees(Stream.of(users).map(User::getLogin).collect(Collectors.toCollection(ArrayList::new)));
                }
                if (milestoneModel != null) {
                    createIssue.setMilestone((long) milestoneModel.getNumber());
                }
            }
            makeRestCall(RestProvider.getIssueService(isEnterprise()).createIssue(login, repo, createIssue), issueModel -> {
                if (issueModel != null) {
                    sendToView(view -> view.onSuccessSubmission(issueModel));
                } else {
                    sendToView(view -> view.showMessage(R.string.error, R.string.error_creating_issue));
                }
            }, false);
        } else {
            if (issue != null) {
                issue.setBody(InputHelper.toString(description));
                issue.setTitle(title);
                int number = issue.getNumber();
                if (isCollaborator) {
                    if (labels != null) {
                        LabelListModel labelModels = new LabelListModel();
                        labelModels.addAll(labels);
                        issue.setLabels(labelModels);
                    }
                    if (milestoneModel != null) {
                        issue.setMilestone(milestoneModel);
                    }
                    if (users != null) {
                        UsersListModel usersListModel = new UsersListModel();
                        usersListModel.addAll(users);
                        issue.setAssignees(usersListModel);
                    }
                }
                IssueRequestModel requestModel = IssueRequestModel.clone(issue, false);
                makeRestCall(RestProvider.getIssueService(isEnterprise()).editIssue(login, repo, number, requestModel), issueModel -> {
                    if (issueModel != null) {
                        sendToView(view -> view.onSuccessSubmission(issueModel));
                    } else {
                        sendToView(view -> view.showMessage(R.string.error, R.string.error_creating_issue));
                    }
                }, false);
            }
            if (pullRequestModel != null) {
                int number = pullRequestModel.getNumber();
                pullRequestModel.setBody(InputHelper.toString(description));
                pullRequestModel.setTitle(title);
                if (isCollaborator) {
                    if (labels != null) {
                        LabelListModel labelModels = new LabelListModel();
                        labelModels.addAll(labels);
                        pullRequestModel.setLabels(labelModels);
                    }
                    if (milestoneModel != null) {
                        pullRequestModel.setMilestone(milestoneModel);
                    }
                    if (users != null) {
                        UsersListModel usersListModel = new UsersListModel();
                        usersListModel.addAll(users);
                        pullRequestModel.setAssignees(usersListModel);
                    }
                }
                IssueRequestModel requestModel = IssueRequestModel.clone(pullRequestModel, false);
                makeRestCall(RestProvider.getPullRequestService(isEnterprise()).editPullRequest(login, repo, number, requestModel).flatMap(pullRequest1 -> RestProvider.getIssueService(isEnterprise()).getIssue(login, repo, number), (pullRequest1, issueReaction) -> {
                    // hack to get reactions from issue api
                    if (issueReaction != null) {
                        pullRequest1.setReactions(issueReaction.getReactions());
                    }
                    return pullRequest1;
                }), pr -> {
                    if (pr != null) {
                        sendToView(view -> view.onSuccessSubmission(pr));
                    } else {
                        sendToView(view -> view.showMessage(R.string.error, R.string.error_creating_issue));
                    }
                }, false);
            }
        }
    }
}
Also used : Stream(com.annimon.stream.Stream) IssueRequestModel(com.fastaccess.data.dao.IssueRequestModel) InputHelper(com.fastaccess.helper.InputHelper) Intent(android.content.Intent) NonNull(android.support.annotation.NonNull) Issue(com.fastaccess.data.dao.model.Issue) ArrayList(java.util.ArrayList) RxHelper(com.fastaccess.helper.RxHelper) MilestoneModel(com.fastaccess.data.dao.MilestoneModel) LabelListModel(com.fastaccess.data.dao.LabelListModel) User(com.fastaccess.data.dao.model.User) BasePresenter(com.fastaccess.ui.base.mvp.presenter.BasePresenter) BundleConstant(com.fastaccess.helper.BundleConstant) Login(com.fastaccess.data.dao.model.Login) RestProvider(com.fastaccess.provider.rest.RestProvider) Collectors(com.annimon.stream.Collectors) BaseMvp(com.fastaccess.ui.base.mvp.BaseMvp) CreateIssueModel(com.fastaccess.data.dao.CreateIssueModel) LabelModel(com.fastaccess.data.dao.LabelModel) PullRequest(com.fastaccess.data.dao.model.PullRequest) R(com.fastaccess.R) BuildConfig(com.fastaccess.BuildConfig) UsersListModel(com.fastaccess.data.dao.UsersListModel) Nullable(android.support.annotation.Nullable) Activity(android.app.Activity) User(com.fastaccess.data.dao.model.User) IssueRequestModel(com.fastaccess.data.dao.IssueRequestModel) UsersListModel(com.fastaccess.data.dao.UsersListModel) LabelModel(com.fastaccess.data.dao.LabelModel) LabelListModel(com.fastaccess.data.dao.LabelListModel) CreateIssueModel(com.fastaccess.data.dao.CreateIssueModel) ArrayList(java.util.ArrayList)

Aggregations

Activity (android.app.Activity)1 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 Collectors (com.annimon.stream.Collectors)1 Stream (com.annimon.stream.Stream)1 BuildConfig (com.fastaccess.BuildConfig)1 R (com.fastaccess.R)1 CreateIssueModel (com.fastaccess.data.dao.CreateIssueModel)1 IssueRequestModel (com.fastaccess.data.dao.IssueRequestModel)1 LabelListModel (com.fastaccess.data.dao.LabelListModel)1 LabelModel (com.fastaccess.data.dao.LabelModel)1 MilestoneModel (com.fastaccess.data.dao.MilestoneModel)1 UsersListModel (com.fastaccess.data.dao.UsersListModel)1 Issue (com.fastaccess.data.dao.model.Issue)1 Login (com.fastaccess.data.dao.model.Login)1 PullRequest (com.fastaccess.data.dao.model.PullRequest)1 User (com.fastaccess.data.dao.model.User)1 BundleConstant (com.fastaccess.helper.BundleConstant)1 InputHelper (com.fastaccess.helper.InputHelper)1