Ejemplo n.º 1
0
        public IssuesViewModel(ISessionService sessionService, IActionMenuFactory actionMenuFactory)
            : base(sessionService)
	    {
            _sessionService = sessionService;
            Filter = new RepositoryIssuesFilterViewModel(sessionService, actionMenuFactory);

            Title = "Issues";

            GoToNewIssueCommand = ReactiveCommand.Create();
	        GoToNewIssueCommand.Subscribe(_ => {
	            var vm = this.CreateViewModel<IssueAddViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
	            vm.RepositoryName = RepositoryName;
                vm.SaveCommand.Subscribe(x => LoadCommand.ExecuteIfCan());
                NavigateTo(vm);
	        });

            Filter.SaveCommand.Subscribe(_ => {
                IssuesBacking.Clear();
                LoadCommand.ExecuteIfCan();
            });

            GoToCustomFilterCommand = ReactiveCommand.Create();
            GoToCustomFilterCommand.Subscribe(_ => FilterSelection = IssueFilterSelection.Custom);
	    }
Ejemplo n.º 2
0
        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);
            });
        }
Ejemplo n.º 3
0
        public OAuthFlowLoginViewModel(
            IAccountsRepository accountsRepository,
            IActionMenuFactory actionMenuService,
            IAlertDialogFactory alertDialogService)
        {
            _accountsRepository = accountsRepository;
            _alertDialogService = alertDialogService;

            Title = "Login";

            var oauthLogin = ReactiveCommand.Create().WithSubscription(_ =>
                NavigateTo(this.CreateViewModel<OAuthTokenLoginViewModel>()));

            var canLogin = this.WhenAnyValue(x => x.Code).Select(x => !string.IsNullOrEmpty(x));
            var loginCommand = ReactiveCommand.CreateAsyncTask(canLogin,_ => Login(Code));
            loginCommand.Subscribe(x => MessageBus.Current.SendMessage(new LogoutMessage()));
            LoginCommand = loginCommand;

            ShowLoginOptionsCommand = ReactiveCommand.CreateAsyncTask(sender =>
            {
                var actionMenu = actionMenuService.Create();
                actionMenu.AddButton("Login via Token", oauthLogin);
                return actionMenu.Show(sender);
            });

            _loginUrl = this.WhenAnyValue(x => x.WebDomain)
                .IsNotNull()
                .Select(x => x.TrimEnd('/'))
                .Select(x => 
                    string.Format(x + "/login/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}", 
                    ClientId, Uri.EscapeDataString(RedirectUri), Uri.EscapeDataString(string.Join(",", OctokitClientFactory.Scopes))))
                .ToProperty(this, x => x.LoginUrl);

            WebDomain = DefaultWebDomain;
        }
Ejemplo n.º 4
0
        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));
            });
        }
Ejemplo n.º 5
0
        public GistFileViewModel(IAccountsService accounts, IApplicationService applicationService,
                                 IFilesystemService filesystemService, IActionMenuFactory actionMenuService)
            : base(accounts)
        {
            this.WhenAnyValue(x => x.Filename).Subscribe(x =>
            {
                Title = x == null ? "Gist" : x.Substring(x.LastIndexOf('/') + 1);
            });

            _isMarkdown = this.WhenAnyValue(x => x.GistFile).IsNotNull().Select(x =>
                                                                                string.Equals(x.Language, MarkdownLanguage, StringComparison.OrdinalIgnoreCase)).ToProperty(this, x => x.IsMarkdown);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.SourceItem).Select(x => x != null),
                _ =>
            {
                var menu = actionMenuService.Create(Title);
                menu.AddButton("Open With", OpenWithCommand);
                return(menu.Show());
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                if (GistFile == null)
                {
                    var data = await applicationService.Client.ExecuteAsync(applicationService.Client.Gists[_id].Get());
                    GistFile = data.Data.Files[_filename];
                }

                //Check to make sure...
                if (GistFile == null || GistFile.Content == null)
                {
                    throw new Exception("Unable to retreive gist!");
                }

                var content = GistFile.Content;
                if (MarkdownLanguage.Equals(GistFile.Language, StringComparison.OrdinalIgnoreCase))
                {
                    content = await applicationService.Client.Markdown.GetMarkdown(content);
                }

                var gistFileName = System.IO.Path.GetFileName(GistFile.Filename);
                string filePath;

                using (var stream = filesystemService.CreateTempFile(out filePath, gistFileName))
                {
                    using (var fs = new System.IO.StreamWriter(stream))
                    {
                        fs.Write(content);
                    }
                }

                var fileUri = new Uri(filePath);
                SourceItem  = new FileSourceItemViewModel(fileUri, false);
            });
        }
Ejemplo n.º 6
0
        public PullRequestViewModel(
            ISessionService applicationService,
            IMarkdownService markdownService,
            IActionMenuFactory actionMenuService,
            IAlertDialogFactory alertDialogFactory)
            : base(applicationService, markdownService, actionMenuService, alertDialogFactory)
        {
            this.WhenAnyValue(x => x.Id)
            .Subscribe(x => Title = "Pull Request #" + x);

            this.WhenAnyValue(x => x.PullRequest.HtmlUrl)
            .ToProperty(this, x => x.HtmlUrl, out _htmlUrl);

            var canMergeObservable = this.WhenAnyValue(x => x.PullRequest)
                                     .Select(x => x != null && !x.Merged && x.Mergeable.HasValue && x.Mergeable.Value);

            _canMerge = canMergeObservable.CombineLatest(
                this.WhenAnyValue(x => x.PushAccess), (x, y) => x && y)
                        .ToProperty(this, x => x.CanMerge);

            _commentsCount = this.WhenAnyValue(x => x.Issue.Comments, x => x.Comments.Count, (x, y) => x + y)
                             .ToProperty(this, x => x.CommentCount);

            MergeCommand = ReactiveCommand.CreateAsyncTask(canMergeObservable, async t => {
                using (alertDialogFactory.Activate("Merging..."))
                {
                    var req = new MergePullRequest {
                        Message = MergeComment
                    };
                    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));
                    }
                    await LoadCommand.ExecuteAsync();
                }
            });

            var canGoToCommits = this.WhenAnyValue(x => x.PullRequest.Commits).Select(x => x > 0);

            GoToCommitsCommand = ReactiveCommand.Create(canGoToCommits);
            GoToCommitsCommand
            .Select(x => this.CreateViewModel <PullRequestCommitsViewModel>())
            .Select(x => x.Init(RepositoryOwner, RepositoryName, Id))
            .Subscribe(NavigateTo);

            var canGoToFiles = this.WhenAnyValue(x => x.PullRequest.ChangedFiles).Select(x => x > 0);

            GoToFilesCommand = ReactiveCommand.Create(canGoToFiles);
            GoToFilesCommand
            .Select(x => this.CreateViewModel <PullRequestFilesViewModel>())
            .Select(x => x.Init(RepositoryOwner, RepositoryName, Id, PullRequest.Head.Sha))
            .Do(x => x.CommentCreated.Subscribe(AddComment))
            .Subscribe(NavigateTo);
        }
Ejemplo n.º 7
0
        public GistFileViewModel(ISessionService sessionService, ISessionService applicationService, 
            IFilesystemService filesystemService, IActionMenuFactory actionMenuService, IMarkdownService markdownService)
            : base(sessionService)
	    {
	        this.WhenAnyValue(x => x.Filename)
                .Select(x => x == null ? "Gist" : x.Substring(x.LastIndexOf('/') + 1))
                .Subscribe(x => Title = x);
                
            _isMarkdown = this.WhenAnyValue(x => x.GistFile)
                .IsNotNull()
                .Select(x => string.Equals(x.Language, MarkdownLanguage, StringComparison.OrdinalIgnoreCase))
                .ToProperty(this, x => x.IsMarkdown);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.SourceItem).Select(x => x != null),
                sender => {
                    var menu = actionMenuService.Create();
                    menu.AddButton("Open With", OpenWithCommand);
                    return menu.Show(sender);
                });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                if (GistFile == null)
			    {
                    var data = await applicationService.Client.ExecuteAsync(applicationService.Client.Gists[_id].Get());
                    GistFile = data.Data.Files[_filename];
			    }

			    //Check to make sure...
                if (GistFile == null || GistFile.Content == null)
			    {
				    throw new Exception("Unable to retreive gist!");
			    }

                var content = GistFile.Content;
                if (MarkdownLanguage.Equals(GistFile.Language, StringComparison.OrdinalIgnoreCase))
                    content = await applicationService.Client.Markdown.GetMarkdown(content);

                var gistFileName = System.IO.Path.GetFileName(GistFile.Filename).Trim();
                string filePath;
                
                using (var stream = filesystemService.CreateTempFile(out filePath, gistFileName))
                {
                    using (var fs = new System.IO.StreamWriter(stream))
                    {
                        fs.Write(content);
                    }
                }

                var fileUri = new Uri(filePath);
                SourceItem = new FileSourceItemViewModel(fileUri, false);
            });
	    }
Ejemplo n.º 8
0
        public ReleaseViewModel(ISessionService applicationService,
            IUrlRouterService urlRouterService, IActionMenuFactory actionMenuService)
        {
            Title = "Release";

            this.WhenAnyValue(x => x.ReleaseModel)
                .Select(x => 
                {
                    if (x == null) return "Release";
                    var name = string.IsNullOrEmpty(x.Name) ? x.TagName : x.Name;
                    return name ?? "Release";
                })
                .Subscribe(x => Title = x);

            var shareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null));
            shareCommand.Subscribe(sender => actionMenuService.ShareUrl(sender, ReleaseModel.HtmlUrl));

            var gotoUrlCommand = new Action<string>(x =>
            {
                var vm = this.CreateViewModel<WebBrowserViewModel>();
                vm.Init(x);
                NavigateTo(vm);
            });

            var gotoGitHubCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null));
            gotoGitHubCommand.Select(_ => ReleaseModel.HtmlUrl).Subscribe(gotoUrlCommand);

            GoToLinkCommand = ReactiveCommand.Create();
            GoToLinkCommand.OfType<string>().Subscribe(x =>
            {
                var handledViewModel = urlRouterService.Handle(x);
                if (handledViewModel != null)
                    NavigateTo(handledViewModel);
                else
                    gotoUrlCommand(x);
            });

            var canShowMenu = this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null);
            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(canShowMenu, sender => {
                var menu = actionMenuService.Create();
                menu.AddButton("Share", shareCommand);
                menu.AddButton("Show in GitHub", gotoGitHubCommand);
                return menu.Show(sender);
            });

            _contentText = this.WhenAnyValue(x => x.ReleaseModel).IsNotNull()
                .Select(x => x.BodyHtml).ToProperty(this, x => x.ContentText);

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => {
                var request = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetRelease(ReleaseId);
                ReleaseModel = (await applicationService.Client.ExecuteAsync(request)).Data;
            });
        }
Ejemplo n.º 9
0
        public ChangesetDiffViewModel(IApplicationService applicationService, IActionMenuFactory actionMenuFactory)
        {
            var comments = new ReactiveList <CommitComment>();

            Comments = comments.CreateDerivedCollection(x => x);

            GoToCommentCommand = ReactiveCommand.Create();
            GoToCommentCommand.OfType <int?>().Subscribe(line =>
            {
//                var vm = this.CreateViewModel<CommentViewModel>();
//                ReactiveUI.Legacy.ReactiveCommandMixins.RegisterAsyncTask(vm.SaveCommand, async t =>
//                {
//                    var req = applicationService.Client.Users[Username].Repositories[Repository].Commits[Branch].Comments.Create(vm.Comment, Filename, line);
//                    var response = await applicationService.Client.ExecuteAsync(req);
//			        comments.Add(response.Data);
//                    Dismiss();
//                });
//                NavigateTo(vm);
            });

            _patch = this.WhenAnyValue(x => x.CommitFile)
                     .IsNotNull().Select(x => x.Patch).ToProperty(this, x => x.Patch);

            this.WhenAnyValue(x => x.Filename).Subscribe(x =>
            {
                if (string.IsNullOrEmpty(x))
                {
                    Title = "Diff";
                }
                else
                {
                    _actualFilename = Path.GetFileName(Filename) ??
                                      Filename.Substring(Filename.LastIndexOf('/') + 1);
                    Title = _actualFilename;
                }
            });

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(_ =>
            {
                var sheet = actionMenuFactory.Create(Title);
                sheet.AddButton("Add Comment", ReactiveCommand.Create());
                return(sheet.Show());
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                applicationService.GitHubClient.Repository.RepositoryComments.GetForCommit(Username, Repository, Branch)
                .ToBackground(x => comments.Reset(x));
                var commits = await applicationService.GitHubClient.Repository.Commits.Get(Username, Repository, Branch);
                CommitFile  = commits.Files.FirstOrDefault(x => string.Equals(x.Filename, Filename, StringComparison.Ordinal));
            });
        }
Ejemplo n.º 10
0
        public ChangesetDiffViewModel(ISessionService sessionService, IActionMenuFactory actionMenuFactory, IAlertDialogFactory alertDialogFactory)
        {
            var comments = new ReactiveList <CommitComment>();

            Comments = comments.CreateDerivedCollection(
                x => new FileDiffCommentViewModel(x.User.Login, x.User.AvatarUrl, x.Body, x.Position ?? 0));

            var gotoCreateCommentCommand = ReactiveCommand.Create().WithSubscription(_ => {
                var vm = new ComposerViewModel(async s => {
                    var cmd = new NewCommitComment(s)
                    {
                        Path = Filename, Position = SelectedPatchLine.Item1
                    };
                    var comment = await sessionService.GitHubClient.Repository.RepositoryComments.Create(Username, Repository, Branch, cmd);
                    _commentCreatedObservable.OnNext(comment);
                    comments.Add(comment);
                }, alertDialogFactory);
                NavigateTo(vm);
            });

            GoToCommentCommand = ReactiveCommand.CreateAsyncTask(this.WhenAnyValue(x => x.SelectedPatchLine).Select(x => x != null),
                                                                 sender => {
                var sheet = actionMenuFactory.Create();
                sheet.AddButton(string.Format("Add Comment on Line {0}", SelectedPatchLine.Item2), gotoCreateCommentCommand);
                return(sheet.Show(sender));
            });

            _patch = this.WhenAnyValue(x => x.CommitFile)
                     .IsNotNull().Select(x => x.Patch).ToProperty(this, x => x.Patch);

            this.WhenAnyValue(x => x.Filename).Subscribe(x =>
            {
                if (string.IsNullOrEmpty(x))
                {
                    Title = "Diff";
                }
                else
                {
                    _actualFilename = Path.GetFileName(Filename) ??
                                      Filename.Substring(Filename.LastIndexOf('/') + 1);
                    Title = _actualFilename;
                }
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                sessionService.GitHubClient.Repository.RepositoryComments.GetAllForCommit(Username, Repository, Branch)
                .ToBackground(x => comments.Reset(x.Where(y => string.Equals(y.Path, Filename))));
                var commits = await sessionService.GitHubClient.Repository.Commits.Get(Username, Repository, Branch);
                CommitFile  = commits.Files.FirstOrDefault(x => string.Equals(x.Filename, Filename, StringComparison.Ordinal));
            });
        }
Ejemplo n.º 11
0
        public IssueViewModel(
            ISessionService applicationService, 
            IActionMenuFactory actionMenuFactory,
            IMarkdownService markdownService,
            IAlertDialogFactory alertDialogFactory)
            : base(applicationService, markdownService, actionMenuFactory, alertDialogFactory)
        {
            this.WhenAnyValue(x => x.Id)
                .Subscribe(x => Title = "Issue #" + x);

            _htmlUrl = this.WhenAnyValue(x => x.Issue.HtmlUrl)
                .ToProperty(this, x => x.HtmlUrl);
        }
Ejemplo n.º 12
0
        public IssueViewModel(
            ISessionService applicationService,
            IActionMenuFactory actionMenuFactory,
            IMarkdownService markdownService,
            IAlertDialogFactory alertDialogFactory)
            : base(applicationService, markdownService, actionMenuFactory, alertDialogFactory)
        {
            this.WhenAnyValue(x => x.Id)
            .Subscribe(x => Title = "Issue #" + x);

            _htmlUrl = this.WhenAnyValue(x => x.Issue.HtmlUrl)
                       .ToProperty(this, x => x.HtmlUrl);
        }
Ejemplo n.º 13
0
        public PullRequestViewModel(
            ISessionService applicationService, 
            IMarkdownService markdownService, 
            IActionMenuFactory actionMenuService,
            IAlertDialogFactory alertDialogFactory)
            : base(applicationService, markdownService, actionMenuService, alertDialogFactory)
        {
            this.WhenAnyValue(x => x.Id)
                .Subscribe(x => Title = "Pull Request #" + x);

            this.WhenAnyValue(x => x.PullRequest.HtmlUrl)
                .ToProperty(this, x => x.HtmlUrl, out _htmlUrl);

            var canMergeObservable = this.WhenAnyValue(x => x.PullRequest)
                .Select(x => x != null && !x.Merged && x.Mergeable.HasValue && x.Mergeable.Value);

            _canMerge = canMergeObservable.CombineLatest(
                this.WhenAnyValue(x => x.PushAccess), (x, y) => x && y)
                .ToProperty(this, x => x.CanMerge);

            _commentsCount = this.WhenAnyValue(x => x.Issue.Comments, x => x.Comments.Count, (x, y) => x + y)
                .ToProperty(this, x => x.CommentCount);

            MergeCommand = ReactiveCommand.CreateAsyncTask(canMergeObservable, async t =>  {
                using (alertDialogFactory.Activate("Merging..."))
                {
                    var req = new MergePullRequest { CommitMessage = MergeComment };
                    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));
                    await LoadCommand.ExecuteAsync();
                }
            });

            var canGoToCommits = this.WhenAnyValue(x => x.PullRequest.Commits).Select(x => x > 0);
            GoToCommitsCommand = ReactiveCommand.Create(canGoToCommits);
            GoToCommitsCommand
                .Select(x => this.CreateViewModel<PullRequestCommitsViewModel>())
                .Select(x => x.Init(RepositoryOwner, RepositoryName, Id))
                .Subscribe(NavigateTo);

                var canGoToFiles = this.WhenAnyValue(x => x.PullRequest.ChangedFiles).Select(x => x > 0);
            GoToFilesCommand = ReactiveCommand.Create(canGoToFiles);
            GoToFilesCommand
                .Select(x => this.CreateViewModel<PullRequestFilesViewModel>())
                .Select(x => x.Init(RepositoryOwner, RepositoryName, Id, PullRequest.Head.Sha))
                .Do(x => x.CommentCreated.Subscribe(AddComment))
                .Subscribe(NavigateTo);
        }
Ejemplo n.º 14
0
        public PullRequestDiffViewModel(ISessionService sessionService, IActionMenuFactory actionMenuFactory, IAlertDialogFactory alertDialogFactory)
        {
            var gotoCreateCommentCommand = ReactiveCommand.Create().WithSubscription(_ => {
                var vm = new ComposerViewModel(async s => {
                    var req     = new PullRequestReviewCommentCreate(s, ParentViewModel.HeadSha, Filename, SelectedPatchLine.Item1);
                    var comment = await sessionService.GitHubClient.PullRequest.Comment.Create(ParentViewModel.RepositoryOwner, ParentViewModel.RepositoryName, ParentViewModel.PullRequestId, req);
                    _commentCreatedObservable.OnNext(comment);
                }, alertDialogFactory);
                NavigateTo(vm);
            });

            GoToCommentCommand = ReactiveCommand.CreateAsyncTask(this.WhenAnyValue(x => x.SelectedPatchLine).Select(x => x != null),
                                                                 sender => {
                var sheet = actionMenuFactory.Create();
                sheet.AddButton(string.Format("Add Comment on Line {0}", SelectedPatchLine.Item2), gotoCreateCommentCommand);
                return(sheet.Show(sender));
            });

            this.WhenAnyValue(x => x.PullRequestFile.Patch)
            .IsNotNull()
            .ToProperty(this, x => x.Patch, out _patch);

            var comments = new ReactiveList <PullRequestReviewComment>();

            Comments = comments.CreateDerivedCollection(
                x => new FileDiffCommentViewModel(x.User.Login, x.User.AvatarUrl, x.Body, x.Position ?? 0));

            this.WhenAnyValue(x => x.ParentViewModel.Comments)
            .Merge(this.WhenAnyObservable(x => x.ParentViewModel.Comments.Changed).Select(_ => ParentViewModel.Comments))
            .Select(x => x.Where(y => string.Equals(y.Path, Filename, StringComparison.OrdinalIgnoreCase)).ToList())
            .Subscribe(x => comments.Reset(x));

            this.WhenAnyValue(x => x.PullRequestFile.FileName)
            .ToProperty(this, x => x.Filename, out _filename);

            this.WhenAnyValue(x => x.Filename)
            .Subscribe(x => {
                if (string.IsNullOrEmpty(x))
                {
                    Title = "Diff";
                }
                else
                {
                    Title = Path.GetFileName(Filename) ?? Filename.Substring(Filename.LastIndexOf('/') + 1);
                }
            });
        }
Ejemplo n.º 15
0
        public MyIssuesFilterViewModel(IActionMenuFactory actionMenu)
        {
            Title      = "Filter";
            State      = IssueState.Open;
            SortType   = IssueSort.None;
            FilterType = IssueFilterState.All;

            SaveCommand = ReactiveCommand.CreateAsyncTask(_ => Task.FromResult(new MyIssuesFilterModel(FilterType, State, SortType, Labels, Ascending)));
            SaveCommand.Subscribe(_ => DismissCommand.ExecuteIfCan());
            DismissCommand = ReactiveCommand.Create().WithSubscription(_ => Dismiss());

            SelectStateCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(IssueState)).Cast <IssueState>().ToList();
                var picker  = actionMenu.CreatePicker();
                foreach (var option in options)
                {
                    picker.Options.Add(option.Humanize());
                }
                picker.SelectedOption = options.IndexOf(State);
                var ret = await picker.Show(sender);
                State   = options[ret];
            });

            SelectSortCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(IssueSort)).Cast <IssueSort>().ToList();
                var picker  = actionMenu.CreatePicker();
                foreach (var option in options)
                {
                    picker.Options.Add(option.Humanize());
                }
                picker.SelectedOption = options.IndexOf(SortType);
                var ret  = await picker.Show(sender);
                SortType = options[ret];
            });

            SelectFilterTypeCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(IssueFilterState)).Cast <IssueFilterState>().ToList();
                var picker  = actionMenu.CreatePicker();
                foreach (var option in options)
                {
                    picker.Options.Add(option.Humanize());
                }
                picker.SelectedOption = options.IndexOf(FilterType);
                var ret    = await picker.Show(sender);
                FilterType = options[ret];
            });
        }
Ejemplo n.º 16
0
        public ChangesetDiffViewModel(ISessionService sessionService, IActionMenuFactory actionMenuFactory, IAlertDialogFactory alertDialogFactory)
	    {
            var comments = new ReactiveList<CommitComment>();
            Comments = comments.CreateDerivedCollection(
                x => new FileDiffCommentViewModel(x.User.Login, x.User.AvatarUrl, x.Body, x.Position ?? 0));

            var gotoCreateCommentCommand = ReactiveCommand.Create().WithSubscription(_ => {
                var vm = new ComposerViewModel(async s => {
                    var cmd = new NewCommitComment(s) { Path = Filename, Position = SelectedPatchLine.Item1 };
                    var comment = await sessionService.GitHubClient.Repository.RepositoryComments.Create(Username, Repository, Branch, cmd);
                    _commentCreatedObservable.OnNext(comment);
                    comments.Add(comment);
                }, alertDialogFactory);
                NavigateTo(vm);
            });

            GoToCommentCommand = ReactiveCommand.CreateAsyncTask(this.WhenAnyValue(x => x.SelectedPatchLine).Select(x => x != null),
                sender => {
                    var sheet = actionMenuFactory.Create();
                    sheet.AddButton(string.Format("Add Comment on Line {0}", SelectedPatchLine.Item2), gotoCreateCommentCommand);
                    return sheet.Show(sender);
                });

            _patch = this.WhenAnyValue(x => x.CommitFile)
                .IsNotNull().Select(x => x.Patch).ToProperty(this, x => x.Patch);

	        this.WhenAnyValue(x => x.Filename).Subscribe(x =>
	        {
	            if (string.IsNullOrEmpty(x))
                    Title = "Diff";
	            else
	            {
	                _actualFilename = Path.GetFileName(Filename) ??
	                                  Filename.Substring(Filename.LastIndexOf('/') + 1);
                    Title = _actualFilename;
	            }
	        });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
	        {
                sessionService.GitHubClient.Repository.RepositoryComments.GetAllForCommit(Username, Repository, Branch)
                        .ToBackground(x => comments.Reset(x.Where(y => string.Equals(y.Path, Filename))));
                var commits = await sessionService.GitHubClient.Repository.Commits.Get(Username, Repository, Branch);
                CommitFile = commits.Files.FirstOrDefault(x => string.Equals(x.Filename, Filename, StringComparison.Ordinal));
	        });
	    }
Ejemplo n.º 17
0
        public IssueViewModel(
            IApplicationService applicationService,
            IActionMenuFactory actionMenuFactory,
            IMarkdownService markdownService)
            : base(applicationService, markdownService)
        {
            var issuePresenceObservable = this.WhenAnyValue(x => x.Issue).Select(x => x != null);

            this.WhenAnyValue(x => x.Id)
            .Subscribe(x => Title = "Issue #" + x);

            ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null));
            ShareCommand.Subscribe(_ => actionMenuFactory.ShareUrl(Issue.HtmlUrl));

            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);
            });

            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());
            });
        }
Ejemplo n.º 18
0
        public PullRequestDiffViewModel(ISessionService sessionService, IActionMenuFactory actionMenuFactory, IAlertDialogFactory alertDialogFactory)
        {
            var gotoCreateCommentCommand = ReactiveCommand.Create().WithSubscription(_ => {
                var vm = new ComposerViewModel(async s => {
                    var req = new PullRequestReviewCommentCreate(s, ParentViewModel.HeadSha, Filename, SelectedPatchLine.Item1);
                    var comment = await sessionService.GitHubClient.PullRequest.Comment.Create(ParentViewModel.RepositoryOwner, ParentViewModel.RepositoryName, ParentViewModel.PullRequestId, req);
                    _commentCreatedObservable.OnNext(comment);
                }, alertDialogFactory);
                NavigateTo(vm);
            });

            GoToCommentCommand = ReactiveCommand.CreateAsyncTask(this.WhenAnyValue(x => x.SelectedPatchLine).Select(x => x != null),
                sender => {
                    var sheet = actionMenuFactory.Create();
                    sheet.AddButton(string.Format("Add Comment on Line {0}", SelectedPatchLine.Item2), gotoCreateCommentCommand);
                    return sheet.Show(sender);
                });

            this.WhenAnyValue(x => x.PullRequestFile.Patch)
                .IsNotNull()
                .ToProperty(this, x => x.Patch, out _patch);

            var comments = new ReactiveList<PullRequestReviewComment>();
            Comments = comments.CreateDerivedCollection(
                x => new FileDiffCommentViewModel(x.User.Login, x.User.AvatarUrl, x.Body, x.Position ?? 0));

            this.WhenAnyValue(x => x.ParentViewModel.Comments)
                .Merge(this.WhenAnyObservable(x => x.ParentViewModel.Comments.Changed).Select(_ => ParentViewModel.Comments))
                .Select(x => x.Where(y => string.Equals(y.Path, Filename, StringComparison.OrdinalIgnoreCase)).ToList())
                .Subscribe(x => comments.Reset(x));

            this.WhenAnyValue(x => x.PullRequestFile.FileName)
                .ToProperty(this, x => x.Filename, out _filename);

            this.WhenAnyValue(x => x.Filename)
                .Subscribe(x => {
                    if (string.IsNullOrEmpty(x))
                        Title = "Diff";
                    else
                        Title = Path.GetFileName(Filename) ?? Filename.Substring(Filename.LastIndexOf('/') + 1);
                });
        }
Ejemplo n.º 19
0
        public MyIssuesFilterViewModel(IActionMenuFactory actionMenu)
        {
            Title = "Filter";
            State = IssueState.Open;
            SortType = IssueSort.None;
            FilterType = IssueFilterState.All;

            SaveCommand = ReactiveCommand.CreateAsyncTask(_ => Task.FromResult(new MyIssuesFilterModel(FilterType, State, SortType, Labels, Ascending)));
            SaveCommand.Subscribe(_ => DismissCommand.ExecuteIfCan());
            DismissCommand = ReactiveCommand.Create().WithSubscription(_ => Dismiss());

            SelectStateCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(IssueState)).Cast<IssueState>().ToList();
                var picker = actionMenu.CreatePicker();
                foreach (var option in options)
                    picker.Options.Add(option.Humanize());
                picker.SelectedOption = options.IndexOf(State);
                var ret = await picker.Show(sender);
                State = options[ret];
            });

            SelectSortCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(IssueSort)).Cast<IssueSort>().ToList();
                var picker = actionMenu.CreatePicker();
                foreach (var option in options)
                    picker.Options.Add(option.Humanize());
                picker.SelectedOption = options.IndexOf(SortType);
                var ret = await picker.Show(sender);
                SortType = options[ret];
            });

            SelectFilterTypeCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(IssueFilterState)).Cast<IssueFilterState>().ToList();
                var picker = actionMenu.CreatePicker();
                foreach (var option in options)
                    picker.Options.Add(option.Humanize());
                picker.SelectedOption = options.IndexOf(FilterType);
                var ret = await picker.Show(sender);
                FilterType = options[ret];
            });
        }
Ejemplo n.º 20
0
        public AddEnterpriseAccountViewModel(
            ILoginService loginFactory,
            IAccountsRepository accountsRepository,
            IAlertDialogFactory alertDialogFactory,
            IActionMenuFactory actionMenuFactory)
            : base(alertDialogFactory)
        {
            _loginFactory       = loginFactory;
            _accountsRepository = accountsRepository;

            var gotoOAuthToken = ReactiveCommand.Create().WithSubscription(_ => {
                var vm    = this.CreateViewModel <EnterpriseOAuthTokenLoginViewModel>();
                vm.Domain = Domain;
                NavigateTo(vm);
            });

            ShowLoginOptionsCommand = ReactiveCommand.CreateAsyncTask(sender => {
                var actionMenu = actionMenuFactory.Create();
                actionMenu.AddButton("Login via Token", gotoOAuthToken);
                return(actionMenu.Show(sender));
            });
        }
Ejemplo n.º 21
0
        public AddEnterpriseAccountViewModel(
            ILoginService loginFactory, 
            IAccountsRepository accountsRepository,
            IAlertDialogFactory alertDialogFactory,
            IActionMenuFactory actionMenuFactory)
            : base(alertDialogFactory)
        {
            _loginFactory = loginFactory;
            _accountsRepository = accountsRepository;

            var gotoOAuthToken = ReactiveCommand.Create().WithSubscription(_ => {
                var vm = this.CreateViewModel<EnterpriseOAuthTokenLoginViewModel>();
                vm.Domain = Domain;
                NavigateTo(vm);
            });

            ShowLoginOptionsCommand = ReactiveCommand.CreateAsyncTask(sender => {
                var actionMenu = actionMenuFactory.Create();
                actionMenu.AddButton("Login via Token", gotoOAuthToken);
                return actionMenu.Show(sender);
            });
        }
Ejemplo n.º 22
0
        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));

            var showWebBrowser = new Action<string>(x =>
            {
                var vm = this.CreateViewModel<WebBrowserViewModel>();
                vm.Init(x);
                NavigateTo(vm);
            });

            GoToGitHubCommand = ReactiveCommand.Create(nonNullContentModel);
            GoToGitHubCommand.Select(_ => ContentModel.HtmlUrl).Subscribe(showWebBrowser);

            GoToLinkCommand = ReactiveCommand.Create();
            GoToLinkCommand.OfType<string>().Subscribe(showWebBrowser);

            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(async x =>
            {
                var repository = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName];
                ContentText = await repository.GetReadmeRendered();
                ContentModel = (await applicationService.Client.ExecuteAsync(repository.GetReadme())).Data;
            });
        }
Ejemplo n.º 23
0
        public ReadmeViewModel(IApplicationService applicationService,
                               IActionMenuFactory actionMenuService)
        {
            Title = "Readme";

            var nonNullContentModel = this.WhenAnyValue(x => x.ContentModel).Select(x => x != null);

            ShareCommand = ReactiveCommand.Create(nonNullContentModel);
            ShareCommand.Subscribe(_ => actionMenuService.ShareUrl(ContentModel.HtmlUrl));

            var showWebBrowser = new Action <string>(x =>
            {
                var vm = this.CreateViewModel <WebBrowserViewModel>();
                vm.Url = x;
                NavigateTo(vm);
            });

            GoToGitHubCommand = ReactiveCommand.Create(nonNullContentModel);
            GoToGitHubCommand.Select(_ => ContentModel.HtmlUrl).Subscribe(showWebBrowser);

            GoToLinkCommand = ReactiveCommand.Create();
            GoToLinkCommand.OfType <string>().Subscribe(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;
            });
        }
Ejemplo n.º 24
0
        public OAuthFlowLoginViewModel(
            IAccountsRepository accountsRepository,
            IActionMenuFactory actionMenuService,
            IAlertDialogFactory alertDialogService)
        {
            _accountsRepository = accountsRepository;
            _alertDialogService = alertDialogService;

            Title = "Login";

            var oauthLogin = ReactiveCommand.Create().WithSubscription(_ =>
                                                                       NavigateTo(this.CreateViewModel <OAuthTokenLoginViewModel>()));

            var canLogin     = this.WhenAnyValue(x => x.Code).Select(x => !string.IsNullOrEmpty(x));
            var loginCommand = ReactiveCommand.CreateAsyncTask(canLogin, _ => Login(Code));

            loginCommand.Subscribe(x => MessageBus.Current.SendMessage(new LogoutMessage()));
            LoginCommand = loginCommand;

            ShowLoginOptionsCommand = ReactiveCommand.CreateAsyncTask(sender =>
            {
                var actionMenu = actionMenuService.Create();
                actionMenu.AddButton("Login via Token", oauthLogin);
                return(actionMenu.Show(sender));
            });

            _loginUrl = this.WhenAnyValue(x => x.WebDomain)
                        .IsNotNull()
                        .Select(x => x.TrimEnd('/'))
                        .Select(x =>
                                string.Format(x + "/login/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                                              ClientId, Uri.EscapeDataString(RedirectUri), Uri.EscapeDataString(string.Join(",", OctokitClientFactory.Scopes))))
                        .ToProperty(this, x => x.LoginUrl);

            WebDomain = DefaultWebDomain;
        }
Ejemplo n.º 25
0
        public CommitViewModel(ISessionService applicationService, IActionMenuFactory actionMenuService, IAlertDialogFactory alertDialogFactory)
        {
            Title = "Commit";

            var comments = new ReactiveList<CommentModel>();
            Comments = comments.CreateDerivedCollection(x => new CommitCommentItemViewModel(x));

            this.WhenAnyValue(x => x.Commit)
                .Select(x => new GitHubAvatar(x.GenerateGravatarUrl()))
                .ToProperty(this, x => x.Avatar, out _avatar);

            var files = this.WhenAnyValue(x => x.Commit.Files).IsNotNull();

            files.Select(x => x.Count(y => string.Equals(y.Status, "added")))
                .ToProperty(this, x => x.DiffAdditions, out _diffAdditions);

            files.Select(x => x.Count(y => string.Equals(y.Status, "removed")))
                .ToProperty(this, x => x.DiffDeletions, out _diffDeletions);

            files.Select(x => x.Count(y => string.Equals(y.Status, "modified")))
                .ToProperty(this, x => x.DiffModifications, out _diffModifications);

            GoToAddedFiles = ReactiveCommand.Create(this.WhenAnyValue(x => x.DiffAdditions).Select(x => x > 0));
            GoToAddedFiles
                .Select(_ => new CommitFilesViewModel())
                .Select(y => y.Init(RepositoryOwner, RepositoryName, Node, "Added", Commit.Files.Where(x => string.Equals(x.Status, "added"))))
                .Subscribe(NavigateTo);

            GoToRemovedFiles = ReactiveCommand.Create(this.WhenAnyValue(x => x.DiffDeletions).Select(x => x > 0));
            GoToRemovedFiles
                .Select(_ => new CommitFilesViewModel())
                .Select(y => y.Init(RepositoryOwner, RepositoryName, Node, "Removed", Commit.Files.Where(x => string.Equals(x.Status, "removed"))))
                .Subscribe(NavigateTo);

            GoToModifiedFiles = ReactiveCommand.Create(this.WhenAnyValue(x => x.DiffModifications).Select(x => x > 0));
            GoToModifiedFiles
                .Select(_ => new CommitFilesViewModel())
                .Select(y => y.Init(RepositoryOwner, RepositoryName, Node, "Modified", Commit.Files.Where(x => string.Equals(x.Status, "modified"))))
                .Subscribe(NavigateTo);

            GoToAllFiles = ReactiveCommand.Create(this.WhenAnyValue(x => x.Commit.Files).Select(x => x != null));
            GoToAllFiles
                .Select(_ => new CommitFilesViewModel())
                .Select(y => y.Init(RepositoryOwner, RepositoryName, Node, "All Changes", Commit.Files))
                .Subscribe(NavigateTo);

            this.WhenAnyValue(x => x.Commit)
                .IsNotNull()
                .Select(x => x.GenerateCommiterName())
                .ToProperty(this, x => x.CommiterName, out _commiterName);

            this.WhenAnyValue(x => x.Commit)
                .IsNotNull()
                .Select(x => x.Commit.Message ?? string.Empty)
                .Select(x => Emojis.FindAndReplace(x))
                .ToProperty(this, x => x.CommitMessage, out _commitMessage);

            this.WhenAnyValue(x => x.CommitMessage)
                .IsNotNull()
                .Select(x => {
                    var firstNewLine = x.IndexOf("\n", StringComparison.Ordinal);
                    return firstNewLine > 0 ? x.Substring(0, firstNewLine) : x;   
                })
                .ToProperty(this, x => x.CommitMessageSummary, out _commitMessageSummary);

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Commit).Select(x => x != null));
            GoToHtmlUrlCommand
                .Select(_ => this.CreateViewModel<WebBrowserViewModel>())
                .Select(x => x.Init(Commit.HtmlUrl))
                .Subscribe(NavigateTo);
      
            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.Subscribe(_ => {
                var vm = this.CreateViewModel<RepositoryViewModel>();
                vm.Init(RepositoryOwner, RepositoryName);
                NavigateTo(vm);
            });

            AddCommentCommand = ReactiveCommand.Create().WithSubscription(_ => {
                var vm = new ComposerViewModel(async s => {
                    var request = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Commits[Node].Comments.Create(s);
                    comments.Add((await applicationService.Client.ExecuteAsync(request)).Data);
                }, alertDialogFactory);
                NavigateTo(vm);
            });

            var validCommitObservable = this.WhenAnyValue(x => x.Commit).Select(x => x != null);

            var copyShaCommand = ReactiveCommand.Create(validCommitObservable)
                .WithSubscription(x => actionMenuService.SendToPasteBoard(this.Commit.Sha));

            var shareCommand = ReactiveCommand.Create(validCommitObservable)
                .WithSubscription(sender => actionMenuService.ShareUrl(sender, this.Commit.HtmlUrl));

            var browseCodeCommand = ReactiveCommand.Create(validCommitObservable)
                .WithSubscription(x => 
                {
                    var vm = this.CreateViewModel<SourceTreeViewModel>();
                    vm.RepositoryName = RepositoryName;
                    vm.RepositoryOwner = RepositoryOwner;
                    vm.Branch = this.Commit.Sha;
                    NavigateTo(vm);
                });

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(sender => {
                var menu = actionMenuService.Create();
                menu.AddButton("Add Comment", AddCommentCommand);
                menu.AddButton("Copy SHA", copyShaCommand);
                menu.AddButton("Browse Code", browseCodeCommand);
                menu.AddButton("Share", shareCommand);
                menu.AddButton("Show in GitHub", GoToHtmlUrlCommand);
                return menu.Show(sender);
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t => {
                var commentRequest = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Commits[Node].Comments.GetAll();
                applicationService.Client.ExecuteAsync(commentRequest).ToBackground(x => comments.Reset(x.Data.Where(y => y.Position.HasValue)));
                Commit = await applicationService.GitHubClient.Repository.Commits.Get(RepositoryOwner, RepositoryName, Node);
            });
        }
Ejemplo n.º 26
0
        public PullRequestViewModel(IApplicationService applicationService,
                                    IMarkdownService markdownService, IActionMenuFactory actionMenuService)
        {
            _applicationService = applicationService;
            _markdownService    = markdownService;

            Comments = new ReactiveList <Octokit.IssueComment>();
            Events   = new ReactiveList <Octokit.IssueEvent>();

            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);
                }
            });

            GoToUrlCommand = ReactiveCommand.Create();
            GoToUrlCommand.OfType <string>().Subscribe(x =>
            {
                var vm = this.CreateViewModel <WebBrowserViewModel>();
                vm.Url = x;
                NavigateTo(vm);
            });

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.PullRequest).Select(x => x != null));
            GoToHtmlUrlCommand.Select(_ => PullRequest.HtmlUrl).Subscribe(x => GoToUrlCommand.ExecuteIfCan(x.AbsolutePath));

            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.Username      = RepositoryOwner;
                vm.Repository    = RepositoryName;
                vm.PullRequestId = Id;
                NavigateTo(vm);
            });
//
//            ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.PullRequest).Select(x => x != null && !string.IsNullOrEmpty(x.HtmlUrl)))
//                .WithSubscription(_ => shareService.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);
            });
//
//            GoToLabelsCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)).WithSubscription(_ =>
//            {
//                var vm = this.CreateViewModel<IssueLabelsViewModel>();
//                vm.RepositoryOwner = RepositoryOwner;
//                vm.RepositoryName = RepositoryName;
//                vm.Id = Id;
//                vm.SaveOnSelect = true;
////                vm.SelectedLabels.Reset(Issue.Labels);
////                vm.WhenAnyValue(x => x.Labels).Skip(1).Subscribe(x =>
////                {
////                    Issue.Labels = x.ToList();
////                    this.RaisePropertyChanged("Issue");
////                });
//                NavigateTo(vm);
//            });

//            GoToMilestoneCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)).WithSubscription(_ =>
//            {
//                var vm = this.CreateViewModel<IssueMilestonesViewModel>();
//                vm.RepositoryOwner = RepositoryOwner;
//                vm.RepositoryName = RepositoryName;
//                vm.IssueId = Id;
//                vm.SaveOnSelect = true;
////                vm.SelectedMilestone = Issue.Milestone;
////                vm.WhenAnyValue(x => x.SelectedMilestone).Skip(1).Subscribe(x =>
////                {
////                    Issue.Milestone = x;
////                    this.RaisePropertyChanged("Issue");
////                });
//                NavigateTo(vm);
//            });
//
//            GoToAssigneeCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)).WithSubscription(_ =>
//            {
//                var vm = this.CreateViewModel<IssueAssignedToViewModel>();
//                vm.RepositoryOwner = RepositoryOwner;
//                vm.RepositoryName = RepositoryName;
//                vm.IssueId = Id;
//                //vm.SaveOnSelect = true;
////                vm.SelectedUser = Issue.Assignee;
////                vm.WhenAnyValue(x => x.SelectedUser).Skip(1).Subscribe(x =>
////                {
////                    Issue.Assignee = x;
////                    this.RaisePropertyChanged("Issue");
////                });
//                NavigateTo(vm);
//            });

            GoToAddCommentCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = this.CreateViewModel <IssueCommentViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.Id = Id;
                vm.SaveCommand.Subscribe(Comments.Add);
                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());
            });

            _markdownDescription = this.WhenAnyValue(x => x.PullRequest).IsNotNull()
                                   .Select(x => _markdownService.Convert(x.Body))
                                   .ToProperty(this, x => x.MarkdownDescription);

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                var pullRequest = _applicationService.GitHubClient.PullRequest.Get(RepositoryOwner, RepositoryName, Id);
                var comments    = _applicationService.GitHubClient.PullRequest.Comment.GetAll(RepositoryOwner, RepositoryName, Id);
                var events      = _applicationService.GitHubClient.Issue.Events.GetForIssue(RepositoryOwner, RepositoryName, Id);
                var issue       = _applicationService.GitHubClient.Issue.Get(RepositoryOwner, RepositoryName, Id);

                await Task.WhenAll(pullRequest, issue, comments, events);

                PullRequest = pullRequest.Result;
                Issue       = issue.Result;
            });
        }
Ejemplo n.º 27
0
        public GistViewModel(IApplicationService applicationService,
                             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(_ => actionMenuService.ShareUrl(new Uri(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         = this.CreateViewModel <GistViewModel>();
                vm.Id          = forkedGist.Id;
                vm.Gist        = forkedGist;
                NavigateTo(vm);
            });

            ForkCommand.IsExecuting.Subscribe(x =>
            {
                if (x)
                {
                    alertDialogFactory.Show("Forking...");
                }
                else
                {
                    alertDialogFactory.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(x =>
            {
                var vm = this.CreateViewModel <WebBrowserViewModel>();
                vm.Url = x;
                NavigateTo(vm);
            });

            GoToFileSourceCommand = ReactiveCommand.Create();
            GoToFileSourceCommand.OfType <GistFileModel>().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).Select(x => x != null && x.Owner != null));
            GoToOwnerCommand.Subscribe(x =>
            {
                var vm      = this.CreateViewModel <UserViewModel>();
                vm.Username = Gist.Owner.Login;
                NavigateTo(vm);
            });

            AddCommentCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm = this.CreateViewModel <GistCommentViewModel>();
                vm.Id  = Id;
                vm.SaveCommand.Subscribe(Comments.Add);
                NavigateTo(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);
                Comments.SimpleCollectionLoad(applicationService.Client.Gists[Id].GetComments(), forceCacheInvalidation);
                return(t1);
            });
        }
Ejemplo n.º 28
0
        public UserViewModel(IApplicationService applicationService, IActionMenuFactory actionMenuService)
        {
            _applicationService = applicationService;

            ToggleFollowingCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsFollowing).Select(x => x.HasValue), t => ToggleFollowing());

            GoToGistsCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm      = this.CreateViewModel <UserGistsViewModel>();
                vm.Username = Username;
                NavigateTo(vm);
            });

            GoToRepositoriesCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm      = this.CreateViewModel <UserRepositoriesViewModel>();
                vm.Username = Username;
                NavigateTo(vm);
            });

            GoToOrganizationsCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm      = this.CreateViewModel <OrganizationsViewModel>();
                vm.Username = Username;
                NavigateTo(vm);
            });

            GoToEventsCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm      = this.CreateViewModel <UserEventsViewModel>();
                vm.Username = Username;
                NavigateTo(vm);
            });

            GoToFollowingCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm      = this.CreateViewModel <UserFollowingsViewModel>();
                vm.Username = Username;
                NavigateTo(vm);
            });

            GoToFollowersCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm      = this.CreateViewModel <UserFollowersViewModel>();
                vm.Username = Username;
                NavigateTo(vm);
            });

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsFollowing).Select(x => x.HasValue),
                _ =>
            {
                var menu = actionMenuService.Create(Title);
                menu.AddButton(IsFollowing.Value ? "Unfollow" : "Follow", ToggleFollowingCommand);
                return(menu.Show());
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                if (!IsLoggedInUser)
                {
                    applicationService.GitHubClient.User.Followers.IsFollowingForCurrent(Username)
                    .ToBackground(x => IsFollowing = x);
                }

                User = await applicationService.GitHubClient.User.Get(Username);
            });
        }
Ejemplo n.º 29
0
        public RepositoryViewModel(ISessionService applicationService,
                                   IAccountsRepository accountsService, IActionMenuFactory actionMenuService)
        {
            ApplicationService = applicationService;
            _accountsService   = accountsService;

            var validRepositoryObservable = this.WhenAnyValue(x => x.Repository).Select(x => x != null);

            this.WhenAnyValue(x => x.RepositoryName).Subscribe(x => Title = x);

            this.WhenAnyValue(x => x.Repository.Owner.AvatarUrl)
            .Select(x => new GitHubAvatar(x))
            .ToProperty(this, x => x.Avatar, out _avatar);

            this.WhenAnyValue(x => x.Repository).Subscribe(x =>
            {
                Stargazers = (int?)x?.StargazersCount;
                Watchers   = (int?)x?.SubscribersCount;
            });

            this.WhenAnyValue(x => x.Repository.Description)
            .Select(x => Emojis.FindAndReplace(x))
            .ToProperty(this, x => x.Description, out _description);

            ToggleStarCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue), t => ToggleStar());

            ToggleWatchCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsWatched, x => x.HasValue), t => ToggleWatch());

            GoToOwnerCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository).Select(x => x != null));
            GoToOwnerCommand.Select(_ => Repository.Owner).Subscribe(x => {
                if (AccountType.Organization.Equals(x.Type))
                {
                    var vm = this.CreateViewModel <OrganizationViewModel>();
                    vm.Init(RepositoryOwner);
                    NavigateTo(vm);
                }
                else
                {
                    var vm = this.CreateViewModel <UserViewModel>();
                    vm.Init(RepositoryOwner);
                    NavigateTo(vm);
                }
            });

            PinCommand = ReactiveCommand.Create(validRepositoryObservable);
            PinCommand.Subscribe(x => PinRepository());

            GoToForkParentCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && x.Fork && x.Parent != null));
            GoToForkParentCommand.Subscribe(x =>
            {
                var vm             = this.CreateViewModel <RepositoryViewModel>();
                vm.RepositoryOwner = Repository.Parent.Owner.Login;
                vm.RepositoryName  = Repository.Parent.Name;
                vm.Repository      = Repository.Parent;
                NavigateTo(vm);
            });

            GoToStargazersCommand = ReactiveCommand.Create();
            GoToStargazersCommand.Subscribe(_ =>
            {
                var vm = this.CreateViewModel <RepositoryStargazersViewModel>();
                vm.Init(RepositoryOwner, RepositoryName);
                NavigateTo(vm);
            });

            GoToWatchersCommand = ReactiveCommand.Create();
            GoToWatchersCommand.Subscribe(_ =>
            {
                var vm = this.CreateViewModel <RepositoryWatchersViewModel>();
                vm.Init(RepositoryOwner, RepositoryName);
                NavigateTo(vm);
            });

            GoToEventsCommand = ReactiveCommand.Create();
            GoToEventsCommand.Subscribe(_ => {
                var vm = this.CreateViewModel <RepositoryEventsViewModel>();
                vm.Init(RepositoryOwner, RepositoryName);
                NavigateTo(vm);
            });

            GoToIssuesCommand = ReactiveCommand.Create();
            GoToIssuesCommand
            .Select(_ => this.CreateViewModel <IssuesViewModel>())
            .Select(x => x.Init(RepositoryOwner, RepositoryName))
            .Subscribe(NavigateTo);

            GoToReadmeCommand = ReactiveCommand.Create();
            GoToReadmeCommand
            .Select(_ => this.CreateViewModel <ReadmeViewModel>())
            .Select(x => x.Init(RepositoryOwner, RepositoryName))
            .Subscribe(NavigateTo);

            GoToBranchesCommand = ReactiveCommand.Create();
            GoToBranchesCommand
            .Select(_ => this.CreateViewModel <CommitBranchesViewModel>())
            .Select(x => x.Init(RepositoryOwner, RepositoryName))
            .Subscribe(NavigateTo);

            GoToCommitsCommand = ReactiveCommand.Create();
            GoToCommitsCommand.Subscribe(_ =>
            {
                if (Branches != null && Branches.Count == 1)
                {
                    var vm     = this.CreateViewModel <CommitsViewModel>();
                    var branch = Repository == null ? null : Repository.DefaultBranch;
                    NavigateTo(vm.Init(RepositoryOwner, RepositoryName, branch));
                }
                else
                {
                    GoToBranchesCommand.ExecuteIfCan();
                }
            });

            GoToPullRequestsCommand = ReactiveCommand.Create();
            GoToPullRequestsCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <PullRequestsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToSourceCommand = ReactiveCommand.Create();
            GoToSourceCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <BranchesAndTagsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToContributors = ReactiveCommand.Create();
            GoToContributors.Subscribe(_ =>
            {
                var vm = this.CreateViewModel <RepositoryContributorsViewModel>();
                vm.Init(RepositoryOwner, RepositoryName);
                NavigateTo(vm);
            });

            GoToForksCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = this.CreateViewModel <RepositoryForksViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToReleasesCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = this.CreateViewModel <ReleasesViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            ShareCommand = ReactiveCommand.Create(validRepositoryObservable);
            ShareCommand.Subscribe(sender => actionMenuService.ShareUrl(sender, Repository.HtmlUrl));

            var canShowMenu = this.WhenAnyValue(x => x.Repository, x => x.IsStarred, x => x.IsWatched)
                              .Select(x => x.Item1 != null && x.Item2 != null && x.Item3 != null);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(canShowMenu, sender => {
                var menu = actionMenuService.Create();
                menu.AddButton(IsPinned ? "Unpin from Slideout Menu" : "Pin to Slideout Menu", PinCommand);
                menu.AddButton(IsStarred.Value ? "Unstar This Repo" : "Star This Repo", ToggleStarCommand);
                menu.AddButton(IsWatched.Value ? "Unwatch This Repo" : "Watch This Repo", ToggleWatchCommand);
                menu.AddButton("Show in GitHub", GoToHtmlUrlCommand);
                menu.AddButton("Share", ShareCommand);
                return(menu.Show(sender));
            });

            var gotoWebUrl = new Action <string>(x =>
            {
                var vm = this.CreateViewModel <WebBrowserViewModel>();
                vm.Init(x);
                NavigateTo(vm);
            });

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && !string.IsNullOrEmpty(x.HtmlUrl)));
            GoToHtmlUrlCommand.Select(_ => Repository.HtmlUrl).Subscribe(gotoWebUrl);

            GoToHomepageCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && !string.IsNullOrEmpty(x.Homepage)));
            GoToHomepageCommand.Select(_ => Repository.Homepage).Subscribe(gotoWebUrl);

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => {
                var t1 = applicationService.GitHubClient.Repository.Get(RepositoryOwner, RepositoryName);

                applicationService.GitHubClient.Repository.Content.GetReadme(RepositoryOwner, RepositoryName)
                .ToBackground(x => Readme = x);

                applicationService.GitHubClient.Repository.GetAllBranches(RepositoryOwner, RepositoryName)
                .ToBackground(x => Branches = x);

                applicationService.GitHubClient.Activity.Watching.CheckWatched(RepositoryOwner, RepositoryName)
                .ToBackground(x => IsWatched = x);

                applicationService.GitHubClient.Activity.Starring.CheckStarred(RepositoryOwner, RepositoryName)
                .ToBackground(x => IsStarred = x);

                applicationService.Client.ExecuteAsync(applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContributors())
                .ToBackground(x => Contributors = x.Data.Count);

//                applicationService.GitHubClient.Repository.GetAllLanguages(RepositoryOwner, RepositoryName)
//                    .ToBackground(x => Languages = x.Count);

                applicationService.Client.ExecuteAsync(applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetReleases())
                .ToBackground(x => Releases = x.Data.Count);

                Repository = await t1;
            });
        }
Ejemplo n.º 30
0
        public SourceViewModel(ISessionService sessionService, IActionMenuFactory actionMenuFactory,
                               IFilesystemService filesystemService)
            : base(sessionService)
        {
            var canEdit = this.WhenAnyValue(x => x.SourceItem, x => x.TrueBranch, x => x.PushAccess)
                          .Select(x => x.Item1 != null && x.Item2 && !x.Item1.IsBinary && x.Item3.HasValue && x.Item3.Value);

            GoToEditCommand = ReactiveCommand.Create(canEdit);
            GoToEditCommand.Subscribe(_ => {
                var vm = this.CreateViewModel <EditFileViewModel>();
                vm.Init(RepositoryOwner, RepositoryName, Path, null, Branch);
                vm.SaveCommand.Subscribe(x => {
                    GitUrl = x.Content.GitUrl.AbsoluteUri;
                    LoadCommand.ExecuteIfCan();
                });
                NavigateTo(vm);
            });

            this.WhenAnyValue(x => x.Name).Subscribe(x => Title = x ?? string.Empty);

            _isMarkdown = this.WhenAnyValue(x => x.Path).IsNotNull().Select(x =>
                                                                            MarkdownExtensions.Any(Path.EndsWith)).ToProperty(this, x => x.IsMarkdown);

            OpenInGitHubCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.HtmlUrl).Select(x => !string.IsNullOrEmpty(x)));
            OpenInGitHubCommand
            .Select(_ => this.CreateViewModel <WebBrowserViewModel>())
            .Select(x => x.Init(this.HtmlUrl))
            .Subscribe(NavigateTo);

            var canShowMenu = this.WhenAnyValue(x => x.SourceItem).Select(x => x != null);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(canShowMenu, sender => {
                var menu = actionMenuFactory.Create();
                if (GoToEditCommand.CanExecute(null))
                {
                    menu.AddButton("Edit", GoToEditCommand);
                }
                menu.AddButton("Open With", OpenWithCommand);
                if (OpenInGitHubCommand.CanExecute(null))
                {
                    menu.AddButton("Open in GitHub", OpenInGitHubCommand);
                }
                return(menu.Show(sender));
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                string filepath;
                bool isBinary = false;

                if (!PushAccess.HasValue)
                {
                    sessionService.GitHubClient.Repository.Get(RepositoryOwner, RepositoryName)
                    .ToBackground(x => PushAccess = x.Permissions.Push);
                }

                using (var stream = filesystemService.CreateTempFile(out filepath, Name))
                {
                    if (MarkdownExtensions.Any(Path.EndsWith))
                    {
                        var renderedContent = await sessionService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContentFileRendered(Path, Branch);
                        using (var stringWriter = new StreamWriter(stream))
                        {
                            await stringWriter.WriteAsync(renderedContent);
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(GitUrl) || string.IsNullOrEmpty(HtmlUrl))
                        {
                            var req  = sessionService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContentFile(Path, Branch);
                            var data = (await sessionService.Client.ExecuteAsync(req)).Data;
                            GitUrl   = data.GitUrl;
                            HtmlUrl  = data.HtmlUrl;
                        }

                        var mime = await sessionService.Client.DownloadRawResource2(GitUrl, stream) ?? string.Empty;
                        isBinary = !(mime ?? string.Empty).Contains("charset");
                    }
                }

                // We can force a binary representation if it was passed during init. In which case we don't care to figure out via the mime.
                var fileUri = new Uri(filepath);
                SourceItem  = new FileSourceItemViewModel(fileUri, ForceBinary || isBinary);
            });
        }
Ejemplo n.º 31
0
        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));
        }
Ejemplo n.º 32
0
        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());
            });
        }
Ejemplo n.º 33
0
        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);
            });
        }
Ejemplo n.º 34
0
        public RepositoryViewModel(IApplicationService applicationService,
                                   IAccountsService accountsService, IActionMenuFactory actionMenuService)
        {
            ApplicationService = applicationService;
            _accountsService   = accountsService;

            this.WhenAnyValue(x => x.RepositoryName).Subscribe(x => Title = x);

            this.WhenAnyValue(x => x.Repository).Subscribe(x =>
            {
                Stargazers = x != null ? (int?)x.StargazersCount : null;
                Watchers   = x != null ? (int?)x.SubscribersCount : null;
            });

            ToggleStarCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue), t => ToggleStar());

            ToggleWatchCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsWatched, x => x.HasValue), t => ToggleWatch());

            GoToOwnerCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository).Select(x => x != null));
            GoToOwnerCommand.Select(_ => Repository.Owner).Subscribe(x =>
            {
                if (string.Equals(x.Type, "organization", StringComparison.OrdinalIgnoreCase))
                {
                    var vm      = this.CreateViewModel <OrganizationViewModel>();
                    vm.Username = RepositoryOwner;
                    NavigateTo(vm);
                }
                else
                {
                    var vm      = this.CreateViewModel <UserViewModel>();
                    vm.Username = RepositoryOwner;
                    NavigateTo(vm);
                }
            });

            PinCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository).Select(x => x != null));
            PinCommand.Subscribe(x => PinRepository());

            GoToForkParentCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && x.Fork && x.Parent != null));
            GoToForkParentCommand.Subscribe(x =>
            {
                var vm             = this.CreateViewModel <RepositoryViewModel>();
                vm.RepositoryOwner = Repository.Parent.Owner.Login;
                vm.RepositoryName  = Repository.Parent.Name;
                vm.Repository      = Repository.Parent;
                NavigateTo(vm);
            });

            GoToStargazersCommand = ReactiveCommand.Create();
            GoToStargazersCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <RepositoryStargazersViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToWatchersCommand = ReactiveCommand.Create();
            GoToWatchersCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <RepositoryWatchersViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToEventsCommand = ReactiveCommand.Create();
            GoToEventsCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <RepositoryEventsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToIssuesCommand = ReactiveCommand.Create();
            GoToIssuesCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <IssuesViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToReadmeCommand = ReactiveCommand.Create();
            GoToReadmeCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <ReadmeViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToBranchesCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = this.CreateViewModel <CommitBranchesViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToCommitsCommand = ReactiveCommand.Create();
            GoToCommitsCommand.Subscribe(_ =>
            {
                if (Branches != null && Branches.Count == 1)
                {
                    var vm             = this.CreateViewModel <CommitsViewModel>();
                    vm.RepositoryOwner = RepositoryOwner;
                    vm.RepositoryName  = RepositoryName;
                    vm.Branch          = Repository == null ? null : Repository.DefaultBranch;
                    NavigateTo(vm);
                }
                else
                {
                    GoToBranchesCommand.ExecuteIfCan();
                }
            });

            GoToPullRequestsCommand = ReactiveCommand.Create();
            GoToPullRequestsCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <PullRequestsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToSourceCommand = ReactiveCommand.Create();
            GoToSourceCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <BranchesAndTagsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToContributors = ReactiveCommand.Create();
            GoToContributors.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <RepositoryContributorsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToForksCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = this.CreateViewModel <RepositoryForksViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToReleasesCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = this.CreateViewModel <ReleasesViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.Repository, x => x.IsStarred, x => x.IsWatched)
                .Select(x => x.Item1 != null && x.Item2 != null && x.Item3 != null),
                _ =>
            {
                var menu = actionMenuService.Create(Title);
                menu.AddButton(IsPinned ? "Unpin from Slideout Menu" : "Pin to Slideout Menu", PinCommand);
                menu.AddButton(IsStarred.Value ? "Unstar This Repo" : "Star This Repo", ToggleStarCommand);
                menu.AddButton(IsWatched.Value ? "Unwatch This Repo" : "Watch This Repo", ToggleWatchCommand);
                menu.AddButton("Show in GitHub", GoToHtmlUrlCommand);
                return(menu.Show());
            });

            var gotoWebUrl = new Action <string>(x =>
            {
                var vm = this.CreateViewModel <WebBrowserViewModel>();
                vm.Url = x;
                NavigateTo(vm);
            });

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && !string.IsNullOrEmpty(x.HtmlUrl)));
            GoToHtmlUrlCommand.Select(_ => Repository.HtmlUrl).Subscribe(gotoWebUrl);

            GoToHomepageCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && !string.IsNullOrEmpty(x.Homepage)));
            GoToHomepageCommand.Select(_ => Repository.Homepage).Subscribe(gotoWebUrl);

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var forceCacheInvalidation = t as bool?;

                var t1 = this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Get(),
                                           forceCacheInvalidation, response => Repository = response.Data);

                this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetReadme(),
                                  forceCacheInvalidation, response => Readme = response.Data);

                this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetBranches(),
                                  forceCacheInvalidation, response => Branches = response.Data);

                this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].IsWatching(),
                                  forceCacheInvalidation, response => IsWatched = response.Data);

                this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].IsStarred(),
                                  forceCacheInvalidation, response => IsStarred = response.Data);

                this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContributors(),
                                  forceCacheInvalidation, response => Contributors = response.Data.Count);

                this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetLanguages(),
                                  forceCacheInvalidation, response => Languages = response.Data.Count);

                this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetReleases(),
                                  forceCacheInvalidation, response => Releases = response.Data.Count);

                return(t1);
            });
        }
Ejemplo n.º 35
0
 public static void ShareUrl(this IActionMenuFactory @this, object sender, string uri)
 {
     @this.ShareUrl(sender, new Uri(uri));
 }
        public RepositoryIssuesFilterViewModel(ISessionService sessionService, IActionMenuFactory actionMenu)
        {
            Title = "Filter";
            State = IssueState.Open;
            SortType = CodeHub.Core.Filters.IssueSort.None;

            _userGet = new Lazy<Task<User>>(sessionService.GitHubClient.User.Current);

            SaveCommand = ReactiveCommand.Create().WithSubscription(_ => Dismiss());

            this.WhenAnyValue(x => x.Milestones.Selected)
                .Select(x => x?.Title)
                .ToProperty(this, x => x.MilestoneString, out _milestoneString);

            this.WhenAnyValue(x => x.Assignees.Selected)
                .Select(x => x?.Login)
                .ToProperty(this, x => x.AssigneeString, out _assigneeString);

            this.WhenAnyValue(x => x.Labels.Selected)
                .Select(x => x ?? new List<Label>())
                .Select(x => string.Join(",", x.Select(y => y.Name)))
                .ToProperty(this, x => x.LabelsString, out _labelsString);

            var getAssignees = new Lazy<Task<IReadOnlyList<User>>>(() => sessionService.GitHubClient.Issue.Assignee.GetAllForRepository(RepositoryOwner, RepositoryName));
            var getMilestones = new Lazy<Task<IReadOnlyList<Milestone>>>(() => sessionService.GitHubClient.Issue.Milestone.GetAllForRepository(RepositoryOwner, RepositoryName));
            var getLables = new Lazy<Task<IReadOnlyList<Label>>>(() => sessionService.GitHubClient.Issue.Labels.GetAllForRepository(RepositoryOwner, RepositoryName));

            Assignees = new IssueAssigneeViewModel(() => getAssignees.Value);
            Milestones = new IssueMilestonesViewModel(() => getMilestones.Value);
            Labels = new IssueLabelsViewModel(() => getLables.Value);

            SelectAssigneeCommand = ReactiveCommand.Create();
            SelectMilestoneCommand = ReactiveCommand.Create();
            SelectLabelsCommand = ReactiveCommand.Create();

            SelectStateCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(IssueState)).Cast<IssueState>().ToList();
                var picker = actionMenu.CreatePicker();
                foreach (var option in options)
                    picker.Options.Add(option.Humanize());
                picker.SelectedOption = options.IndexOf(State);
                var ret = await picker.Show(sender);
                State = options[ret];
            });

            SelectSortCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(CodeHub.Core.Filters.IssueSort)).Cast<CodeHub.Core.Filters.IssueSort>().ToList();
                var picker = actionMenu.CreatePicker();
                foreach (var option in options)
                    picker.Options.Add(option.Humanize());
                picker.SelectedOption = options.IndexOf(SortType);
                var ret = await picker.Show(sender);
                SortType = options[ret];
            });
        }
Ejemplo n.º 37
0
        public SourceViewModel(ISessionService sessionService, IActionMenuFactory actionMenuFactory,
            IFilesystemService filesystemService)
            : base(sessionService)
	    {
            var canEdit = this.WhenAnyValue(x => x.SourceItem, x => x.TrueBranch, x => x.PushAccess)
                .Select(x => x.Item1 != null && x.Item2 && !x.Item1.IsBinary && x.Item3.HasValue && x.Item3.Value);
            GoToEditCommand = ReactiveCommand.Create(canEdit);
	        GoToEditCommand.Subscribe(_ => {
	            var vm = this.CreateViewModel<EditFileViewModel>();
                vm.Path = Path;
                vm.Branch = Branch;
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName = RepositoryName;
                vm.SaveCommand.Subscribe(x => {
                    GitUrl = x.Content.GitUrl.AbsoluteUri;
                    LoadCommand.ExecuteIfCan();
                });
	            NavigateTo(vm);
            });

            this.WhenAnyValue(x => x.Name).Subscribe(x => Title = x ?? string.Empty);

            _isMarkdown = this.WhenAnyValue(x => x.Path).IsNotNull().Select(x => 
                MarkdownExtensions.Any(Path.EndsWith)).ToProperty(this, x => x.IsMarkdown);

            OpenInGitHubCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.HtmlUrl).Select(x => !string.IsNullOrEmpty(x)));
            OpenInGitHubCommand
                .Select(_ => this.CreateViewModel<WebBrowserViewModel>())
                .Select(x => x.Init(this.HtmlUrl))
                .Subscribe(NavigateTo);

            var canShowMenu = this.WhenAnyValue(x => x.SourceItem).Select(x => x != null);
            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(canShowMenu, sender => {
                var menu = actionMenuFactory.Create();
                if (GoToEditCommand.CanExecute(null))
                    menu.AddButton("Edit", GoToEditCommand);
                menu.AddButton("Open With", OpenWithCommand);
                if (OpenInGitHubCommand.CanExecute(null))
                    menu.AddButton("Open in GitHub", OpenInGitHubCommand);
                return menu.Show(sender);
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
	        {
	            string filepath;
                bool isBinary = false;

                if (!PushAccess.HasValue)
                {
                    sessionService.GitHubClient.Repository.Get(RepositoryOwner, RepositoryName)
                        .ToBackground(x => PushAccess = x.Permissions.Push);
                }

                using (var stream = filesystemService.CreateTempFile(out filepath, Name))
                {
                    if (MarkdownExtensions.Any(Path.EndsWith))
                    {
                        var renderedContent = await sessionService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContentFileRendered(Path, Branch);
                        using (var stringWriter = new StreamWriter(stream))
                        {
                            await stringWriter.WriteAsync(renderedContent);
                        }
                    } 
                    else
                    {
                        if (string.IsNullOrEmpty(GitUrl) || string.IsNullOrEmpty(HtmlUrl))
                        {
                            var req = sessionService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContentFile(Path, Branch);
                            var data = (await sessionService.Client.ExecuteAsync(req)).Data;
                            GitUrl = data.GitUrl;
                            HtmlUrl = data.HtmlUrl;
                        }

                        var mime = await sessionService.Client.DownloadRawResource2(GitUrl, stream) ?? string.Empty;
                        isBinary = !(mime ?? string.Empty).Contains("charset");
                    }
                }

                // We can force a binary representation if it was passed during init. In which case we don't care to figure out via the mime.
                var fileUri = new Uri(filepath);
                SourceItem = new FileSourceItemViewModel(fileUri, ForceBinary || isBinary);
	        });
	    }
        public RepositoryIssuesFilterViewModel(ISessionService sessionService, IActionMenuFactory actionMenu)
        {
            Title    = "Filter";
            State    = IssueState.Open;
            SortType = CodeHub.Core.Filters.IssueSort.None;

            SaveCommand = ReactiveCommand.CreateAsyncTask(_ => {
                var model = new IssuesFilterModel(Assignee, Creator, Mentioned, Labels, Milestone, State, SortType, Ascending);
                return(Task.FromResult(model));
            });
            SaveCommand.Subscribe(_ => Dismiss());
            DismissCommand = ReactiveCommand.Create().WithSubscription(_ => Dismiss());

            _assigneesCache = new Lazy <Task <IReadOnlyList <User> > >(() =>
                                                                       sessionService.GitHubClient.Issue.Assignee.GetAllForRepository(RepositoryOwner, RepositoryName));
            _milestonesCache = new Lazy <Task <IReadOnlyList <Milestone> > >(() =>
                                                                             sessionService.GitHubClient.Issue.Milestone.GetAllForRepository(RepositoryOwner, RepositoryName));
            _labelsCache = new Lazy <Task <IReadOnlyList <Label> > >(() =>
                                                                     sessionService.GitHubClient.Issue.Labels.GetAllForRepository(RepositoryOwner, RepositoryName));

            this.WhenAnyValue(x => x.Milestone)
            .Select(x => x?.Title)
            .ToProperty(this, x => x.MilestoneString, out _milestoneString);

            this.WhenAnyValue(x => x.Assignee)
            .Select(x => x?.Login)
            .ToProperty(this, x => x.AssigneeString, out _assigneeString);

            this.WhenAnyValue(x => x.Labels)
            .Select(x => x ?? new List <Label>())
            .Select(x => string.Join(", ", x.Select(y => y.Name)))
            .ToProperty(this, x => x.LabelsString, out _labelsString);

            SelectAssigneeCommand  = ReactiveCommand.Create();
            SelectMilestoneCommand = ReactiveCommand.Create();
            SelectLabelsCommand    = ReactiveCommand.Create();

            SelectStateCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(IssueState)).Cast <IssueState>().ToList();
                var picker  = actionMenu.CreatePicker();
                foreach (var option in options)
                {
                    picker.Options.Add(option.Humanize());
                }
                picker.SelectedOption = options.IndexOf(State);
                var ret = await picker.Show(sender);
                State   = options[ret];
            });

            SelectSortCommand = ReactiveCommand.CreateAsyncTask(async sender => {
                var options = Enum.GetValues(typeof(CodeHub.Core.Filters.IssueSort)).Cast <CodeHub.Core.Filters.IssueSort>().ToList();
                var picker  = actionMenu.CreatePicker();
                foreach (var option in options)
                {
                    picker.Options.Add(option.Humanize());
                }
                picker.SelectedOption = options.IndexOf(SortType);
                var ret  = await picker.Show(sender);
                SortType = options[ret];
            });
        }
Ejemplo n.º 39
0
        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);

            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));
                }, 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));
        }
Ejemplo n.º 40
0
 public static Task ShareUrl(this IActionMenuFactory @this, string uri)
 {
     return(@this.ShareUrl(new Uri(uri)));
 }
Ejemplo n.º 41
0
        public CommitViewModel(IApplicationService applicationService, IActionMenuFactory actionMenuService)
        {
            Title = "Commit";

            var comments = new ReactiveList <Octokit.CommitComment>();

            Comments = comments.CreateDerivedCollection(x => x);

            this.WhenAnyValue(x => x.Commit)
            .IsNotNull()
            .Select(x => x.Commit.Message ?? string.Empty)
            .Select(x =>
            {
                var firstNewLine = x.IndexOf("\n", StringComparison.Ordinal);
                return(firstNewLine > 0 ? x.Substring(0, firstNewLine) : x);
            })
            .ToProperty(this, x => x.CommitMessageSummary, out _commitMessageSummary);

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Commit).Select(x => x != null));
            GoToHtmlUrlCommand.Select(x => Commit.HtmlUrl).Subscribe(x =>
            {
                var vm = this.CreateViewModel <WebBrowserViewModel>();
                vm.Url = x;
                NavigateTo(vm);
            });

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <RepositoryViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                NavigateTo(vm);
            });

            GoToCommentCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = this.CreateViewModel <CommitCommentViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.Node            = Node;
                vm.SaveCommand.Subscribe(comments.Add);
                NavigateTo(vm);
            });

            GoToFileCommand = ReactiveCommand.Create();
            GoToFileCommand.OfType <Octokit.GitHubCommitFile>().Subscribe(x =>
            {
                if (x.Patch == null)
                {
                    var vm             = this.CreateViewModel <SourceViewModel>();
                    vm.Branch          = Commit.Sha;
                    vm.Path            = x.Filename;
                    vm.RepositoryOwner = RepositoryOwner;
                    vm.RepositoryName  = RepositoryName;
                    vm.Name            = System.IO.Path.GetFileName(x.Filename);
                    vm.ForceBinary     = true;
                    NavigateTo(vm);
                }
                else
                {
                    var vm        = this.CreateViewModel <ChangesetDiffViewModel>();
                    vm.Username   = RepositoryOwner;
                    vm.Repository = RepositoryName;
                    vm.Branch     = Commit.Sha;
                    vm.Filename   = x.Filename;
                    NavigateTo(vm);
                }
            });

            var validCommitObservable = this.WhenAnyValue(x => x.Commit).Select(x => x != null);

            var copyShaCommand = ReactiveCommand.Create(validCommitObservable)
                                 .WithSubscription(x => actionMenuService.SendToPasteBoard(this.Commit.Sha));

            var shareCommand = ReactiveCommand.Create(validCommitObservable)
                               .WithSubscription(x => actionMenuService.ShareUrl(this.Commit.HtmlUrl));

            var browseCodeCommand = ReactiveCommand.Create(validCommitObservable)
                                    .WithSubscription(x =>
            {
                var vm             = this.CreateViewModel <SourceTreeViewModel>();
                vm.RepositoryName  = RepositoryName;
                vm.RepositoryOwner = RepositoryOwner;
                vm.Branch          = this.Commit.Sha;
                NavigateTo(vm);
            });

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(_ =>
            {
                var menu = actionMenuService.Create(Title);
                menu.AddButton("Add Comment", GoToCommentCommand);
                menu.AddButton("Copy SHA", copyShaCommand);
                menu.AddButton("Browse Code", browseCodeCommand);
                menu.AddButton("Share", shareCommand);
                menu.AddButton("Show in GitHub", GoToHtmlUrlCommand);
                return(menu.Show());
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                applicationService.GitHubClient.Repository.RepositoryComments.GetForCommit(RepositoryOwner, RepositoryName, Node)
                .ToBackground(x => comments.Reset(x));
                Commit = await applicationService.GitHubClient.Repository.Commits.Get(RepositoryOwner, RepositoryName, Node);
            });
        }
Ejemplo n.º 42
0
        public ReleaseViewModel(ISessionService applicationService,
                                IUrlRouterService urlRouterService, IActionMenuFactory actionMenuService)
        {
            Title = "Release";

            this.WhenAnyValue(x => x.ReleaseModel)
            .Select(x =>
            {
                if (x == null)
                {
                    return("Release");
                }
                var name = string.IsNullOrEmpty(x.Name) ? x.TagName : x.Name;
                return(name ?? "Release");
            })
            .Subscribe(x => Title = x);

            var shareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null));

            shareCommand.Subscribe(sender => actionMenuService.ShareUrl(sender, ReleaseModel.HtmlUrl));

            var gotoUrlCommand = new Action <string>(x =>
            {
                var vm = this.CreateViewModel <WebBrowserViewModel>();
                vm.Init(x);
                NavigateTo(vm);
            });

            var gotoGitHubCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null));

            gotoGitHubCommand.Select(_ => ReleaseModel.HtmlUrl).Subscribe(gotoUrlCommand);

            GoToLinkCommand = ReactiveCommand.Create();
            GoToLinkCommand.OfType <string>().Subscribe(x =>
            {
                var handledViewModel = urlRouterService.Handle(x);
                if (handledViewModel != null)
                {
                    NavigateTo(handledViewModel);
                }
                else
                {
                    gotoUrlCommand(x);
                }
            });

            var canShowMenu = this.WhenAnyValue(x => x.ReleaseModel).Select(x => x != null);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(canShowMenu, sender => {
                var menu = actionMenuService.Create();
                menu.AddButton("Share", shareCommand);
                menu.AddButton("Show in GitHub", gotoGitHubCommand);
                return(menu.Show(sender));
            });

            _contentText = this.WhenAnyValue(x => x.ReleaseModel).IsNotNull()
                           .Select(x => x.BodyHtml).ToProperty(this, x => x.ContentText);

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => {
                var request  = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetRelease(ReleaseId);
                ReleaseModel = (await applicationService.Client.ExecuteAsync(request)).Data;
            });
        }
Ejemplo n.º 43
0
        public CommitViewModel(ISessionService applicationService, IActionMenuFactory actionMenuService, IAlertDialogFactory alertDialogFactory)
        {
            Title = "Commit";

            var comments = new ReactiveList <CommentModel>();

            Comments = comments.CreateDerivedCollection(x => new CommitCommentItemViewModel(x));

            this.WhenAnyValue(x => x.Commit)
            .Select(x => new GitHubAvatar(x.GenerateGravatarUrl()))
            .ToProperty(this, x => x.Avatar, out _avatar);

            var files = this.WhenAnyValue(x => x.Commit.Files).IsNotNull();

            files.Select(x => x.Count(y => string.Equals(y.Status, "added")))
            .ToProperty(this, x => x.DiffAdditions, out _diffAdditions);

            files.Select(x => x.Count(y => string.Equals(y.Status, "removed")))
            .ToProperty(this, x => x.DiffDeletions, out _diffDeletions);

            files.Select(x => x.Count(y => string.Equals(y.Status, "modified")))
            .ToProperty(this, x => x.DiffModifications, out _diffModifications);

            GoToAddedFiles = ReactiveCommand.Create(this.WhenAnyValue(x => x.DiffAdditions).Select(x => x > 0));
            GoToAddedFiles
            .Select(_ => new CommitFilesViewModel())
            .Select(y => y.Init(RepositoryOwner, RepositoryName, Node, "Added", Commit.Files.Where(x => string.Equals(x.Status, "added"))))
            .Subscribe(NavigateTo);

            GoToRemovedFiles = ReactiveCommand.Create(this.WhenAnyValue(x => x.DiffDeletions).Select(x => x > 0));
            GoToRemovedFiles
            .Select(_ => new CommitFilesViewModel())
            .Select(y => y.Init(RepositoryOwner, RepositoryName, Node, "Removed", Commit.Files.Where(x => string.Equals(x.Status, "removed"))))
            .Subscribe(NavigateTo);

            GoToModifiedFiles = ReactiveCommand.Create(this.WhenAnyValue(x => x.DiffModifications).Select(x => x > 0));
            GoToModifiedFiles
            .Select(_ => new CommitFilesViewModel())
            .Select(y => y.Init(RepositoryOwner, RepositoryName, Node, "Modified", Commit.Files.Where(x => string.Equals(x.Status, "modified"))))
            .Subscribe(NavigateTo);

            GoToAllFiles = ReactiveCommand.Create(this.WhenAnyValue(x => x.Commit.Files).Select(x => x != null));
            GoToAllFiles
            .Select(_ => new CommitFilesViewModel())
            .Select(y => y.Init(RepositoryOwner, RepositoryName, Node, "All Changes", Commit.Files))
            .Subscribe(NavigateTo);

            this.WhenAnyValue(x => x.Commit)
            .IsNotNull()
            .Select(x => x.GenerateCommiterName())
            .ToProperty(this, x => x.CommiterName, out _commiterName);

            this.WhenAnyValue(x => x.Commit)
            .IsNotNull()
            .Select(x => x.Commit.Message ?? string.Empty)
            .Select(x => Emojis.FindAndReplace(x))
            .ToProperty(this, x => x.CommitMessage, out _commitMessage);

            this.WhenAnyValue(x => x.CommitMessage)
            .IsNotNull()
            .Select(x => {
                var firstNewLine = x.IndexOf("\n", StringComparison.Ordinal);
                return(firstNewLine > 0 ? x.Substring(0, firstNewLine) : x);
            })
            .ToProperty(this, x => x.CommitMessageSummary, out _commitMessageSummary);

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Commit).Select(x => x != null));
            GoToHtmlUrlCommand
            .Select(_ => this.CreateViewModel <WebBrowserViewModel>())
            .Select(x => x.Init(Commit.HtmlUrl))
            .Subscribe(NavigateTo);

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.Subscribe(_ => {
                var vm = this.CreateViewModel <RepositoryViewModel>();
                vm.Init(RepositoryOwner, RepositoryName);
                NavigateTo(vm);
            });

            AddCommentCommand = ReactiveCommand.Create().WithSubscription(_ => {
                var vm = new ComposerViewModel(async s => {
                    var request = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Commits[Node].Comments.Create(s);
                    comments.Add((await applicationService.Client.ExecuteAsync(request)).Data);
                }, alertDialogFactory);
                NavigateTo(vm);
            });

            var validCommitObservable = this.WhenAnyValue(x => x.Commit).Select(x => x != null);

            var copyShaCommand = ReactiveCommand.Create(validCommitObservable)
                                 .WithSubscription(x => actionMenuService.SendToPasteBoard(this.Commit.Sha));

            var shareCommand = ReactiveCommand.Create(validCommitObservable)
                               .WithSubscription(sender => actionMenuService.ShareUrl(sender, this.Commit.HtmlUrl));

            var browseCodeCommand = ReactiveCommand.Create(validCommitObservable)
                                    .WithSubscription(x =>
            {
                var vm             = this.CreateViewModel <SourceTreeViewModel>();
                vm.RepositoryName  = RepositoryName;
                vm.RepositoryOwner = RepositoryOwner;
                vm.Branch          = this.Commit.Sha;
                NavigateTo(vm);
            });

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(sender => {
                var menu = actionMenuService.Create();
                menu.AddButton("Add Comment", AddCommentCommand);
                menu.AddButton("Copy SHA", copyShaCommand);
                menu.AddButton("Browse Code", browseCodeCommand);
                menu.AddButton("Share", shareCommand);
                menu.AddButton("Show in GitHub", GoToHtmlUrlCommand);
                return(menu.Show(sender));
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t => {
                var commentRequest = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Commits[Node].Comments.GetAll();
                applicationService.Client.ExecuteAsync(commentRequest).ToBackground(x => comments.Reset(x.Data.Where(y => y.Position.HasValue)));
                Commit = await applicationService.GitHubClient.Repository.Commits.Get(RepositoryOwner, RepositoryName, Node);
            });
        }
Ejemplo n.º 44
0
        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).Select(x => x != null && x.Owner != 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);
            });
        }
Ejemplo n.º 45
0
        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());
            });
        }
Ejemplo n.º 46
0
        public UserViewModel(ISessionService applicationService, IActionMenuFactory actionMenuService)
        {
            _applicationService = applicationService;

            ToggleFollowingCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsFollowing).Select(x => x.HasValue), t => ToggleFollowing());

            _hasBlog = this.WhenAnyValue(x => x.User.Blog)
                       .Select(x => !string.IsNullOrEmpty(x))
                       .ToProperty(this, x => x.HasBlog);

            _avatar = this.WhenAnyValue(x => x.User.AvatarUrl)
                      .Select(x => new GitHubAvatar(x))
                      .ToProperty(this, x => x.Avatar);

            GoToGistsCommand = ReactiveCommand.Create();
            GoToGistsCommand
            .Select(_ => this.CreateViewModel <UserGistsViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToRepositoriesCommand = ReactiveCommand.Create();
            GoToRepositoriesCommand
            .Select(_ => this.CreateViewModel <UserRepositoriesViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToOrganizationsCommand = ReactiveCommand.Create();
            GoToOrganizationsCommand
            .Select(_ => this.CreateViewModel <OrganizationsViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToEventsCommand = ReactiveCommand.Create();
            GoToEventsCommand
            .Select(_ => this.CreateViewModel <UserEventsViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToFollowingCommand = ReactiveCommand.Create();
            GoToFollowingCommand
            .Select(_ => this.CreateViewModel <UserFollowingsViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToFollowersCommand = ReactiveCommand.Create();
            GoToFollowersCommand
            .Select(_ => this.CreateViewModel <UserFollowersViewModel>())
            .Select(x => x.Init(Username))
            .Subscribe(NavigateTo);

            GoToWebsiteCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.HasBlog));
            GoToWebsiteCommand
            .Select(_ => this.CreateViewModel <WebBrowserViewModel>())
            .Select(x => x.Init(User.Blog))
            .Subscribe(NavigateTo);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsFollowing).Select(x => x.HasValue),
                sender => {
                var menu = actionMenuService.Create();
                menu.AddButton(IsFollowing.Value ? "Unfollow" : "Follow", ToggleFollowingCommand);
                return(menu.Show(sender));
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                if (!IsLoggedInUser)
                {
                    applicationService.GitHubClient.User.Followers.IsFollowingForCurrent(Username)
                    .ToBackground(x => IsFollowing = x);
                }

                User = await applicationService.GitHubClient.User.Get(Username);
            });
        }
Ejemplo n.º 47
0
        public LoginViewModel(ILoginService loginFactory,
                              IAccountsService accountsService,
                              IActionMenuFactory actionMenuService,
                              IAlertDialogFactory alertDialogService)
        {
            _loginFactory = loginFactory;

            Title = "Login";

            GoToOldLoginWaysCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                                NavigateTo(this.CreateViewModel <AddAccountViewModel>()));

            var loginCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.Code).Select(x => !string.IsNullOrEmpty(x)), _ => Login(Code));

            loginCommand.Subscribe(x => accountsService.ActiveAccount = x);
            loginCommand.Subscribe(x => MessageBus.Current.SendMessage(new LogoutMessage()));
            LoginCommand = loginCommand;

            ShowLoginOptionsCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var actionMenu = actionMenuService.Create(Title);
                actionMenu.AddButton("Login via BASIC", GoToOldLoginWaysCommand);
                return(actionMenu.Show());
            });

            LoginCommand.IsExecuting.Skip(1).Subscribe(x =>
            {
                if (x)
                {
                    alertDialogService.Show("Logging in...");
                }
                else
                {
                    alertDialogService.Hide();
                }
            });

            _loginUrl = this.WhenAnyValue(x => x.WebDomain)
                        .IsNotNull()
                        .Select(x => x.TrimEnd('/'))
                        .Select(x =>
                                string.Format(x + "/login/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                                              ClientId, Uri.EscapeDataString(RedirectUri), Uri.EscapeDataString("user,repo,notifications,gist")))
                        .ToProperty(this, x => x.LoginUrl);

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                if (IsEnterprise && string.IsNullOrEmpty(WebDomain))
                {
                    var response = await alertDialogService.PromptTextBox("Enterprise URL",
                                                                          "Please enter the webpage address for the GitHub Enterprise installation",
                                                                          DefaultWebDomain, "Ok");
                    WebDomain = response;
                }
                else
                {
                    WebDomain = DefaultWebDomain;
                }
            });

            LoadCommand.ThrownExceptions.Take(1).Subscribe(x => Dismiss());
        }
Ejemplo n.º 48
0
        public RepositoryViewModel(ISessionService applicationService, 
            IAccountsRepository accountsService, IActionMenuFactory actionMenuService)
        {
            ApplicationService = applicationService;
            _accountsService = accountsService;

            var validRepositoryObservable = this.WhenAnyValue(x => x.Repository).Select(x => x != null);

            this.WhenAnyValue(x => x.RepositoryName).Subscribe(x => Title = x);

            this.WhenAnyValue(x => x.Repository).Subscribe(x => 
            {
                Stargazers = x != null ? (int?)x.StargazersCount : null;
                Watchers = x != null ? (int?)x.SubscribersCount : null;
            });

            this.WhenAnyValue(x => x.Repository.Description)
                .Select(x => Emojis.FindAndReplace(x))
                .ToProperty(this, x => x.Description, out _description);

            ToggleStarCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue), t => ToggleStar());

            ToggleWatchCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsWatched, x => x.HasValue), t => ToggleWatch());

            GoToOwnerCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository).Select(x => x != null));
            GoToOwnerCommand.Select(_ => Repository.Owner).Subscribe(x => {
                if (string.Equals(x.Type, "organization", StringComparison.OrdinalIgnoreCase))
                {
                    var vm = this.CreateViewModel<OrganizationViewModel>();
                    vm.Init(RepositoryOwner);
                    NavigateTo(vm);
                }
                else
                {
                    var vm = this.CreateViewModel<UserViewModel>();
                    vm.Init(RepositoryOwner);
                    NavigateTo(vm);
                }
            });

            PinCommand = ReactiveCommand.Create(validRepositoryObservable);
            PinCommand.Subscribe(x => PinRepository());

            GoToForkParentCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && x.Fork && x.Parent != null));
            GoToForkParentCommand.Subscribe(x =>
            {
                var vm = this.CreateViewModel<RepositoryViewModel>();
                vm.RepositoryOwner = Repository.Parent.Owner.Login;
                vm.RepositoryName = Repository.Parent.Name;
                vm.Repository = Repository.Parent;
                NavigateTo(vm);
            });

            GoToStargazersCommand = ReactiveCommand.Create();
            GoToStargazersCommand.Subscribe(_ =>
            {
                var vm = this.CreateViewModel<RepositoryStargazersViewModel>();
                vm.Init(RepositoryOwner, RepositoryName);
                NavigateTo(vm);
            });

            GoToWatchersCommand = ReactiveCommand.Create();
            GoToWatchersCommand.Subscribe(_ =>
            {
                var vm = this.CreateViewModel<RepositoryWatchersViewModel>();
                vm.Init(RepositoryOwner, RepositoryName);
                NavigateTo(vm);
            });

            GoToEventsCommand = ReactiveCommand.Create();
            GoToEventsCommand.Subscribe(_ => {
                var vm = this.CreateViewModel<RepositoryEventsViewModel>();
                vm.Init(RepositoryOwner, RepositoryName);
                NavigateTo(vm);
            });

            GoToIssuesCommand = ReactiveCommand.Create();
            GoToIssuesCommand
                .Select(_ => this.CreateViewModel<IssuesViewModel>())
                .Select(x => x.Init(RepositoryOwner, RepositoryName))
                .Subscribe(NavigateTo);

            GoToReadmeCommand = ReactiveCommand.Create();
            GoToReadmeCommand
                .Select(_ => this.CreateViewModel<ReadmeViewModel>())
                .Select(x => x.Init(RepositoryOwner, RepositoryName))
                .Subscribe(NavigateTo);

            GoToBranchesCommand = ReactiveCommand.Create();
            GoToBranchesCommand
                .Select(_ => this.CreateViewModel<CommitBranchesViewModel>())
                .Select(x => x.Init(RepositoryOwner, RepositoryName))
                .Subscribe(NavigateTo);

            GoToCommitsCommand = ReactiveCommand.Create();
            GoToCommitsCommand.Subscribe(_ =>
            {
                if (Branches != null && Branches.Count == 1)
                {
                    var vm = this.CreateViewModel<CommitsViewModel>();
                    var branch = Repository == null ? null : Repository.DefaultBranch;
                    NavigateTo(vm.Init(RepositoryOwner, RepositoryName, branch));
                }
                else
                {
                    GoToBranchesCommand.ExecuteIfCan();
                }
            });

            GoToPullRequestsCommand = ReactiveCommand.Create();
            GoToPullRequestsCommand.Subscribe(_ =>
            {
                var vm = this.CreateViewModel<PullRequestsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName = RepositoryName;
                NavigateTo(vm);
            });

            GoToSourceCommand = ReactiveCommand.Create();
            GoToSourceCommand.Subscribe(_ =>
            {
                var vm = this.CreateViewModel<BranchesAndTagsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName = RepositoryName;
                NavigateTo(vm);
            });

            GoToContributors = ReactiveCommand.Create();
            GoToContributors.Subscribe(_ =>
            {
                var vm = this.CreateViewModel<RepositoryContributorsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName = RepositoryName;
                NavigateTo(vm);
            });

            GoToForksCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm = this.CreateViewModel<RepositoryForksViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName = RepositoryName;
                NavigateTo(vm);
            });

            GoToReleasesCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm = this.CreateViewModel<ReleasesViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName = RepositoryName;
                NavigateTo(vm);
            });

            ShareCommand = ReactiveCommand.Create(validRepositoryObservable);
            ShareCommand.Subscribe(sender => actionMenuService.ShareUrl(sender, Repository.HtmlUrl));

            var canShowMenu = this.WhenAnyValue(x => x.Repository, x => x.IsStarred, x => x.IsWatched)
                .Select(x => x.Item1 != null && x.Item2 != null && x.Item3 != null);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(canShowMenu, sender => {
                var menu = actionMenuService.Create();
                menu.AddButton(IsPinned ? "Unpin from Slideout Menu" : "Pin to Slideout Menu", PinCommand);
                menu.AddButton(IsStarred.Value ? "Unstar This Repo" : "Star This Repo", ToggleStarCommand);
                menu.AddButton(IsWatched.Value ? "Unwatch This Repo" : "Watch This Repo", ToggleWatchCommand);
                menu.AddButton("Show in GitHub", GoToHtmlUrlCommand);
                menu.AddButton("Share", ShareCommand);
                return menu.Show(sender);
            });

            var gotoWebUrl = new Action<string>(x =>
            {
                var vm = this.CreateViewModel<WebBrowserViewModel>();
                vm.Init(x);
                NavigateTo(vm);
            });

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && !string.IsNullOrEmpty(x.HtmlUrl)));
            GoToHtmlUrlCommand.Select(_ => Repository.HtmlUrl).Subscribe(gotoWebUrl);

            GoToHomepageCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && !string.IsNullOrEmpty(x.Homepage)));
            GoToHomepageCommand.Select(_ => Repository.Homepage).Subscribe(gotoWebUrl);

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => {

                var t1 = applicationService.Client.ExecuteAsync(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Get());

                applicationService.Client.ExecuteAsync(applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetReadme())
                    .ToBackground(x => Readme = x.Data);

                applicationService.Client.ExecuteAsync(applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetBranches())
                    .ToBackground(x => Branches = x.Data);

                applicationService.GitHubClient.Activity.Watching.CheckWatched(RepositoryOwner, RepositoryName)
                    .ToBackground(x => IsWatched = x);

                applicationService.GitHubClient.Activity.Starring.CheckStarred(RepositoryOwner, RepositoryName)
                    .ToBackground(x => IsStarred = x);

                applicationService.Client.ExecuteAsync(applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContributors())
                    .ToBackground(x => Contributors = x.Data.Count);

//                applicationService.GitHubClient.Repository.GetAllLanguages(RepositoryOwner, RepositoryName)
//                    .ToBackground(x => Languages = x.Count);

                applicationService.Client.ExecuteAsync(applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetReleases())
                    .ToBackground(x => Releases = x.Data.Count);

                Repository = (await t1).Data;
            });
        }
Ejemplo n.º 49
0
        public UserViewModel(ISessionService applicationService, IActionMenuFactory actionMenuService)
        {
            _applicationService = applicationService;

            ToggleFollowingCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsFollowing).Select(x => x.HasValue), t => ToggleFollowing());

            _hasBlog = this.WhenAnyValue(x => x.User.Blog)
                .Select(x => !string.IsNullOrEmpty(x))
                .ToProperty(this, x => x.HasBlog);

            GoToGistsCommand = ReactiveCommand.Create();
            GoToGistsCommand
                .Select(_ => this.CreateViewModel<UserGistsViewModel>())
                .Select(x => x.Init(Username))
                .Subscribe(NavigateTo);

            GoToRepositoriesCommand = ReactiveCommand.Create();
            GoToRepositoriesCommand
                .Select(_ => this.CreateViewModel<UserRepositoriesViewModel>())
                .Select(x => x.Init(Username))
                .Subscribe(NavigateTo);

            GoToOrganizationsCommand = ReactiveCommand.Create();
            GoToOrganizationsCommand
                .Select(_ => this.CreateViewModel<OrganizationsViewModel>())
                .Select(x => x.Init(Username))
                .Subscribe(NavigateTo);
            
            GoToEventsCommand = ReactiveCommand.Create();
            GoToEventsCommand
                .Select(_ => this.CreateViewModel<UserEventsViewModel>())
                .Select(x => x.Init(Username))
                .Subscribe(NavigateTo);

            GoToFollowingCommand = ReactiveCommand.Create();
            GoToFollowingCommand
                .Select(_ => this.CreateViewModel<UserFollowingsViewModel>())
                .Select(x => x.Init(Username))
                .Subscribe(NavigateTo);

            GoToFollowersCommand = ReactiveCommand.Create();
            GoToFollowersCommand
                .Select(_ => this.CreateViewModel<UserFollowersViewModel>())
                .Select(x => x.Init(Username))
                .Subscribe(NavigateTo);

            GoToWebsiteCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.HasBlog));
            GoToWebsiteCommand
                .Select(_ => this.CreateViewModel<WebBrowserViewModel>())
                .Select(x => x.Init(User.Blog))
                .Subscribe(NavigateTo);

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.IsFollowing).Select(x => x.HasValue),
                sender => {
                    var menu = actionMenuService.Create();
                    menu.AddButton(IsFollowing.Value ? "Unfollow" : "Follow", ToggleFollowingCommand);
                    return menu.Show(sender);
                });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                if (!IsLoggedInUser)
                {
                    applicationService.GitHubClient.User.Followers.IsFollowingForCurrent(Username)
                        .ToBackground(x => IsFollowing = x);
                }

                User = await applicationService.GitHubClient.User.Get(Username);
            });
        }