protected override async Task Save() { try { var labels = AssignedLabels.With(x => x.Select(y => y.Name).ToArray()); var milestone = AssignedMilestone.With(x => (int?)x.Number); var user = AssignedUser.With(x => x.Login); var request = _applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Issues .Create(Subject, Content, user, milestone, labels); var data = await _applicationService.Client.ExecuteAsync(request); _createdIssueSubject.OnNext(data.Data); } catch (Exception e) { throw new Exception("Unable to save new issue! Please try again.", e); } }
protected BaseIssueViewModel( IApplicationService applicationService, IMarkdownService markdownService) { _applicationService = applicationService; var issuePresenceObservable = this.WhenAnyValue(x => x.Issue, x => x.CanModify) .Select(x => x.Item1 != null && x.Item2); Events = InternalEvents.CreateDerivedCollection(x => x); _participants = Events.Changed .Select(_ => Events.Select(y => y.Actor).Distinct().Count()) .Select(x => x == 0 ? 1 : x) .ToProperty(this, x => x.Participants); GoToAssigneesCommand = ReactiveCommand.Create(issuePresenceObservable) .WithSubscription(_ => Assignees.LoadCommand.ExecuteIfCan()); GoToLabelsCommand = ReactiveCommand.Create(issuePresenceObservable) .WithSubscription(_ => Labels.LoadCommand.ExecuteIfCan()); GoToMilestonesCommand = ReactiveCommand.Create(issuePresenceObservable) .WithSubscription(_ => Milestones.LoadCommand.ExecuteIfCan()); _assignedUser = this.WhenAnyValue(x => x.Issue.Assignee) .ToProperty(this, x => x.AssignedUser); _assignedMilestone = this.WhenAnyValue(x => x.Issue.Milestone) .ToProperty(this, x => x.AssignedMilestone); _assignedLabels = this.WhenAnyValue(x => x.Issue.Labels) .ToProperty(this, x => x.AssignedLabels); _isClosed = this.WhenAnyValue(x => x.Issue.State) .Select(x => x == Octokit.ItemState.Closed) .ToProperty(this, x => x.IsClosed); Assignees = new IssueAssigneeViewModel( () => applicationService.GitHubClient.Issue.Assignee.GetForRepository(RepositoryOwner, RepositoryName), () => Task.FromResult(Issue.Assignee), x => UpdateIssue(new Octokit.IssueUpdate { Assignee = x.With(y => y.Login), Milestone = AssignedMilestone.With(y => (int?)y.Number) })); Milestones = new IssueMilestonesViewModel( () => applicationService.GitHubClient.Issue.Milestone.GetForRepository(RepositoryOwner, RepositoryName), () => Task.FromResult(Issue.Milestone), x => UpdateIssue(new Octokit.IssueUpdate { Assignee = AssignedUser.With(y => y.Login), Milestone = x.With(y => (int?)y.Number) })); Labels = new IssueLabelsViewModel( () => applicationService.GitHubClient.Issue.Labels.GetForRepository(RepositoryOwner, RepositoryName), () => Task.FromResult((IReadOnlyList <Octokit.Label>) new ReadOnlyCollection <Octokit.Label>(Issue.Labels.ToList())), x => { var update = new Octokit.IssueUpdate { Assignee = AssignedUser.With(y => y.Login), Milestone = AssignedMilestone.With(y => (int?)y.Number), }; foreach (var label in x.Select(y => y.Name)) { update.AddLabel(label); } return(UpdateIssue(update)); }); _markdownDescription = this.WhenAnyValue(x => x.Issue) .Select(x => ((x == null || string.IsNullOrEmpty(x.Body)) ? null : markdownService.Convert(x.Body))) .ToProperty(this, x => x.MarkdownDescription); LoadCommand = ReactiveCommand.CreateAsyncTask(t => Load(applicationService)); GoToOwnerCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)); GoToOwnerCommand.Select(_ => Issue.User).Subscribe(x => { var vm = this.CreateViewModel <UserViewModel>(); vm.Username = x.Login; NavigateTo(vm); }); ToggleStateCommand = ReactiveCommand.CreateAsyncTask(issuePresenceObservable, async t => { try { Issue = await applicationService.GitHubClient.Issue.Update(RepositoryOwner, RepositoryName, Id, new Octokit.IssueUpdate { State = (Issue.State == Octokit.ItemState.Open) ? Octokit.ItemState.Closed : Octokit.ItemState.Open }); } catch (Exception e) { var close = (Issue.State == Octokit.ItemState.Open) ? "close" : "open"; throw new Exception("Unable to " + close + " the item. " + e.Message, e); } }); AddCommentCommand = ReactiveCommand.Create() .WithSubscription(_ => { var vm = this.CreateViewModel <IssueCommentViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.Id = Id; vm.SaveCommand.Subscribe(x => InternalEvents.Add(new IssueCommentItemViewModel(x))); NavigateTo(vm); }); GoToUrlCommand = ReactiveCommand.Create(); GoToUrlCommand.OfType <string>().Subscribe(x => { var vm = this.CreateViewModel <WebBrowserViewModel>(); vm.Url = x; NavigateTo(vm); }); GoToUrlCommand.OfType <Uri>().Subscribe(x => { var vm = this.CreateViewModel <WebBrowserViewModel>(); vm.Url = x.AbsoluteUri; NavigateTo(vm); }); }