Beispiel #1
0
        public ShowcaseViewModel(INetworkActivityService networkActivity, ShowcaseRepository showcaseRepository)
        {
            Title = "Showcase";

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <Octokit.Repository>().Subscribe(x =>
            {
                var vm = CreateViewModel <RepositoryViewModel>();
                vm.RepositoryIdentifier = new BaseRepositoryViewModel.RepositoryIdentifierModel(x.Owner.Login, x.Name);
                ShowViewModel(vm);
            });

            var repositories = new ReactiveList <Octokit.Repository>();

            Repositories = repositories;
            LoadCommand  = ReactiveCommand.CreateAsyncTask(async t =>
            {
                var showcaseRepos = await showcaseRepository.GetShowcaseRepositories(ShowcaseSlug);
                Title             = showcaseRepos.Name;
                Showcase          = new Showcase {
                    Slug = showcaseRepos.Slug, Description = showcaseRepos.Description, Name = showcaseRepos.Name
                };
                repositories.Reset(showcaseRepos.Repositories);
            });
            LoadCommand.TriggerNetworkActivity(networkActivity);
        }
        public ChangesetViewModel(IApplicationService applicationService)
        {
            _applicationService = applicationService;

            Comments = new ReactiveList <CommentModel>();

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Commit).Select(x => x != null));
            GoToHtmlUrlCommand.Select(x => Commit).Subscribe(GoToUrlCommand.ExecuteIfCan);

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

            GoToFileCommand = ReactiveCommand.Create();
            GoToFileCommand.OfType <CommitModel.CommitFileModel>().Subscribe(x =>
            {
                if (x.Patch == null)
                {
                    var vm        = CreateViewModel <SourceViewModel>();
                    vm.Branch     = Commit.Sha;
                    vm.Username   = RepositoryOwner;
                    vm.Repository = RepositoryName;
                    vm.Items      = new []
                    {
                        new SourceViewModel.SourceItemModel
                        {
                            ForceBinary = true,
                            GitUrl      = x.BlobUrl,
                            Name        = x.Filename,
                            Path        = x.Filename,
                            HtmlUrl     = x.BlobUrl
                        }
                    };
                    vm.CurrentItemIndex = 0;
                    ShowViewModel(vm);
                }
                else
                {
                    var vm        = CreateViewModel <ChangesetDiffViewModel>();
                    vm.Username   = RepositoryOwner;
                    vm.Repository = RepositoryName;
                    vm.Branch     = Commit.Sha;
                    vm.Filename   = x.Filename;
                    ShowViewModel(vm);
                }
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var forceCacheInvalidation = t as bool?;
                var t1 = this.RequestModel(_applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Commits[Node].Get(), forceCacheInvalidation, response => Commit = response.Data);
                Comments.SimpleCollectionLoad(_applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Commits[Node].Comments.GetAll(), forceCacheInvalidation).FireAndForget();
                return(t1);
            });
        }
Beispiel #3
0
        protected BaseEventsViewModel(IApplicationService applicationService)
        {
            ApplicationService = applicationService;
            var events = new ReactiveList <EventModel>();

            Events           = events.CreateDerivedCollection(CreateEventTextBlocks);
            ReportRepository = true;

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <EventModel.RepoModel>().Subscribe(x =>
            {
                var repoId         = new RepositoryIdentifier(x.Name);
                var vm             = CreateViewModel <RepositoryViewModel>();
                vm.RepositoryOwner = repoId.Owner;
                vm.RepositoryName  = repoId.Name;
                ShowViewModel(vm);
            });

            GoToGistCommand = ReactiveCommand.Create();
            GoToGistCommand.OfType <EventModel.GistEvent>().Subscribe(x =>
            {
                var vm  = CreateViewModel <GistViewModel>();
                vm.Id   = x.Gist.Id;
                vm.Gist = x.Gist;
                ShowViewModel(vm);
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
                                                          this.RequestModel(CreateRequest(0, 100), t as bool?, response =>
            {
                //this.CreateMore(response, m => { }, events.AddRange);
                events.Reset(response.Data);
            }));
        }
        public TrendingViewModel(INetworkActivityService networkActivity, TrendingRepository trendingRepository)
        {
            SelectedLanguage = _defaultLanguage;

            var repositories = new ReactiveList <TrendingRepositoryViewModel>();

            Repositories = repositories.CreateDerivedCollection(x => x);

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <Octokit.Repository>().Subscribe(x =>
            {
                var vm = CreateViewModel <RepositoryViewModel>();
                vm.RepositoryIdentifier = new BaseRepositoryViewModel.RepositoryIdentifierModel(x.Owner.Login, x.Name);
                ShowViewModel(vm);
            });

            GoToLanguages = ReactiveCommand.Create();
            GoToLanguages.Subscribe(_ =>
            {
                var vm = CreateViewModel <LanguagesViewModel>();
                vm.ExtraLanguages.Add(_defaultLanguage);
                vm.SelectedLanguage = SelectedLanguage;
                vm.WhenAnyValue(x => x.SelectedLanguage).Skip(1).Subscribe(x =>
                {
                    SelectedLanguage = x;
                    vm.DismissCommand.ExecuteIfCan();
                });
                ShowViewModel(vm);
            });

            this.WhenAnyValue(x => x.SelectedLanguage).Skip(1).Subscribe(_ => LoadCommand.ExecuteIfCan());

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                var tempRepos = new List <TrendingRepositoryViewModel>();
                foreach (var t in _times)
                {
                    var language = SelectedLanguage == null ? null : SelectedLanguage.Slug;
                    var repos    = await trendingRepository.GetTrendingRepositories(t.Slug, language);
                    tempRepos.AddRange(repos.Select(x => new TrendingRepositoryViewModel {
                        Repository = x, Time = t.Name
                    }));
                }
                repositories.Reset(tempRepos);
            });

            LoadCommand.TriggerNetworkActivity(networkActivity);
        }
Beispiel #5
0
        protected BaseRepositoriesViewModel()
        {
            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <StumbledRepository>().Subscribe(x =>
            {
                var vm = CreateViewModel <StumbledRepositoryViewModel>();
                vm.RepositoryIdentifier = new BaseRepositoryViewModel.RepositoryIdentifierModel(x.Owner, x.Name);
                ShowViewModel(vm);
            });

            RepositoryCollection = new ReactiveList <StumbledRepository>();
            Repositories         = RepositoryCollection.CreateDerivedCollection(
                x => x,
                x => x.Owner.StartsWith(SearchKeyword ?? string.Empty, System.StringComparison.OrdinalIgnoreCase) ||
                x.Name.StartsWith(SearchKeyword ?? string.Empty, System.StringComparison.OrdinalIgnoreCase),
                signalReset: this.WhenAnyValue(x => x.SearchKeyword));
        }
Beispiel #6
0
        public RepositoriesExploreViewModel(IApplicationService applicationService, INetworkActivityService networkActivityService)
        {
            _applicationService = applicationService;
            Repositories        = new ReactiveList <RepositorySearchModel.RepositoryModel>();

            SearchCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.SearchText).Select(x => !string.IsNullOrEmpty(x)),
                async t =>
            {
                try
                {
                    var request      = applicationService.Client.Repositories.SearchRepositories(new[] { SearchText }, new string[] { });
                    request.UseCache = false;
                    var response     = await applicationService.Client.ExecuteAsync(request);
                    Repositories.Reset(response.Data.Items);
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to search for repositories. Please try again.", e);
                }
            });

            SearchCommand.IsExecuting.Skip(1).Subscribe(x =>
            {
                if (x)
                {
                    networkActivityService.PushNetworkActive();
                }
                else
                {
                    networkActivityService.PopNetworkActive();
                }
            });

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <RepositorySearchModel.RepositoryModel>().Subscribe(x =>
            {
                var vm             = CreateViewModel <RepositoryViewModel>();
                vm.RepositoryOwner = x.Owner.Login;
                vm.RepositoryName  = x.Name;
                ShowViewModel(vm);
            });
        }
        public RepositoriesTrendingViewModel(IApplicationService applicationService, IJsonHttpClientService jsonHttpClient)
        {
            _applicationService = applicationService;
            _jsonHttpClient     = jsonHttpClient;

            Languages    = new ReactiveList <LanguageModel>();
            Repositories = new ReactiveList <RepositoryModel>();

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <RepositoryModel>().Subscribe(x =>
            {
                var vm             = CreateViewModel <RepositoryViewModel>();
                vm.RepositoryOwner = x.Owner;
                vm.RepositoryName  = x.Name;
                ShowViewModel(vm);
            });

            SelectedTime     = Times[0];
            SelectedLanguage = _defaultLanguage;
            GetLanguages().FireAndForget();

            this.WhenAnyValue(x => x.SelectedTime, x => x.SelectedLanguage, (x, y) => Unit.Default)
            .Skip(1).Subscribe(_ => LoadCommand.ExecuteIfCan());

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                var query = "?";
                if (SelectedLanguage != null && SelectedLanguage.Slug != null)
                {
                    query += string.Format("language={0}&", SelectedLanguage.Slug);
                }
                if (SelectedTime != null && SelectedTime.Slug != null)
                {
                    query += string.Format("since={0}", SelectedTime.Slug);
                }

                var repos = await _jsonHttpClient.Get <List <RepositoryModel> >(TrendingUrl + query);
                Repositories.Reset(repos);
            });
        }
Beispiel #8
0
        private TextBlock CreateRepositoryTextBlock(EventModel.RepoModel repoModel)
        {
            //Most likely indicates a deleted repository
            if (repoModel == null)
            {
                return(new TextBlock("Unknown Repository"));
            }
            if (repoModel.Name == null)
            {
                return(new TextBlock("<Deleted Repository>"));
            }

            var repoSplit = repoModel.Name.Split('/');

            if (repoSplit.Length < 2)
            {
                return(new TextBlock(repoModel.Name));
            }

//            var repoOwner = repoSplit[0];
//            var repoName = repoSplit[1];
            return(new AnchorBlock(repoModel.Name, () => GoToRepositoryCommand.Execute(repoModel)));
        }
Beispiel #9
0
        public MenuViewModel(IApplicationService applicationService, IAccountsService accountsService)
            : base(accountsService)
        {
            _applicationService = applicationService;

            GoToNotificationsCommand = ReactiveCommand.Create();
            GoToNotificationsCommand.Subscribe(_ =>
            {
                var vm = CreateViewModel <NotificationsViewModel>();
                ShowViewModel(vm);
            });

            GoToAccountsCommand = ReactiveCommand.Create();
            GoToAccountsCommand.Subscribe(_ => CreateAndShowViewModel <AccountsViewModel>());

            GoToProfileCommand = ReactiveCommand.Create();
            GoToProfileCommand.Subscribe(_ =>
            {
                var vm      = CreateViewModel <UserViewModel>();
                vm.Username = Account.Username;
                ShowViewModel(vm);
            });

            GoToMyIssuesCommand = ReactiveCommand.Create();
            GoToMyIssuesCommand.Subscribe(_ => CreateAndShowViewModel <MyIssuesViewModel>());

            GoToUpgradesCommand = ReactiveCommand.Create();
            GoToUpgradesCommand.Subscribe(_ => CreateAndShowViewModel <UpgradesViewModel>());

            GoToAboutCommand = ReactiveCommand.Create();
            GoToAboutCommand.Subscribe(_ => CreateAndShowViewModel <AboutViewModel>());

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <RepositoryIdentifier>().Subscribe(x =>
            {
                var vm             = CreateViewModel <RepositoryViewModel>();
                vm.RepositoryOwner = x.Owner;
                vm.RepositoryName  = x.Name;
                ShowViewModel(vm);
            });

            GoToSettingsCommand = ReactiveCommand.Create();
            GoToSettingsCommand.Subscribe(_ => CreateAndShowViewModel <SettingsViewModel>());

            GoToNewsCommand = ReactiveCommand.Create();
            GoToNewsCommand.Subscribe(_ => CreateAndShowViewModel <NewsViewModel>());

            GoToOrganizationsCommand = ReactiveCommand.Create();
            GoToOrganizationsCommand.Subscribe(_ =>
            {
                var vm      = CreateViewModel <OrganizationsViewModel>();
                vm.Username = Account.Username;
                ShowViewModel(vm);
            });

            GoToTrendingRepositoriesCommand = ReactiveCommand.Create();
            GoToTrendingRepositoriesCommand.Subscribe(_ => CreateAndShowViewModel <RepositoriesTrendingViewModel>());

            GoToExploreRepositoriesCommand = ReactiveCommand.Create();
            GoToExploreRepositoriesCommand.Subscribe(_ => CreateAndShowViewModel <RepositoriesExploreViewModel>());

            GoToOrganizationEventsCommand = ReactiveCommand.Create();
            GoToOrganizationEventsCommand.OfType <string>().Subscribe(name =>
            {
                var vm      = CreateViewModel <UserEventsViewModel>();
                vm.Username = name;
                ShowViewModel(vm);
            });

            GoToOrganizationCommand = ReactiveCommand.Create();
            GoToOrganizationCommand.OfType <string>().Subscribe(name =>
            {
                var vm      = CreateViewModel <OrganizationViewModel>();
                vm.Username = name;
                ShowViewModel(vm);
            });

            GoToOwnedRepositoriesCommand = ReactiveCommand.Create();
            GoToOwnedRepositoriesCommand.Subscribe(_ =>
            {
                var vm      = CreateViewModel <UserRepositoriesViewModel>();
                vm.Username = Account.Username;
                ShowViewModel(vm);
            });

            GoToStarredRepositoriesCommand = ReactiveCommand.Create().WithSubscription(
                _ => CreateAndShowViewModel <RepositoriesStarredViewModel>());

            GoToPublicGistsCommand = ReactiveCommand.Create().WithSubscription(
                _ => CreateAndShowViewModel <PublicGistsViewModel>());

            GoToStarredGistsCommand = ReactiveCommand.Create().WithSubscription(
                _ => CreateAndShowViewModel <StarredGistsViewModel>());

            GoToMyGistsCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm      = CreateViewModel <UserGistsViewModel>();
                vm.Username = Account.Username;
                ShowViewModel(vm);
            });

            GoToMyEvents = ReactiveCommand.Create();
            GoToMyEvents.Subscribe(_ =>
            {
                var vm      = CreateViewModel <UserEventsViewModel>();
                vm.Username = Account.Username;
                ShowViewModel(vm);
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(_ =>
            {
                var notificationRequest = applicationService.Client.Notifications.GetAll();
                notificationRequest.RequestFromCache = false;
                notificationRequest.CheckIfModified  = false;

                var task2 = applicationService.Client.ExecuteAsync(notificationRequest)
                            .ContinueWith(t => Notifications = t.Result.Data.Count, TaskScheduler.FromCurrentSynchronizationContext());

                var task3 = applicationService.Client.ExecuteAsync(applicationService.Client.AuthenticatedUser.GetOrganizations())
                            .ContinueWith(t => Organizations = t.Result.Data.Select(y => y.Login).ToList(), TaskScheduler.FromCurrentSynchronizationContext());

                return(Task.WhenAll(task2, task3));
            });
        }
        public ProfileViewModel(IApplicationService applicationService, INetworkActivityService networkActivity, IFeaturesService featuresService)
        {
            StumbleHistory       = new ReactiveList <StumbledRepository>();
            GoToInterestsCommand = ReactiveCommand.Create();
            Username             = applicationService.Account.Username;

            Action updateStumbled = () =>
            {
                var stumbledRepositories = applicationService.Account.StumbledRepositories.Count();
                Interests      = applicationService.Account.Interests.Count();
                Likes          = applicationService.Account.StumbledRepositories.LikedRepositories();
                Dislikes       = applicationService.Account.StumbledRepositories.DislikedRepositories();
                HasMoreHistory = stumbledRepositories > 30;
                if (stumbledRepositories != StumbledRepositories)
                {
                    StumbledRepositories = stumbledRepositories;
                    StumbleHistory.Reset(applicationService.Account.StumbledRepositories.Query.OrderByDescending(x => x.CreatedAt).Take(30));
                }
            };

            this.WhenActivated(d =>
            {
                if (applicationService.Account != null)
                {
                    updateStumbled();

                    d(applicationService.RepositoryAdded
                      .Buffer(TimeSpan.FromSeconds(5))
                      .Where(x => x.Count > 0)
                      .ObserveOn(SynchronizationContext.Current)
                      .Subscribe(x => updateStumbled()));
                }

                CanPurchase = !featuresService.ProEditionEnabled;
            });

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <StumbledRepository>().Subscribe(x =>
            {
                var vm = CreateViewModel <RepositoryViewModel>();
                vm.RepositoryIdentifier = new BaseRepositoryViewModel.RepositoryIdentifierModel(x.Owner, x.Name);
                ShowViewModel(vm);
            });

            GoToPurchaseCommand = ReactiveCommand.Create();
            GoToPurchaseCommand.Subscribe(_ => CreateAndShowViewModel <PurchaseProViewModel>());

            GoToHistoryCommand = ReactiveCommand.Create();
            GoToHistoryCommand.Subscribe(_ => CreateAndShowViewModel <HistoryViewModel>());

            GoToLikesCommand = ReactiveCommand.Create();
            GoToLikesCommand.Subscribe(_ => CreateAndShowViewModel <LikedRepositoriesViewModel>());

            GoToDislikesCommand = ReactiveCommand.Create();
            GoToDislikesCommand.Subscribe(_ => CreateAndShowViewModel <DislikedRepositoriesViewModel>());

            GoToSettingsCommand = ReactiveCommand.Create();
            GoToSettingsCommand.Subscribe(_ => CreateAndShowViewModel <SettingsViewModel>());


            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                User = await applicationService.Client.User.Current();
            });

            LoadCommand.TriggerNetworkActivity(networkActivity);
        }
Beispiel #11
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);
            });
        }
Beispiel #12
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);
            });
        }
Beispiel #13
0
        public ChangesetViewModel(IApplicationService applicationService, IActionMenuService actionMenuService)
        {
            _applicationService = applicationService;

            Title = "Commit";

            GoToUrlCommand = this.CreateUrlCommand();

            Comments = new ReactiveList <CommentModel>();

            var goToUrlCommand = this.CreateUrlCommand();

            GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Commit).Select(x => x != null));
            GoToHtmlUrlCommand.Select(x => Commit.HtmlUrl).Subscribe(goToUrlCommand.ExecuteIfCan);

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

            GoToCommentCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = CreateViewModel <CommitCommentViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.Node            = Node;
                vm.CommentAdded.Subscribe(Comments.Add);
                ShowViewModel(vm);
            });

            GoToFileCommand = ReactiveCommand.Create();
            GoToFileCommand.OfType <CommitModel.CommitFileModel>().Subscribe(x =>
            {
                if (x.Patch == null)
                {
                    var vm             = CreateViewModel <SourceViewModel>();
                    vm.Branch          = Commit.Sha;
                    vm.RepositoryOwner = RepositoryOwner;
                    vm.RepositoryName  = RepositoryName;
//                    vm.Items = new []
//                    {
//                        new SourceViewModel.SourceItemModel
//                        {
//                            ForceBinary = true,
//                            GitUrl = x.BlobUrl,
//                            Name = x.Filename,
//                            Path = x.Filename,
//                            HtmlUrl = x.BlobUrl
//                        }
//                    };
//                    vm.CurrentItemIndex = 0;
                    ShowViewModel(vm);
                }
                else
                {
                    var vm        = CreateViewModel <ChangesetDiffViewModel>();
                    vm.Username   = RepositoryOwner;
                    vm.Repository = RepositoryName;
                    vm.Branch     = Commit.Sha;
                    vm.Filename   = x.Filename;
                    ShowViewModel(vm);
                }
            });

            var copyShaCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Commit).Select(x => x != null))
                                 .WithSubscription(x => actionMenuService.SendToPasteBoard(this.Commit.Sha));

            var shareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Commit).Select(x => x != null))
                               .WithSubscription(x => actionMenuService.ShareUrl(this.Commit.HtmlUrl));

            ShowMenuCommand = ReactiveCommand.CreateAsyncTask(_ =>
            {
                var menu = actionMenuService.Create(Title);
                menu.AddButton("Add Comment", GoToCommentCommand);
                menu.AddButton("Copy SHA", copyShaCommand);
                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.Users[RepositoryOwner].Repositories[RepositoryName].Commits[Node].Get(), forceCacheInvalidation, response => Commit = response.Data);
                Comments.SimpleCollectionLoad(_applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Commits[Node].Comments.GetAll(), forceCacheInvalidation).FireAndForget();
                return(t1);
            });
        }
Beispiel #14
0
        public MenuViewModel(ISessionService sessionService, IAccountsRepository accountsService)
        {
            _sessionService = sessionService;

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

            GoToNotificationsCommand = ReactiveCommand.Create().WithSubscription(_ => {
                var vm = this.CreateViewModel <NotificationsViewModel>();
                vm.NotificationCount.Subscribe(x => Notifications = x);
                NavigateTo(vm);
            });

            GoToAccountsCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <AccountsViewModel>()));

            GoToProfileCommand = ReactiveCommand.Create();
            GoToProfileCommand
            .Select(_ => this.CreateViewModel <UserViewModel>())
            .Select(x => x.Init(Account.Username))
            .Subscribe(NavigateTo);

            GoToMyIssuesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <MyIssuesViewModel>()));

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <RepositoryIdentifier>()
            .Select(x => this.CreateViewModel <RepositoryViewModel>().Init(x.Owner, x.Name))
            .Subscribe(NavigateTo);

            GoToSettingsCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <SettingsViewModel>()));

            GoToNewsCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                        NavigateTo(this.CreateViewModel <NewsViewModel>()));

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

            GoToTrendingRepositoriesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                                        NavigateTo(this.CreateViewModel <RepositoriesTrendingViewModel>()));

            GoToExploreRepositoriesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                                       NavigateTo(this.CreateViewModel <ExploreViewModel>()));

            GoToOrganizationEventsCommand = ReactiveCommand.Create();
            GoToOrganizationEventsCommand
            .OfType <Octokit.Organization>()
            .Select(x => this.CreateViewModel <UserEventsViewModel>().Init(x.Login))
            .Subscribe(NavigateTo);

            GoToOrganizationCommand = ReactiveCommand.Create();
            GoToOrganizationCommand
            .OfType <Octokit.Organization>()
            .Select(x => this.CreateViewModel <OrganizationViewModel>().Init(x.Login))
            .Subscribe(NavigateTo);

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

            GoToStarredRepositoriesCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <RepositoriesStarredViewModel>()));

            GoToWatchedRepositoriesCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <RepositoriesWatchedViewModel>()));

            GoToPublicGistsCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <PublicGistsViewModel>()));

            GoToStarredGistsCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <StarredGistsViewModel>()));

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

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

            GoToFeedbackCommand = ReactiveCommand.Create();
            GoToFeedbackCommand.Subscribe(_ => {
                var vm = sessionService.Account.IsEnterprise
                    ? (IBaseViewModel)this.CreateViewModel <EnterpriseSupportViewModel>()
                    : this.CreateViewModel <SupportViewModel>();
                NavigateTo(vm);
            });

            DeletePinnedRepositoryCommand = ReactiveCommand.Create();

            DeletePinnedRepositoryCommand.OfType <PinnedRepository>()
            .Subscribe(x => {
                sessionService.Account.PinnnedRepositories.Remove(x);
                accountsService.Update(sessionService.Account);
            });

            ActivateCommand = ReactiveCommand.Create();
            ActivateCommand.Subscribe(x => {
                var startupViewModel            = sessionService.StartupViewModel;
                sessionService.StartupViewModel = null;
                if (startupViewModel != null)
                {
                    NavigateTo(startupViewModel);
                }
                else
                {
                    GoToDefaultTopView.ExecuteIfCan();
                }
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(_ => {
//                var notifications = sessionService.GitHubClient.Notification.GetAllForCurrent();
//                notifications.ToBackground(x => Notifications = x.Count);
////
//                var organizations = sessionService.GitHubClient.Organization.GetAllForCurrent();
//                organizations.ToBackground(x => Organizations = x);
//
//                return Task.WhenAll(notifications, organizations);

                return(Task.FromResult(Unit.Default));
            });
        }
Beispiel #15
0
        private EventBlock CreateEventTextBlocks(EventModel eventModel)
        {
            var eventBlock = new EventBlock();
            var repoId     = eventModel.Repo != null ? new RepositoryIdentifier(eventModel.Repo.Name) : new RepositoryIdentifier();
            var username   = eventModel.Actor != null ? eventModel.Actor.Login : null;

            // Insert the actor
            eventBlock.Header.Add(new AnchorBlock(username, () => GoToUser(username)));

            var commitCommentEvent = eventModel.PayloadObject as EventModel.CommitCommentEvent;

            if (commitCommentEvent != null)
            {
                var node = commitCommentEvent.Comment.CommitId.Substring(0, commitCommentEvent.Comment.CommitId.Length > 6 ? 6 : commitCommentEvent.Comment.CommitId.Length);
                eventBlock.Tapped = () => GoToChangeset(repoId, commitCommentEvent.Comment.CommitId);
                eventBlock.Header.Add(new TextBlock(" commented on commit "));
                eventBlock.Header.Add(new AnchorBlock(node, eventBlock.Tapped));

                if (ReportRepository)
                {
                    eventBlock.Header.Add(new TextBlock(" in "));
                    eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                }

                eventBlock.Body.Add(new TextBlock(commitCommentEvent.Comment.Body));
                return(eventBlock);
            }

            var createEvent = eventModel.PayloadObject as EventModel.CreateEvent;

            if (createEvent != null)
            {
                if (createEvent.RefType.Equals("repository"))
                {
                    if (ReportRepository)
                    {
                        eventBlock.Tapped = () => GoToRepositoryCommand.Execute(eventModel.Repo);
                        eventBlock.Header.Add(new TextBlock(" created repository "));
                        eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                    }
                    else
                    {
                        eventBlock.Header.Add(new TextBlock(" created this repository"));
                    }
                }
                else if (createEvent.RefType.Equals("branch"))
                {
                    eventBlock.Tapped = () => GoToBranches(repoId);
                    eventBlock.Header.Add(new TextBlock(" created branch "));
                    eventBlock.Header.Add(new AnchorBlock(createEvent.Ref, eventBlock.Tapped));

                    if (ReportRepository)
                    {
                        eventBlock.Header.Add(new TextBlock(" in "));
                        eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                    }
                }
                else if (createEvent.RefType.Equals("tag"))
                {
                    eventBlock.Tapped = () => GoToTags(eventModel.Repo);
                    eventBlock.Header.Add(new TextBlock(" created tag "));
                    eventBlock.Header.Add(new AnchorBlock(createEvent.Ref, eventBlock.Tapped));

                    if (ReportRepository)
                    {
                        eventBlock.Header.Add(new TextBlock(" in "));
                        eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                    }
                }
            }


            var deleteEvent = eventModel.PayloadObject as EventModel.DeleteEvent;

            if (deleteEvent != null)
            {
                if (deleteEvent.RefType.Equals("branch"))
                {
                    eventBlock.Tapped = () => GoToBranches(repoId);
                    eventBlock.Header.Add(new TextBlock(" deleted branch "));
                }
                else if (deleteEvent.RefType.Equals("tag"))
                {
                    eventBlock.Tapped = () => GoToTags(eventModel.Repo);
                    eventBlock.Header.Add(new TextBlock(" deleted tag "));
                }
                else
                {
                    return(null);
                }

                eventBlock.Header.Add(new AnchorBlock(deleteEvent.Ref, eventBlock.Tapped));
                if (!ReportRepository)
                {
                    return(eventBlock);
                }
                eventBlock.Header.Add(new TextBlock(" in "));
                eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                return(eventBlock);
            }


            if (eventModel.PayloadObject is EventModel.DownloadEvent)
            {
                // Don't show the download event for now...
                return(null);
            }


            var followEvent = eventModel.PayloadObject as EventModel.FollowEvent;

            if (followEvent != null)
            {
                eventBlock.Tapped = () => GoToUser(followEvent.Target.Login);
                eventBlock.Header.Add(new TextBlock(" started following "));
                eventBlock.Header.Add(new AnchorBlock(followEvent.Target.Login, eventBlock.Tapped));
                return(eventBlock);
            }

            /*
             * FORK EVENT
             */
            else if (eventModel.PayloadObject is EventModel.ForkEvent)
            {
                var forkEvent  = (EventModel.ForkEvent)eventModel.PayloadObject;
                var forkedRepo = new EventModel.RepoModel {
                    Id = forkEvent.Forkee.Id, Name = forkEvent.Forkee.FullName, Url = forkEvent.Forkee.Url
                };
                eventBlock.Tapped = () => GoToRepositoryCommand.Execute(forkedRepo);
                eventBlock.Header.Add(new TextBlock(" forked "));
                eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                eventBlock.Header.Add(new TextBlock(" to "));
                eventBlock.Header.Add(CreateRepositoryTextBlock(forkedRepo));
            }

            /*
             * FORK APPLY EVENT
             */
            else if (eventModel.PayloadObject is EventModel.ForkApplyEvent)
            {
                var forkEvent = (EventModel.ForkApplyEvent)eventModel.PayloadObject;
                eventBlock.Tapped = () => GoToRepositoryCommand.Execute(eventModel.Repo);
                eventBlock.Header.Add(new TextBlock(" applied fork to "));
                eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                eventBlock.Header.Add(new TextBlock(" on branch "));
                eventBlock.Header.Add(new AnchorBlock(forkEvent.Head, () => GoToBranches(repoId)));
            }

            /*
             * GIST EVENT
             */
            else if (eventModel.PayloadObject is EventModel.GistEvent)
            {
                var gistEvent = (EventModel.GistEvent)eventModel.PayloadObject;
                eventBlock.Tapped = () => GoToGistCommand.Execute(gistEvent);

                if (string.Equals(gistEvent.Action, "create", StringComparison.OrdinalIgnoreCase))
                {
                    eventBlock.Header.Add(new TextBlock(" created Gist #"));
                }
                else if (string.Equals(gistEvent.Action, "update", StringComparison.OrdinalIgnoreCase))
                {
                    eventBlock.Header.Add(new TextBlock(" updated Gist #"));
                }
                else if (string.Equals(gistEvent.Action, "fork", StringComparison.OrdinalIgnoreCase))
                {
                    eventBlock.Header.Add(new TextBlock(" forked Gist #"));
                }

                eventBlock.Header.Add(new AnchorBlock(gistEvent.Gist.Id, eventBlock.Tapped));
                eventBlock.Body.Add(new TextBlock(gistEvent.Gist.Description.Replace('\n', ' ').Replace("\r", "").Trim()));
            }

            /*
             * GOLLUM EVENT (WIKI)
             */
            else if (eventModel.PayloadObject is EventModel.GollumEvent)
            {
                var gollumEvent = eventModel.PayloadObject as EventModel.GollumEvent;
                eventBlock.Header.Add(new TextBlock(" modified the wiki in "));
                eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));

                if (gollumEvent != null && gollumEvent.Pages != null)
                {
                    foreach (var page in gollumEvent.Pages)
                    {
                        var p = page;
                        eventBlock.Body.Add(new AnchorBlock(page.PageName, () => GoToUrlCommand.Execute(p.HtmlUrl)));
                        eventBlock.Body.Add(new TextBlock(" - " + page.Action + "\n"));
                    }

                    eventBlock.Multilined = true;
                }
            }

            /*
             * ISSUE COMMENT EVENT
             */
            else if (eventModel.PayloadObject is EventModel.IssueCommentEvent)
            {
                var commentEvent = (EventModel.IssueCommentEvent)eventModel.PayloadObject;

                if (commentEvent.Issue.PullRequest != null && !string.IsNullOrEmpty(commentEvent.Issue.PullRequest.HtmlUrl))
                {
                    eventBlock.Tapped = () => GoToPullRequest(repoId, commentEvent.Issue.Number);
                    eventBlock.Header.Add(new TextBlock(" commented on pull request "));
                }
                else
                {
                    eventBlock.Tapped = () => GoToIssue(repoId, commentEvent.Issue.Number);
                    eventBlock.Header.Add(new TextBlock(" commented on issue "));
                }

                eventBlock.Header.Add(new AnchorBlock("#" + commentEvent.Issue.Number, eventBlock.Tapped));
                eventBlock.Header.Add(new TextBlock(" in "));
                eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));

                eventBlock.Body.Add(new TextBlock(commentEvent.Comment.Body.Replace('\n', ' ').Replace("\r", "").Trim()));
            }

            /*
             * ISSUES EVENT
             */
            else if (eventModel.PayloadObject is EventModel.IssuesEvent)
            {
                var issueEvent = (EventModel.IssuesEvent)eventModel.PayloadObject;
                eventBlock.Tapped = () => GoToIssue(repoId, issueEvent.Issue.Number);

                if (string.Equals(issueEvent.Action, "opened", StringComparison.OrdinalIgnoreCase))
                {
                    eventBlock.Header.Add(new TextBlock(" opened issue "));
                }
                else if (string.Equals(issueEvent.Action, "closed", StringComparison.OrdinalIgnoreCase))
                {
                    eventBlock.Header.Add(new TextBlock(" closed issue "));
                }
                else if (string.Equals(issueEvent.Action, "reopened", StringComparison.OrdinalIgnoreCase))
                {
                    eventBlock.Header.Add(new TextBlock(" reopened issue "));
                }

                eventBlock.Header.Add(new AnchorBlock("#" + issueEvent.Issue.Number, eventBlock.Tapped));
                eventBlock.Header.Add(new TextBlock(" in "));
                eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                eventBlock.Body.Add(new TextBlock(issueEvent.Issue.Title.Trim()));
            }

            /*
             * MEMBER EVENT
             */
            else if (eventModel.PayloadObject is EventModel.MemberEvent)
            {
                var memberEvent = (EventModel.MemberEvent)eventModel.PayloadObject;
                eventBlock.Tapped = () => GoToRepositoryCommand.Execute(eventModel.Repo);

                if (memberEvent.Action.Equals("added"))
                {
                    eventBlock.Header.Add(new TextBlock(" added as a collaborator"));
                }
                else if (memberEvent.Action.Equals("removed"))
                {
                    eventBlock.Header.Add(new TextBlock(" removed as a collaborator"));
                }

                if (ReportRepository)
                {
                    eventBlock.Header.Add(new TextBlock(" to "));
                    eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                }
            }

            /*
             * PUBLIC EVENT
             */
            else if (eventModel.PayloadObject is EventModel.PublicEvent)
            {
                eventBlock.Tapped = () => GoToRepositoryCommand.Execute(eventModel.Repo);
                if (ReportRepository)
                {
                    eventBlock.Header.Add(new TextBlock(" has open sourced "));
                    eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                }
                else
                {
                    eventBlock.Header.Add(new TextBlock(" has been open sourced this repository!"));
                }
            }

            /*
             * PULL REQUEST EVENT
             */
            else if (eventModel.PayloadObject is EventModel.PullRequestEvent)
            {
                var pullEvent = (EventModel.PullRequestEvent)eventModel.PayloadObject;
                eventBlock.Tapped = () => GoToPullRequest(repoId, pullEvent.Number);

                if (pullEvent.Action.Equals("closed"))
                {
                    eventBlock.Header.Add(new TextBlock(" closed pull request "));
                }
                else if (pullEvent.Action.Equals("opened"))
                {
                    eventBlock.Header.Add(new TextBlock(" opened pull request "));
                }
                else if (pullEvent.Action.Equals("synchronize"))
                {
                    eventBlock.Header.Add(new TextBlock(" synchronized pull request "));
                }
                else if (pullEvent.Action.Equals("reopened"))
                {
                    eventBlock.Header.Add(new TextBlock(" reopened pull request "));
                }

                eventBlock.Header.Add(new AnchorBlock("#" + pullEvent.PullRequest.Number, eventBlock.Tapped));
                eventBlock.Header.Add(new TextBlock(" in "));
                eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));

                eventBlock.Body.Add(new TextBlock(pullEvent.PullRequest.Title));
            }

            /*
             * PULL REQUEST REVIEW COMMENT EVENT
             */
            else if (eventModel.PayloadObject is EventModel.PullRequestReviewCommentEvent)
            {
                var commentEvent = (EventModel.PullRequestReviewCommentEvent)eventModel.PayloadObject;
                eventBlock.Tapped = () => GoToPullRequests(repoId);
                eventBlock.Header.Add(new TextBlock(" commented on pull request "));
                if (ReportRepository)
                {
                    eventBlock.Header.Add(new TextBlock(" in "));
                    eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                }

                eventBlock.Body.Add(new TextBlock(commentEvent.Comment.Body.Replace('\n', ' ').Replace("\r", "").Trim()));
            }

            /*
             * PUSH EVENT
             */
            else if (eventModel.PayloadObject is EventModel.PushEvent)
            {
                var pushEvent = (EventModel.PushEvent)eventModel.PayloadObject;

                string branchRef = null;
                if (!string.IsNullOrEmpty(pushEvent.Ref))
                {
                    var lastSlash = pushEvent.Ref.LastIndexOf("/", StringComparison.Ordinal) + 1;
                    branchRef = pushEvent.Ref.Substring(lastSlash);
                }

                if (eventModel.Repo != null)
                {
                    eventBlock.Tapped = () => GoToCommits(eventModel.Repo, pushEvent.Ref);
                }

                eventBlock.Header.Add(new TextBlock(" pushed to "));
                if (branchRef != null)
                {
                    eventBlock.Header.Add(new AnchorBlock(branchRef, () => GoToBranches(repoId)));
                }

                if (ReportRepository)
                {
                    eventBlock.Header.Add(new TextBlock(" at "));
                    eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                }

                if (pushEvent.Commits != null)
                {
                    foreach (var commit in pushEvent.Commits)
                    {
                        var desc         = (commit.Message ?? "");
                        var sha          = commit.Sha;
                        var firstNewLine = desc.IndexOf("\n");
                        if (firstNewLine <= 0)
                        {
                            firstNewLine = desc.Length;
                        }

                        desc = desc.Substring(0, firstNewLine);
                        var shortSha = commit.Sha;
                        if (shortSha.Length > 6)
                        {
                            shortSha = shortSha.Substring(0, 6);
                        }

                        eventBlock.Body.Add(new AnchorBlock(shortSha, () => GoToChangeset(repoId, sha)));
                        eventBlock.Body.Add(new TextBlock(" - " + desc + "\n"));
                        eventBlock.Multilined = true;
                    }
                }
            }


            var teamAddEvent = eventModel.PayloadObject as EventModel.TeamAddEvent;

            if (teamAddEvent != null)
            {
                eventBlock.Header.Add(new TextBlock(" added "));

                if (teamAddEvent.User != null)
                {
                    eventBlock.Header.Add(new AnchorBlock(teamAddEvent.User.Login, () => GoToUser(teamAddEvent.User.Login)));
                }
                else if (teamAddEvent.Repo != null)
                {
                    eventBlock.Header.Add(CreateRepositoryTextBlock(new EventModel.RepoModel {
                        Id = teamAddEvent.Repo.Id, Name = teamAddEvent.Repo.FullName, Url = teamAddEvent.Repo.Url
                    }));
                }
                else
                {
                    return(null);
                }

                if (teamAddEvent.Team == null)
                {
                    return(eventBlock);
                }
                eventBlock.Header.Add(new TextBlock(" to team "));
                eventBlock.Header.Add(new AnchorBlock(teamAddEvent.Team.Name, () => { }));
                return(eventBlock);
            }


            var watchEvent = eventModel.PayloadObject as EventModel.WatchEvent;

            if (watchEvent != null)
            {
                eventBlock.Tapped = () => GoToRepositoryCommand.Execute(eventModel.Repo);
                eventBlock.Header.Add(watchEvent.Action.Equals("started") ?
                                      new TextBlock(" starred ") : new TextBlock(" unstarred "));
                eventBlock.Header.Add(CreateRepositoryTextBlock(eventModel.Repo));
                return(eventBlock);
            }

            var releaseEvent = eventModel.PayloadObject as EventModel.ReleaseEvent;

            if (releaseEvent != null)
            {
                eventBlock.Tapped = () => GoToUrlCommand.Execute(releaseEvent.Release.HtmlUrl);
                eventBlock.Header.Add(new TextBlock(" " + releaseEvent.Action + " release " + releaseEvent.Release.Name));
                return(eventBlock);
            }

            return(eventBlock);
        }
Beispiel #16
0
        public CommitViewModel(
            string username, string repository, string node, bool showRepository = false,
            IApplicationService applicationService = null, IActionMenuService actionMenuService = null,
            IAlertDialogService alertDialogService = null)
        {
            _applicationService = applicationService = applicationService ?? Locator.Current.GetService <IApplicationService>();
            actionMenuService   = actionMenuService ?? Locator.Current.GetService <IActionMenuService>();
            alertDialogService  = alertDialogService ?? Locator.Current.GetService <IAlertDialogService>();

            Username       = username;
            Repository     = repository;
            Node           = node;
            ShowRepository = showRepository;

            var shortNode = node.Substring(0, node.Length > 7 ? 7 : node.Length);

            Title = $"Commit {shortNode}";

            var changesetFiles =
                this.WhenAnyValue(x => x.Changeset)
                .Where(x => x != null)
                .Select(x => x.Files ?? Enumerable.Empty <Client.V1.ChangesetFile>());

            changesetFiles
            .Select(x => x.Count(y => y.Type == "added"))
            .ToProperty(this, x => x.DiffAdditions, out _diffAdditions);

            changesetFiles
            .Select(x => x.Count(y => y.Type == "removed"))
            .ToProperty(this, x => x.DiffDeletions, out _diffDeletions);

            changesetFiles
            .Select(x => x.Count(y => y.Type != "added" && y.Type != "removed"))
            .ToProperty(this, x => x.DiffModifications, out _diffModifications);

            Comments = _comments.CreateDerivedCollection(comment =>
            {
                var name   = comment.User.DisplayName ?? comment.User.Username;
                var avatar = new Avatar(comment.User.Links?.Avatar?.Href);
                return(new CommentItemViewModel(name, avatar, comment.CreatedOn.Humanize(), comment.Content.Raw));
            }, x => x.Inline == null);

            GoToUserCommand = ReactiveCommand.Create <string>(user => NavigateTo(new UserViewModel(user)));

            GoToRepositoryCommand
            .Select(_ => new RepositoryViewModel(username, repository))
            .Subscribe(NavigateTo);

            GoToAddedFiles = ReactiveCommand.Create(
                () => { },
                this.WhenAnyValue(x => x.DiffAdditions).Select(x => x > 0));

            GoToRemovedFiles = ReactiveCommand.Create(
                () => { },
                this.WhenAnyValue(x => x.DiffDeletions).Select(x => x > 0));

            GoToModifiedFiles = ReactiveCommand.Create(
                () => { },
                this.WhenAnyValue(x => x.DiffModifications).Select(x => x > 0));

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

            ShowMenuCommand = ReactiveCommand.Create <object>(sender =>
            {
                var uri  = new Uri($"https://bitbucket.org/{username}/{repository}/commits/{node}");
                var menu = actionMenuService.Create();
                menu.AddButton("Add Comment", _ => AddCommentCommand.ExecuteNow());
                menu.AddButton("Copy SHA", _ => actionMenuService.SendToPasteBoard(node));
                menu.AddButton("Share", x => actionMenuService.ShareUrl(x, uri));
                menu.AddButton("Show In Bitbucket", _ => NavigateTo(new WebBrowserViewModel(uri.AbsoluteUri)));
                menu.Show(sender);
            }, canShowMenu);

            ToggleApproveButton = ReactiveCommand.CreateFromTask(async _ =>
            {
                if (Approved)
                {
                    await applicationService.Client.Commits.Unapprove(username, repository, node);
                }
                else
                {
                    await applicationService.Client.Commits.Approve(username, repository, node);
                }

                var shouldBe        = !Approved;
                var commit          = await applicationService.Client.Commits.Get(username, repository, node);
                var currentUsername = applicationService.Account.Username;
                var me = commit.Participants.FirstOrDefault(
                    y => string.Equals(currentUsername, y?.User?.Username, StringComparison.OrdinalIgnoreCase));
                if (me != null)
                {
                    me.Approved = shouldBe;
                }
                Commit = commit;
            });

            ToggleApproveButton
            .ThrownExceptions
            .Subscribe(x => alertDialogService.Alert("Error", "Unable to approve commit: " + x.Message).ToBackground());

            var commitFiles = new ReactiveList <Client.V1.ChangesetFile>();

            CommitFiles = commitFiles.CreateDerivedCollection(x =>
            {
                var vm = new CommitFileItemViewModel(username, repository, node, Changeset.Parents.FirstOrDefault(), x);
                vm.GoToCommand
                .Select(_ => new ChangesetDiffViewModel(username, repository, node, x))
                .Subscribe(NavigateTo);
                return(vm);
            });

            this.WhenAnyValue(x => x.Commit.Participants)
            .Select(participants =>
            {
                return((participants ?? Enumerable.Empty <CommitParticipant>())
                       .Where(x => x.Approved)
                       .Select(x =>
                {
                    var avatar = new Avatar(x.User?.Links?.Avatar?.Href);
                    var vm = new UserItemViewModel(x.User?.Username, x.User?.DisplayName, avatar);
                    vm.GoToCommand
                    .Select(_ => new UserViewModel(x.User))
                    .Subscribe(NavigateTo);
                    return vm;
                }));
            })
            .Select(x => x.ToArray())
            .ToProperty(this, x => x.Approvals, out _approvals, new UserItemViewModel[0]);

            this.WhenAnyValue(x => x.Changeset)
            .Subscribe(x => commitFiles.Reset(x?.Files ?? Enumerable.Empty <Client.V1.ChangesetFile>()));

            this.WhenAnyValue(x => x.Commit)
            .Subscribe(x =>
            {
                var currentUsername = applicationService.Account.Username;
                Approved            = x?.Participants
                                      ?.FirstOrDefault(y => string.Equals(currentUsername, y?.User?.Username, StringComparison.OrdinalIgnoreCase))
                                      ?.Approved ?? false;
            });

            LoadCommand = ReactiveCommand.CreateFromTask(_ =>
            {
                var commit = applicationService.Client.Commits.Get(username, repository, node)
                             .OnSuccess(x => Commit = x);
                var changeset = applicationService.Client.Commits.GetChangeset(username, repository, node)
                                .OnSuccess(x => Changeset = x);
                applicationService.Client.AllItems(x => x.Commits.GetComments(username, repository, node))
                .ToBackground(_comments.Reset);
                return(Task.WhenAll(commit, changeset));
            });
        }
Beispiel #17
0
        public MenuViewModel(IApplicationService applicationService, IAccountsService accountsService)
        {
            _applicationService = applicationService;
            _accountsService    = accountsService;

            GoToNotificationsCommand = ReactiveCommand.Create();
            GoToNotificationsCommand.Subscribe(_ =>
            {
                var vm = this.CreateViewModel <NotificationsViewModel>();
                NavigateTo(vm);
            });

            GoToAccountsCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <AccountsViewModel>()));

            GoToProfileCommand = ReactiveCommand.Create();
            GoToProfileCommand.Subscribe(_ =>
            {
                var vm      = this.CreateViewModel <UserViewModel>();
                vm.Username = Account.Username;
                NavigateTo(vm);
            });

            GoToMyIssuesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <MyIssuesViewModel>()));

            GoToUpgradesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <UpgradesViewModel>()));

            GoToRepositoryCommand = ReactiveCommand.Create();
            GoToRepositoryCommand.OfType <RepositoryIdentifier>().Subscribe(x =>
            {
                var vm             = this.CreateViewModel <RepositoryViewModel>();
                vm.RepositoryOwner = x.Owner;
                vm.RepositoryName  = x.Name;
                NavigateTo(vm);
            });

            GoToSettingsCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <SettingsViewModel>()));

            GoToNewsCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                        NavigateTo(this.CreateViewModel <NewsViewModel>()));

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

            GoToTrendingRepositoriesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                                        NavigateTo(this.CreateViewModel <RepositoriesTrendingViewModel>()));

            GoToExploreRepositoriesCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                                       NavigateTo(this.CreateViewModel <RepositoriesExploreViewModel>()));

            GoToOrganizationEventsCommand = ReactiveCommand.Create();
            GoToOrganizationEventsCommand.OfType <Octokit.Organization>().Subscribe(x =>
            {
                var vm      = this.CreateViewModel <UserEventsViewModel>();
                vm.Username = x.Login;
                NavigateTo(vm);
            });

            GoToOrganizationCommand = ReactiveCommand.Create();
            GoToOrganizationCommand.OfType <Octokit.Organization>().Subscribe(x =>
            {
                var vm      = this.CreateViewModel <OrganizationViewModel>();
                vm.Username = x.Login;
                NavigateTo(vm);
            });

            GoToOwnedRepositoriesCommand = ReactiveCommand.Create();
            GoToOwnedRepositoriesCommand.Subscribe(_ =>
            {
                var vm      = this.CreateViewModel <UserRepositoriesViewModel>();
                vm.Username = Account.Username;
                NavigateTo(vm);
            });

            GoToStarredRepositoriesCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <RepositoriesStarredViewModel>()));

            GoToWatchedRepositoriesCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <RepositoriesWatchedViewModel>()));

            GoToPublicGistsCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <PublicGistsViewModel>()));

            GoToStarredGistsCommand = ReactiveCommand.Create().WithSubscription(
                _ => NavigateTo(this.CreateViewModel <StarredGistsViewModel>()));

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

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

            GoToFeedbackCommand = ReactiveCommand.Create().WithSubscription(_ =>
                                                                            NavigateTo(this.CreateViewModel <SupportViewModel>()));

            DeletePinnedRepositoryCommand = ReactiveCommand.Create();

            DeletePinnedRepositoryCommand.OfType <PinnedRepository>()
            .Subscribe(x =>
            {
                accountsService.ActiveAccount.PinnnedRepositories.Remove(x);
                accountsService.Update(accountsService.ActiveAccount);
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(_ =>
            {
                var notifications = applicationService.GitHubClient.Notification.GetAllForCurrent();
                notifications.ToBackground(x => Notifications = x.Count);

                var organizations = applicationService.GitHubClient.Organization.GetAllForCurrent();
                organizations.ToBackground(x => Organizations = x);

                return(Task.WhenAll(notifications, organizations));
            });
        }