public ReadmeViewModel( ISessionService applicationService, IActionMenuFactory actionMenuService) { Title = "Readme"; var nonNullContentModel = this.WhenAnyValue(x => x.ContentModel).Select(x => x != null); ShareCommand = ReactiveCommand.Create(nonNullContentModel); ShareCommand.Subscribe(sender => actionMenuService.ShareUrl(sender, ContentModel.HtmlUrl)); GoToGitHubCommand = ReactiveCommand.Create(nonNullContentModel); GoToGitHubCommand.Select(_ => ContentModel.HtmlUrl).Subscribe(GoToWebBrowser); GoToLinkCommand = ReactiveCommand.Create(); GoToLinkCommand.OfType <string>().Subscribe(x => GoToWebBrowser(new Uri(x))); ShowMenuCommand = ReactiveCommand.CreateAsyncTask(nonNullContentModel, sender => { var menu = actionMenuService.Create(); menu.AddButton("Share", ShareCommand); menu.AddButton("Show in GitHub", GoToGitHubCommand); return(menu.Show(sender)); }); LoadCommand = ReactiveCommand.CreateAsyncTask(x => { var contentTask = applicationService.GitHubClient.Repository.Content.GetReadmeHtml(RepositoryOwner, RepositoryName) .ContinueWith(t => ContentText = t.Result, TaskScheduler.FromCurrentSynchronizationContext()); var modelTask = applicationService.GitHubClient.Repository.Content.GetReadme(RepositoryOwner, RepositoryName) .ContinueWith(t => ContentModel = t.Result, TaskScheduler.FromCurrentSynchronizationContext()); return(Task.WhenAll(contentTask, modelTask)); }); }
public ReadmeViewModel(IApplicationService applicationService, IShareService shareService, IActionMenuService actionMenuService) { Title = "Readme"; var nonNullContentModel = this.WhenAnyValue(x => x.ContentModel).Select(x => x != null); ShareCommand = ReactiveCommand.Create(nonNullContentModel); ShareCommand.Subscribe(_ => shareService.ShareUrl(ContentModel.HtmlUrl)); GoToGitHubCommand = ReactiveCommand.Create(nonNullContentModel); GoToGitHubCommand.Select(_ => ContentModel.HtmlUrl).Subscribe(this.ShowWebBrowser); GoToLinkCommand = ReactiveCommand.Create(); GoToLinkCommand.OfType <string>().Subscribe(this.ShowWebBrowser); ShowMenuCommand = ReactiveCommand.CreateAsyncTask(nonNullContentModel, _ => { var menu = actionMenuService.Create(Title); menu.AddButton("Share", ShareCommand); menu.AddButton("Show in GitHub", GoToGitHubCommand); return(menu.Show()); }); LoadCommand = ReactiveCommand.CreateAsyncTask(async x => { var repository = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName]; ContentText = await repository.GetReadmeRendered(); ContentModel = (await applicationService.Client.ExecuteAsync(repository.GetReadme())).Data; }); }
public ReleaseViewModel(IApplicationService applicationService, IShareService shareService, IUrlRouterService urlRouterService, IActionMenuService actionMenuService) { this.WhenAnyValue(x => x.ReleaseModel) .Select(x => x == null ? "Release" : x.Name).Subscribe(x => Title = x); ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null)); ShareCommand.Subscribe(_ => shareService.ShareUrl(ReleaseModel.HtmlUrl)); var gotoUrlCommand = this.CreateUrlCommand(); GoToGitHubCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null)); GoToGitHubCommand.Select(_ => ReleaseModel.HtmlUrl).Subscribe(gotoUrlCommand.ExecuteIfCan); GoToLinkCommand = ReactiveCommand.Create(); GoToLinkCommand.OfType <string>().Subscribe(x => { var handledViewModel = urlRouterService.Handle(x); if (handledViewModel != null && applicationService.Account.OpenUrlsInApp) { ShowViewModel(handledViewModel); } else { gotoUrlCommand.ExecuteIfCan(x); } }); ShowMenuCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null), _ => { var menu = actionMenuService.Create(Title); menu.AddButton("Share", ShowMenuCommand); menu.AddButton("Show in GitHub", GoToGitHubCommand); return(menu.Show()); }); _contentText = this.WhenAnyValue(x => x.ReleaseModel).IsNotNull() .Select(x => x.BodyHtml).ToProperty(this, x => x.ContentText); LoadCommand = ReactiveCommand.CreateAsyncTask(x => this.RequestModel(applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetRelease(ReleaseId), x as bool?, r => ReleaseModel = r.Data)); }
public GistViewModel( ISessionService sessionService, IActionMenuFactory actionMenuService, IAlertDialogFactory alertDialogFactory) { Comments = new ReactiveList <GistCommentModel>(); Title = "Gist"; this.WhenAnyValue(x => x.Gist).Where(x => x != null && x.Files != null && x.Files.Count > 0) .Select(x => x.Files.First().Key).Subscribe(x => Title = x); ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null)); ShareCommand.Subscribe(sender => actionMenuService.ShareUrl(sender, new Uri(Gist.HtmlUrl))); this.WhenAnyValue(x => x.Gist.Owner.AvatarUrl) .Select(x => new GitHubAvatar(x)) .ToProperty(this, x => x.Avatar, out _avatar); ToggleStarCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue), async t => { try { if (!IsStarred.HasValue) { return; } var request = IsStarred.Value ? sessionService.Client.Gists[Id].Unstar() : sessionService.Client.Gists[Id].Star(); await sessionService.Client.ExecuteAsync(request); IsStarred = !IsStarred.Value; } catch (Exception e) { throw new Exception("Unable to start gist. Please try again.", e); } }); ForkCommand = ReactiveCommand.CreateAsyncTask(async t => { var gist = await sessionService.GitHubClient.Gist.Fork(Id); var vm = this.CreateViewModel <GistViewModel>(); vm.Id = gist.Id; vm.Gist = gist; NavigateTo(vm); }); ForkCommand.IsExecuting.Subscribe(x => { if (x) { alertDialogFactory.Show("Forking..."); } else { alertDialogFactory.Hide(); } }); GoToEditCommand = ReactiveCommand.Create(); GoToEditCommand.Subscribe(_ => { var vm = this.CreateViewModel <GistEditViewModel>(); vm.Gist = Gist; vm.SaveCommand.Subscribe(x => Gist = x); NavigateTo(vm); }); GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null && !string.IsNullOrEmpty(x.HtmlUrl))); GoToHtmlUrlCommand .Select(_ => this.CreateViewModel <WebBrowserViewModel>()) .Select(x => x.Init(Gist.HtmlUrl)) .Subscribe(NavigateTo); GoToFileSourceCommand = ReactiveCommand.Create(); GoToFileSourceCommand.OfType <GistFile>().Subscribe(x => { var vm = this.CreateViewModel <GistFileViewModel>(); vm.Id = Id; vm.GistFile = x; vm.Filename = x.Filename; NavigateTo(vm); }); GoToOwnerCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist.Owner).Select(x => x != null)); GoToOwnerCommand .Select(_ => this.CreateViewModel <UserViewModel>()) .Select(x => x.Init(Gist.Owner.Login)) .Subscribe(NavigateTo); AddCommentCommand = ReactiveCommand.Create().WithSubscription(_ => NavigateTo(new ComposerViewModel("Add Comment", async x => { var request = sessionService.Client.Gists[Id].CreateGistComment(x); Comments.Add((await sessionService.Client.ExecuteAsync(request)).Data); }, alertDialogFactory))); ShowMenuCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.Gist).Select(x => x != null), sender => { var menu = actionMenuService.Create(); if (Gist.Owner != null && string.Equals(sessionService.Account.Username, Gist.Owner.Login, StringComparison.OrdinalIgnoreCase)) { menu.AddButton("Edit", GoToEditCommand); } else { menu.AddButton("Fork", ForkCommand); } menu.AddButton("Share", ShareCommand); menu.AddButton("Show in GitHub", GoToHtmlUrlCommand); return(menu.Show(sender)); }); LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => { sessionService.GitHubClient.Gist.IsStarred(Id).ToBackground(x => IsStarred = x); Comments.SimpleCollectionLoad(sessionService.Client.Gists[Id].GetComments()); Gist = await sessionService.GitHubClient.Gist.Get(Id); }); }
public IssueViewModel(IApplicationService applicationService, IShareService shareService) { _applicationService = applicationService; Comments = new ReactiveList <IssueCommentModel>(); Events = new ReactiveList <IssueEventModel>(); var issuePresenceObservable = this.WhenAnyValue(x => x.Issue).Select(x => x != null); ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue, x => x != null && !string.IsNullOrEmpty(x.HtmlUrl))); ShareCommand.Subscribe(_ => shareService.ShareUrl(Issue.HtmlUrl)); AddCommentCommand = ReactiveCommand.Create(); AddCommentCommand.Subscribe(_ => { var vm = CreateViewModel <CommentViewModel>(); ReactiveUI.Legacy.ReactiveCommandMixins.RegisterAsyncTask(vm.SaveCommand, async t => { var issue = _applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Issues[IssueId]; var comment = await _applicationService.Client.ExecuteAsync(issue.CreateComment(vm.Comment)); Comments.Add(comment.Data); vm.DismissCommand.ExecuteIfCan(); }); ShowViewModel(vm); }); ToggleStateCommand = ReactiveCommand.CreateAsyncTask(issuePresenceObservable, async t => { var close = string.Equals(Issue.State, "open", StringComparison.OrdinalIgnoreCase); try { var issue = _applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Issues[Issue.Number]; var data = await _applicationService.Client.ExecuteAsync(issue.UpdateState(close ? "closed" : "open")); Issue = data.Data; } catch (Exception e) { throw new Exception("Unable to " + (close ? "close" : "open") + " the item. " + e.Message, e); } }); GoToEditCommand = ReactiveCommand.Create(issuePresenceObservable); GoToEditCommand.Subscribe(_ => { var vm = CreateViewModel <IssueEditViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.Id = IssueId; vm.Issue = Issue; vm.WhenAnyValue(x => x.Issue).Skip(1).Subscribe(x => Issue = x); ShowViewModel(vm); }); GoToAssigneeCommand = ReactiveCommand.Create(issuePresenceObservable); GoToAssigneeCommand.Subscribe(_ => { var vm = CreateViewModel <IssueAssignedToViewModel>(); vm.SaveOnSelect = true; vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.IssueId = IssueId; vm.SelectedUser = Issue.Assignee; vm.WhenAnyValue(x => x.SelectedUser).Subscribe(x => { Issue.Assignee = x; this.RaisePropertyChanged("Issue"); }); ShowViewModel(vm); }); GoToLabelsCommand = ReactiveCommand.Create(issuePresenceObservable); GoToLabelsCommand.Subscribe(_ => { var vm = CreateViewModel <IssueLabelsViewModel>(); vm.SaveOnSelect = true; vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.IssueId = IssueId; vm.SelectedLabels.Reset(Issue.Labels); vm.WhenAnyValue(x => x.SelectedLabels).Subscribe(x => { Issue.Labels = x.ToList(); this.RaisePropertyChanged("Issue"); }); ShowViewModel(vm); }); GoToMilestoneCommand = ReactiveCommand.Create(issuePresenceObservable); GoToMilestoneCommand.Subscribe(_ => { var vm = CreateViewModel <IssueMilestonesViewModel>(); vm.SaveOnSelect = true; vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.IssueId = IssueId; vm.SelectedMilestone = Issue.Milestone; vm.WhenAnyValue(x => x.SelectedMilestone).Subscribe(x => { Issue.Milestone = x; this.RaisePropertyChanged("Issue"); }); ShowViewModel(vm); }); LoadCommand = ReactiveCommand.CreateAsyncTask(t => { var forceCacheInvalidation = t as bool?; var issue = _applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Issues[IssueId]; var t1 = this.RequestModel(issue.Get(), forceCacheInvalidation, response => Issue = response.Data); Comments.SimpleCollectionLoad(issue.GetComments(), forceCacheInvalidation).FireAndForget(); Events.SimpleCollectionLoad(issue.GetEvents(), forceCacheInvalidation).FireAndForget(); return(t1); }); }
public GistViewModel(IApplicationService applicationService, IShareService shareService, IActionMenuService actionMenuService, IStatusIndicatorService statusIndicatorService) { Comments = new ReactiveList <GistCommentModel>(); Title = "Gist"; GoToUrlCommand = this.CreateUrlCommand(); this.WhenAnyValue(x => x.Gist).Where(x => x != null && x.Files != null && x.Files.Count > 0) .Select(x => x.Files.First().Key).Subscribe(x => Title = x); ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null)); ShareCommand.Subscribe(_ => shareService.ShareUrl(Gist.HtmlUrl)); ToggleStarCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue), async t => { try { if (!IsStarred.HasValue) { return; } var request = IsStarred.Value ? applicationService.Client.Gists[Id].Unstar() : applicationService.Client.Gists[Id].Star(); await applicationService.Client.ExecuteAsync(request); IsStarred = !IsStarred.Value; } catch (Exception e) { throw new Exception("Unable to start gist. Please try again.", e); } }); ForkCommand = ReactiveCommand.CreateAsyncTask(async t => { var data = await applicationService.Client.ExecuteAsync(applicationService.Client.Gists[Id].ForkGist()); var forkedGist = data.Data; var vm = CreateViewModel <GistViewModel>(); vm.Id = forkedGist.Id; vm.Gist = forkedGist; ShowViewModel(vm); }); ForkCommand.IsExecuting.Subscribe(x => { if (x) { statusIndicatorService.Show("Forking..."); } else { statusIndicatorService.Hide(); } }); GoToEditCommand = ReactiveCommand.Create(); GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null && !string.IsNullOrEmpty(x.HtmlUrl))); GoToHtmlUrlCommand.Select(_ => Gist.HtmlUrl).Subscribe(this.ShowWebBrowser); GoToFileSourceCommand = ReactiveCommand.Create(); GoToFileSourceCommand.OfType <GistFileModel>().Subscribe(x => { var vm = CreateViewModel <GistFileViewModel>(); vm.Id = Id; vm.GistFile = x; vm.Filename = x.Filename; ShowViewModel(vm); }); GoToUserCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null && x.Owner != null)); GoToUserCommand.Subscribe(x => { var vm = CreateViewModel <UserViewModel>(); vm.Username = Gist.Owner.Login; ShowViewModel(vm); }); AddCommentCommand = ReactiveCommand.Create().WithSubscription(_ => { var vm = CreateViewModel <GistCommentViewModel>(); vm.Id = Id; vm.CommentAdded.Subscribe(Comments.Add); ShowViewModel(vm); }); ShowMenuCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.Gist).Select(x => x != null), _ => { var menu = actionMenuService.Create(Title); if (Gist.Owner != null && string.Equals(applicationService.Account.Username, Gist.Owner.Login, StringComparison.OrdinalIgnoreCase)) { menu.AddButton("Edit", GoToEditCommand); } else { menu.AddButton("Fork", ForkCommand); } menu.AddButton("Share", ShareCommand); menu.AddButton("Show in GitHub", GoToHtmlUrlCommand); return(menu.Show()); }); LoadCommand = ReactiveCommand.CreateAsyncTask(t => { var forceCacheInvalidation = t as bool?; var t1 = this.RequestModel(applicationService.Client.Gists[Id].Get(), forceCacheInvalidation, response => Gist = response.Data); this.RequestModel(applicationService.Client.Gists[Id].IsGistStarred(), forceCacheInvalidation, response => IsStarred = response.Data).FireAndForget(); Comments.SimpleCollectionLoad(applicationService.Client.Gists[Id].GetComments(), forceCacheInvalidation).FireAndForget(); return(t1); }); }
public PullRequestViewModel(IApplicationService applicationService, IMarkdownService markdownService, IShareService shareService) { _applicationService = applicationService; _markdownService = markdownService; Comments = new ReactiveList <IssueCommentModel>(); Events = new ReactiveList <IssueEventModel>(); MergeCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.PullRequest).Select(x => x != null && x.Merged.HasValue && !x.Merged.Value && x.Mergable.HasValue && x.Mergable.Value), async t => { try { var response = await _applicationService.Client.ExecuteAsync(_applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].PullRequests[PullRequestId].Merge()); if (!response.Data.Merged) { throw new Exception(response.Data.Message); } var pullRequest = _applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].PullRequests[PullRequestId].Get(); await this.RequestModel(pullRequest, true, r => PullRequest = r.Data); } catch (Exception e) { throw new Exception("Unable to Merge: " + e.Message, e); } }); ToggleStateCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.PullRequest).Select(x => x != null), async t => { var close = string.Equals(PullRequest.State, "open", StringComparison.OrdinalIgnoreCase); try { var data = await _applicationService.Client.ExecuteAsync( _applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].PullRequests[PullRequestId].UpdateState(close ? "closed" : "open")); PullRequest = data.Data; } catch (Exception e) { throw new Exception("Unable to " + (close ? "close" : "open") + " the item. " + e.Message, e); } }); GoToCommitsCommand = ReactiveCommand.Create(); GoToCommitsCommand.Subscribe(_ => { var vm = CreateViewModel <PullRequestCommitsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.PullRequestId = PullRequestId; ShowViewModel(vm); }); GoToFilesCommand = ReactiveCommand.Create(); GoToFilesCommand.Subscribe(_ => { var vm = CreateViewModel <PullRequestFilesViewModel>(); vm.Username = RepositoryOwner; vm.Repository = RepositoryName; vm.PullRequestId = PullRequestId; ShowViewModel(vm); }); ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.PullRequest).Select(x => x != null && !string.IsNullOrEmpty(x.HtmlUrl))); ShareCommand.Subscribe(_ => shareService.ShareUrl(PullRequest.HtmlUrl)); GoToEditCommand = ReactiveCommand.Create(); GoToEditCommand.Subscribe(_ => { var vm = CreateViewModel <IssueEditViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.Id = PullRequestId; vm.Issue = Issue; vm.WhenAnyValue(x => x.Issue).Skip(1).Subscribe(x => Issue = x); ShowViewModel(vm); }); GoToLabelsCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)); GoToLabelsCommand.Subscribe(_ => { var vm = CreateViewModel <IssueLabelsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.IssueId = PullRequestId; vm.SaveOnSelect = true; vm.SelectedLabels.Reset(Issue.Labels); vm.WhenAnyValue(x => x.Labels).Skip(1).Subscribe(x => { Issue.Labels = x.ToList(); this.RaisePropertyChanged("Issue"); }); ShowViewModel(vm); }); GoToMilestoneCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)); GoToMilestoneCommand.Subscribe(_ => { var vm = CreateViewModel <IssueMilestonesViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.IssueId = PullRequestId; vm.SaveOnSelect = true; vm.SelectedMilestone = Issue.Milestone; vm.WhenAnyValue(x => x.SelectedMilestone).Skip(1).Subscribe(x => { Issue.Milestone = x; this.RaisePropertyChanged("Issue"); }); ShowViewModel(vm); }); GoToAssigneeCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)); GoToAssigneeCommand.Subscribe(_ => { var vm = CreateViewModel <IssueAssignedToViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.IssueId = PullRequestId; vm.SaveOnSelect = true; vm.SelectedUser = Issue.Assignee; vm.WhenAnyValue(x => x.SelectedUser).Skip(1).Subscribe(x => { Issue.Assignee = x; this.RaisePropertyChanged("Issue"); }); ShowViewModel(vm); }); GoToAddCommentCommand = ReactiveCommand.Create(); GoToAddCommentCommand.Subscribe(_ => { var vm = CreateViewModel <CommentViewModel>(); ReactiveUI.Legacy.ReactiveCommandMixins.RegisterAsyncTask(vm.SaveCommand, async t => { var req = _applicationService.Client.Users[RepositoryOwner] .Repositories[RepositoryName].Issues[PullRequestId].CreateComment(vm.Comment); var comment = await _applicationService.Client.ExecuteAsync(req); Comments.Add(comment.Data); vm.DismissCommand.ExecuteIfCan(); }); }); LoadCommand = ReactiveCommand.CreateAsyncTask(t => { var forceCacheInvalidation = t as bool?; var pullRequest = _applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].PullRequests[PullRequestId].Get(); var t1 = this.RequestModel(pullRequest, forceCacheInvalidation, response => PullRequest = response.Data); Events.SimpleCollectionLoad(_applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Issues[PullRequestId].GetEvents(), forceCacheInvalidation).FireAndForget(); Comments.SimpleCollectionLoad(_applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Issues[PullRequestId].GetComments(), forceCacheInvalidation).FireAndForget(); this.RequestModel(_applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Issues[PullRequestId].Get(), forceCacheInvalidation, response => Issue = response.Data).FireAndForget(); return(t1); }); }
public IssueViewModel(IApplicationService applicationService, IActionMenuFactory actionMenuFactory, IMarkdownService markdownService, IGraphicService graphicsService) { _applicationService = applicationService; var issuePresenceObservable = this.WhenAnyValue(x => x.Issue).Select(x => x != null); GoToAssigneesCommand = ReactiveCommand.Create(issuePresenceObservable) .WithSubscription(_ => Assignees.LoadCommand.ExecuteIfCan()); GoToLabelsCommand = ReactiveCommand.Create(issuePresenceObservable) .WithSubscription(_ => Labels.LoadCommand.ExecuteIfCan()); GoToMilestonesCommand = ReactiveCommand.Create(issuePresenceObservable) .WithSubscription(_ => Milestones.LoadCommand.ExecuteIfCan()); this.WhenAnyValue(x => x.Id) .Subscribe(x => Title = "Issue #" + x); _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); _markdownDescription = this.WhenAnyValue(x => x.Issue) .Select(x => ((x == null || string.IsNullOrEmpty(x.Body)) ? null : markdownService.Convert(x.Body))) .ToProperty(this, x => x.MarkdownDescription); ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)); ShareCommand.Subscribe(_ => actionMenuFactory.ShareUrl(Issue.HtmlUrl)); var events = new ReactiveList <IIssueEventItemViewModel>(); Events = events.CreateDerivedCollection(x => x); AddCommentCommand = ReactiveCommand.Create(); AddCommentCommand.Subscribe(_ => { var vm = this.CreateViewModel <IssueCommentViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.Id = Id; vm.SaveCommand.Subscribe(x => events.Add(new IssueCommentItemViewModel(x))); 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); } }); GoToEditCommand = ReactiveCommand.Create(issuePresenceObservable); GoToEditCommand.Subscribe(_ => { var vm = this.CreateViewModel <IssueEditViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.Id = Id; // vm.Issue = Issue; // vm.WhenAnyValue(x => x.Issue).Skip(1).Subscribe(x => Issue = x); NavigateTo(vm); }); Assignees = new IssueAssigneeViewModel( () => applicationService.GitHubClient.Issue.Assignee.GetForRepository(RepositoryOwner, RepositoryName), () => Task.FromResult(Issue), UpdateIssue); Milestones = new IssueMilestonesViewModel( () => applicationService.GitHubClient.Issue.Milestone.GetForRepository(RepositoryOwner, RepositoryName), () => Task.FromResult(Issue), UpdateIssue); Labels = new IssueLabelsViewModel( () => applicationService.GitHubClient.Issue.Labels.GetForRepository(RepositoryOwner, RepositoryName), () => Task.FromResult(Issue), UpdateIssue, graphicsService); LoadCommand = ReactiveCommand.CreateAsyncTask(async t => { var issueRequest = applicationService.GitHubClient.Issue.Get(RepositoryOwner, RepositoryName, Id) .ContinueWith(x => Issue = x.Result, new CancellationToken(), TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext()); var eventsRequest = applicationService.GitHubClient.Issue.Events.GetForIssue(RepositoryOwner, RepositoryName, Id); var commentsRequest = applicationService.GitHubClient.Issue.Comment.GetForIssue(RepositoryOwner, RepositoryName, Id); await Task.WhenAll(issueRequest, eventsRequest, commentsRequest); var tempList = new List <IIssueEventItemViewModel>(eventsRequest.Result.Count + commentsRequest.Result.Count); tempList.AddRange(eventsRequest.Result.Select(x => new IssueEventItemViewModel(x))); tempList.AddRange(commentsRequest.Result.Select(x => new IssueCommentItemViewModel(x))); events.Reset(tempList.OrderBy(x => x.CreatedAt)); }); ShowMenuCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.Issue).Select(x => x != null), _ => { var menu = actionMenuFactory.Create(Title); menu.AddButton(Issue.State == Octokit.ItemState.Open ? "Close" : "Open", ToggleStateCommand); // // // var editButton = _actionSheet.AddButton("Edit"); // var commentButton = _actionSheet.AddButton("Comment"); // var shareButton = _actionSheet.AddButton("Share"); // var showButton = _actionSheet.AddButton("Show in GitHub"); return(menu.Show()); }); }
public PullRequestViewModel( IApplicationService applicationService, IMarkdownService markdownService, IActionMenuFactory actionMenuService) : base(applicationService, markdownService) { this.WhenAnyValue(x => x.Id) .Subscribe(x => Title = "Pull Request #" + x); _canMerge = this.WhenAnyValue(x => x.PullRequest) .Select(x => x != null && !x.Merged) .ToProperty(this, x => x.CanMerge); var canMergeObservable = this.WhenAnyValue(x => x.PullRequest).Select(x => x != null && !x.Merged && x.Mergeable.HasValue && x.Mergeable.Value); MergeCommand = ReactiveCommand.CreateAsyncTask(canMergeObservable, async t => { var req = new Octokit.MergePullRequest(null); var response = await applicationService.GitHubClient.PullRequest.Merge(RepositoryOwner, RepositoryName, Id, req); if (!response.Merged) { throw new Exception(string.Format("Unable to merge pull request: {0}", response.Message)); } LoadCommand.ExecuteIfCan(); }); // ToggleStateCommand = ReactiveCommand.CreateAsyncTask( // this.WhenAnyValue(x => x.PullRequest).Select(x => x != null), // async t => // { // var newState = PullRequest.State == Octokit.ItemState.Open ? Octokit.ItemState.Closed : Octokit.ItemState.Open; // // try // { // var req = new Octokit.PullRequestUpdate { State = newState }; // PullRequest = await applicationService.GitHubClient.PullRequest.Update(RepositoryOwner, RepositoryName, Id, req); // } // catch (Exception e) // { // throw new Exception("Unable to " + (newState == Octokit.ItemState.Closed ? "close" : "open") + " the item. " + e.Message, e); // } // }); GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.PullRequest).Select(x => x != null)); GoToHtmlUrlCommand.Select(_ => PullRequest.HtmlUrl).Subscribe(GoToUrlCommand.ExecuteIfCan); GoToCommitsCommand = ReactiveCommand.Create().WithSubscription(_ => { var vm = this.CreateViewModel <PullRequestCommitsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.PullRequestId = Id; NavigateTo(vm); }); GoToFilesCommand = ReactiveCommand.Create().WithSubscription(_ => { var vm = this.CreateViewModel <PullRequestFilesViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.PullRequestId = Id; NavigateTo(vm); }); ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.PullRequest).Select(x => x != null)); ShareCommand.Subscribe(_ => actionMenuService.ShareUrl(PullRequest.HtmlUrl)); GoToEditCommand = ReactiveCommand.Create().WithSubscription(_ => { var vm = this.CreateViewModel <IssueEditViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.Id = Id; //vm.Issue = Issue; // vm.WhenAnyValue(x => x.Issue).Skip(1).Subscribe(x => Issue = x); NavigateTo(vm); }); ShowMenuCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.PullRequest).Select(x => x != null), _ => { var menu = actionMenuService.Create(Title); menu.AddButton("Edit", GoToEditCommand); menu.AddButton(PullRequest.State == Octokit.ItemState.Closed ? "Open" : "Close", ToggleStateCommand); menu.AddButton("Comment", GoToAddCommentCommand); menu.AddButton("Share", ShareCommand); menu.AddButton("Show in GitHub", GoToHtmlUrlCommand); return(menu.Show()); }); }
protected BaseIssueViewModel( ISessionService applicationService, IMarkdownService markdownService, IActionMenuFactory actionMenuFactory, IAlertDialogFactory alertDialogFactory) { _applicationService = applicationService; _markdownService = markdownService; _alertDialogFactory = alertDialogFactory; _assigneesCache = new Lazy <Task <IReadOnlyList <Octokit.User> > >(() => _applicationService.GitHubClient.Issue.Assignee.GetAllForRepository(RepositoryOwner, RepositoryName)); _milestonesCache = new Lazy <Task <IReadOnlyList <Octokit.Milestone> > >(() => _applicationService.GitHubClient.Issue.Milestone.GetAllForRepository(RepositoryOwner, RepositoryName)); _labelsCache = new Lazy <Task <IReadOnlyList <Octokit.Label> > >(() => _applicationService.GitHubClient.Issue.Labels.GetAllForRepository(RepositoryOwner, RepositoryName)); IssueUpdated.Subscribe(x => Issue = x); _avatar = this.WhenAnyValue(x => x.Issue.User.AvatarUrl) .Select(x => new GitHubAvatar(x)) .ToProperty(this, x => x.Avatar); var issuePresenceObservable = this.WhenAnyValue(x => x.Issue, x => x.CanModify) .Select(x => x.Item1 != null && x.Item2); Events = InternalEvents.CreateDerivedCollection(x => x); this.WhenAnyValue(x => x.Issue.Comments) .ToProperty(this, x => x.CommentCount, out _commentsCount); _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); GoToLabelsCommand = ReactiveCommand.Create(issuePresenceObservable); GoToMilestonesCommand = ReactiveCommand.Create(issuePresenceObservable); _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); _markdownDescription = this.WhenAnyValue(x => x.Issue) .Select(x => ((x == null || string.IsNullOrEmpty(x.Body)) ? null : x.Body)) .Where(x => x != null) .Select(x => GetMarkdownDescription().ToObservable()) .Switch() .ToProperty(this, x => x.MarkdownDescription, null, RxApp.MainThreadScheduler); LoadCommand = ReactiveCommand.CreateAsyncTask(t => Load(applicationService)); GoToOwnerCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)); GoToOwnerCommand .Select(_ => this.CreateViewModel <UserViewModel>()) .Select(x => x.Init(Issue.User.Login)) .Subscribe(NavigateTo); ToggleStateCommand = ReactiveCommand.CreateAsyncTask(issuePresenceObservable, async t => { try { var updatedIssue = await applicationService.GitHubClient.Issue.Update(RepositoryOwner, RepositoryName, Id, new Octokit.IssueUpdate { State = (Issue.State == Octokit.ItemState.Open) ? Octokit.ItemState.Closed : Octokit.ItemState.Open }); _issueUpdatedObservable.OnNext(updatedIssue); } catch (Exception e) { var close = (Issue.State == Octokit.ItemState.Open) ? "close" : "open"; throw new Exception("Unable to " + close + " the item. " + e.Message, e); } RetrieveEvents().ToBackground(x => InternalEvents.Reset(x)); }); AddCommentCommand = ReactiveCommand.Create().WithSubscription(_ => { var vm = new ComposerViewModel(async s => { var request = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Issues[Id].CreateComment(s); var comment = (await applicationService.Client.ExecuteAsync(request)).Data; InternalEvents.Add(new IssueCommentItemViewModel(comment)); _commentAddedSubject.OnNext(comment); }, alertDialogFactory); NavigateTo(vm); }); ShowMenuCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.Issue).Select(x => x != null), sender => { var menu = actionMenuFactory.Create(); menu.AddButton("Edit", GoToEditCommand); menu.AddButton(Issue.State == Octokit.ItemState.Closed ? "Open" : "Close", ToggleStateCommand); menu.AddButton("Comment", AddCommentCommand); menu.AddButton("Share", ShareCommand); menu.AddButton("Show in GitHub", GoToHtmlUrlCommand); return(menu.Show(sender)); }); GoToEditCommand = ReactiveCommand.Create().WithSubscription(_ => { var vm = this.CreateViewModel <IssueEditViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.Id = Id; vm.Issue = Issue; vm.SaveCommand.Subscribe(_issueUpdatedObservable.OnNext); NavigateTo(vm); }); GoToUrlCommand = ReactiveCommand.Create(); GoToUrlCommand.OfType <string>().Subscribe(GoToUrl); GoToUrlCommand.OfType <Uri>().Subscribe(x => GoToUrl(x.AbsoluteUri)); var hasHtmlObservable = this.WhenAnyValue(x => x.HtmlUrl).Select(x => x != null); ShareCommand = ReactiveCommand.Create(hasHtmlObservable); ShareCommand.Subscribe(sender => actionMenuFactory.ShareUrl(sender, HtmlUrl)); GoToHtmlUrlCommand = ReactiveCommand.Create(hasHtmlObservable); GoToHtmlUrlCommand.Subscribe(_ => GoToUrl(HtmlUrl.AbsoluteUri)); }