protected BaseRepositoriesViewModel(IApplicationService applicationService, string filterKey = "RepositoryController")
        {
            ApplicationService  = applicationService;
            ShowRepositoryOwner = true;
            Title = "Repositories";

            var gotoRepository = new Action <RepositoryItemViewModel>(x =>
            {
                var vm             = CreateViewModel <RepositoryViewModel>();
                vm.RepositoryOwner = x.Owner;
                vm.RepositoryName  = x.Name;
                ShowViewModel(vm);
            });

            Repositories = RepositoryCollection.CreateDerivedCollection(
                x => new RepositoryItemViewModel(x.Name, x.Owner.Login, x.Owner.AvatarUrl,
                                                 ShowRepositoryDescription ? x.Description : string.Empty, x.StargazersCount, x.ForksCount,
                                                 ShowRepositoryOwner, gotoRepository),
                filter: x => x.Name.ContainsKeyword(SearchKeyword),
                signalReset: this.WhenAnyValue(x => x.SearchKeyword));

            //Filter = applicationService.Account.Filters.GetFilter<RepositoriesFilterModel>(filterKey);

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

//			_repositories.FilteringFunction = x => Repositories.Filter.Ascending ? x.OrderBy(y => y.Name) : x.OrderByDescending(y => y.Name);
//            _repositories.GroupingFunction = CreateGroupedItems;
        }
        public PullRequestsViewModel(ISessionService sessionService)
        {
            _sessionService = sessionService;
            Title           = "Pull Requests";

            Items = InternalItems.CreateDerivedCollection(x => {
                var vm = new PullRequestItemViewModel(x);
                vm.GoToCommand.Subscribe(_ => {
                    var prViewModel = this.CreateViewModel <PullRequestViewModel>();
                    prViewModel.Init(RepositoryOwner, RepositoryName, x.Number, x);
                    NavigateTo(prViewModel);

                    prViewModel.WhenAnyValue(y => y.Issue.State)
                    .DistinctUntilChanged()
                    .Skip(1)
                    .Subscribe(y => LoadCommand.ExecuteIfCan());
                });
                return(vm);
            },
                                                          filter: x => x.Title.ContainsKeyword(SearchKeyword),
                                                          signalReset: this.WhenAnyValue(x => x.SearchKeyword));

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t => {
                InternalItems.Reset(await RetrievePullRequests());
            });

            this.WhenAnyValue(x => x.SelectedFilter).Skip(1).Subscribe(_ => {
                InternalItems.Clear();
                LoadCommand.ExecuteIfCan();
            });
        }
Exemple #3
0
        public PullRequestsViewModel(IApplicationService applicationService)
        {
            Title = "Pull Requests";

            var pullRequests = new ReactiveList <PullRequestModel>();

            PullRequests = pullRequests.CreateDerivedCollection(
                x => new PullRequestItemViewModel(x, () =>
            {
                var vm             = CreateViewModel <PullRequestViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.Id = (int)x.Number;
                //vm.PullRequest = x.PullRequest;
                //              vm.WhenAnyValue(x => x.PullRequest).Skip(1).Subscribe(x =>
                //              {
                //                    var index = PullRequests.IndexOf(pullRequest);
                //                    if (index < 0) return;
                //                    PullRequests[index] = x;
                //                    PullRequests.Reset();
                //              });
                ShowViewModel(vm);
            }),
                filter: x => x.Title.ContainsKeyword(SearchKeyword),
                signalReset: this.WhenAnyValue(x => x.SearchKeyword));

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var state   = SelectedFilter == 0 ? "open" : "closed";
                var request = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].PullRequests.GetAll(state: state);
                return(pullRequests.SimpleCollectionLoad(request, t as bool?));
            });

            this.WhenAnyValue(x => x.SelectedFilter).Skip(1).Subscribe(_ => LoadCommand.ExecuteIfCan());
        }
Exemple #4
0
        public IssuesViewModel(ISessionService sessionService)
            : base(sessionService)
        {
            _sessionService = sessionService;
            Filter          = new IssuesFilterModel();

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

            this.WhenAnyValue(x => x.Filter).Skip(1).Subscribe(filter => {
                InternalItems.Clear();
                LoadCommand.ExecuteIfCan();
                //CustomFilterEnabled = !(filter == _closedFilter || filter == _openFilter);
            });

            GoToFilterCommand = ReactiveCommand.Create();
            GoToFilterCommand.Subscribe(_ => {
                var vm = this.CreateViewModel <RepositoryIssuesFilterViewModel>();
                vm.Init(RepositoryOwner, RepositoryName, Filter);
                vm.SaveCommand.Subscribe(filter => {
                    Filter          = filter;
                    FilterSelection = IssueFilterSelection.Custom;
                });
                NavigateTo(vm);
            });
        }
Exemple #5
0
        public MyIssuesViewModel(ISessionService sessionService)
            : base(sessionService)
        {
            _sessionService = sessionService;

            Title  = "My Issues";
            Filter = MyIssuesFilterModel.CreateOpenFilter();

            _selectedFilter = this.WhenAnyValue(x => x.Filter)
                              .Select(x =>
            {
                if (x == null || _openFilter.Equals(x))
                {
                    return(0);
                }
                return(_closedFilter.Equals(x) ? 1 : -1);
            })
                              .ToProperty(this, x => x.SelectedFilter);

            this.WhenAnyValue(x => x.Filter).Skip(1).Subscribe(filter => {
                IssuesBacking.Clear();
                LoadCommand.ExecuteIfCan();
                CustomFilterEnabled = !(filter == _closedFilter || filter == _openFilter);
            });

            GoToFilterCommand = ReactiveCommand.Create();
            GoToFilterCommand.Subscribe(_ => {
                var vm = this.CreateViewModel <MyIssuesFilterViewModel>();
                vm.Init(Filter);
                vm.SaveCommand.Subscribe(filter => Filter = filter);
                NavigateTo(vm);
            });
        }
Exemple #6
0
        public NotificationsViewModel(IApplicationService applicationService)
        {
            _applicationService = applicationService;
            Title = "Notifications";

            ReadSelectedCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                if (GroupedNotifications.SelectMany(x => x.Notifications).All(x => !x.IsSelected))
                {
                    applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkAsRead()).ToBackground();
                    _notifications.Clear();
                }
                else
                {
                    var selected = GroupedNotifications.SelectMany(x => x.Notifications)
                                   .Where(x => x.IsSelected && x.Notification.Unread).ToList();

                    var tasks = selected
                                .Select(t => _applicationService.GitHubClient.Notification.MarkAsRead(int.Parse(t.Id)));

                    Task.WhenAll(tasks).ToBackground();

                    foreach (var s in selected)
                    {
                        _notifications.Remove(s.Notification);
                    }
                }
            });

            _notifications.Changed.Select(_ => Unit.Default)
            .Merge(_notifications.ItemChanged.Select(_ => Unit.Default))
            .Subscribe(_ =>
            {
                GroupedNotifications = _notifications.GroupBy(x => x.Repository.FullName).Select(x =>
                {
                    var items         = x.Select(y => new NotificationItemViewModel(y, GoToNotification));
                    var notifications = new ReactiveList <NotificationItemViewModel>(items);
                    return(new NotificationGroupViewModel(x.Key, notifications));
                }).ToList();
            });


            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                var all           = ActiveFilter == AllFilter;
                var participating = ActiveFilter == ParticipatingFilter;
                var req           = new Octokit.NotificationsRequest {
                    All = all, Participating = participating, Since = DateTimeOffset.MinValue
                };
                var notifictions = await applicationService.GitHubClient.Notification.GetAllForCurrent(req);
                _notifications.Reset(notifictions);
            });

            this.WhenAnyValue(x => x.ActiveFilter).Subscribe(x =>
            {
                _notifications.Clear();
                LoadCommand.ExecuteIfCan();
            });
        }
        public BranchesAndTagsViewModel(IApplicationService applicationService)
        {
            var branches = new ReactiveList <BranchModel>();

            Branches = branches.CreateDerivedCollection(x => x,
                                                        x => x.Name.ContainsKeyword(SearchKeyword),
                                                        signalReset: this.WhenAnyValue(x => x.SearchKeyword));

            var tags = new ReactiveList <TagModel>();

            Tags = tags.CreateDerivedCollection(x => x,
                                                x => x.Name.ContainsKeyword(SearchKeyword),
                                                signalReset: this.WhenAnyValue(x => x.SearchKeyword));

            GoToSourceCommand = ReactiveCommand.Create();
            GoToSourceCommand.OfType <BranchModel>().Subscribe(x =>
            {
                var vm        = CreateViewModel <SourceTreeViewModel>();
                vm.Username   = RepositoryOwner;
                vm.Repository = RepositoryName;
                vm.Branch     = x.Name;
                vm.TrueBranch = true;
                ShowViewModel(vm);
            });
            GoToSourceCommand.OfType <TagModel>().Subscribe(x =>
            {
                var vm        = CreateViewModel <SourceTreeViewModel>();
                vm.Username   = RepositoryOwner;
                vm.Repository = RepositoryName;
                vm.Branch     = x.Commit.Sha;
                ShowViewModel(vm);
            });


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

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                if (SelectedFilter == ShowIndex.Branches)
                {
                    var request = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetBranches();
                    return(branches.LoadAll <BranchModel>(request));
                }
                else
                {
                    var request = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetTags();
                    return(tags.LoadAll <TagModel>(request));
                }
            });
        }
Exemple #8
0
        public IssuesViewModel(IApplicationService applicationService)
        {
            _applicationService = applicationService;
            _mineFilter         = IssuesFilterModel.CreateMineFilter(applicationService.Account.Username);

            Filter = _openFilter;
            Title  = "Issues";

            _filterSelection = this.WhenAnyValue(x => x.Filter)
                               .Select(x =>
            {
                if (x == null || _openFilter.Equals(x))
                {
                    return(IssueFilterSelection.Open);
                }
                if (_closedFilter.Equals(x))
                {
                    return(IssueFilterSelection.Closed);
                }
                if (_mineFilter.Equals(x))
                {
                    return(IssueFilterSelection.Mine);
                }
                return(IssueFilterSelection.Custom);
            })
                               .ToProperty(this, x => x.FilterSelection);

            GoToNewIssueCommand = ReactiveCommand.Create();
            GoToNewIssueCommand.Subscribe(_ =>
            {
                var vm             = this.CreateViewModel <IssueAddViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.CreatedIssue.IsNotNull().Subscribe(IssuesBacking.Add);
                NavigateTo(vm);
            });

            this.WhenAnyValue(x => x.Filter).Skip(1).Subscribe(_ =>
            {
                IssuesBacking.Clear();
                LoadCommand.ExecuteIfCan();
            });

            GoToCustomFilterCommand = ReactiveCommand.Create();
        }
Exemple #9
0
        public BranchesAndTagsViewModel(ISessionService applicationService)
        {
            var branches = new ReactiveList <Octokit.Branch>();

            Branches = branches.CreateDerivedCollection(
                x => new BranchItemViewModel(x.Name, () =>
            {
                var vm             = this.CreateViewModel <SourceTreeViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.Branch          = x.Name;
                vm.TrueBranch      = true;
                NavigateTo(vm);
            }),
                filter: x => x.Name.ContainsKeyword(SearchKeyword),
                signalReset: this.WhenAnyValue(x => x.SearchKeyword));

            var tags = new ReactiveList <Octokit.RepositoryTag>();

            Tags = tags.CreateDerivedCollection(
                x => new TagItemViewModel(x.Name, () =>
            {
                var vm             = this.CreateViewModel <SourceTreeViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.Branch          = x.Commit.Sha;
                NavigateTo(vm);
            }),
                filter: x => x.Name.ContainsKeyword(SearchKeyword),
                signalReset: this.WhenAnyValue(x => x.SearchKeyword));

            LoadCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                if (SelectedFilter == ShowIndex.Branches)
                {
                    branches.Reset(await applicationService.GitHubClient.Repository.GetAllBranches(RepositoryOwner, RepositoryName));
                }
                else
                {
                    tags.Reset(await applicationService.GitHubClient.Repository.GetAllTags(RepositoryOwner, RepositoryName));
                }
            });

            this.WhenAnyValue(x => x.SelectedFilter).Skip(1).Subscribe(_ => LoadCommand.ExecuteIfCan());
        }
Exemple #10
0
        public PullRequestsViewModel(IApplicationService applicationService)
        {
            ApplicationService = applicationService;
            PullRequests       = new ReactiveList <PullRequest>();

            LoadCommand.RegisterAsyncTask(_ => Load());

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

            GoToPullRequestCommand = new ReactiveCommand();
            GoToPullRequestCommand.OfType <PullRequest>().Subscribe(x =>
            {
                var vm            = CreateViewModel <PullRequestViewModel>();
                vm.ProjectKey     = ProjectKey;
                vm.RepositorySlug = RepositorySlug;
                vm.PullRequestId  = x.Id;
                ShowViewModel(vm);
            });
        }
        protected RepositoriesViewModel(IApplicationService applicationService, string filterKey = "RepositoryController")
        {
            ApplicationService = applicationService;
            Repositories       = new ReactiveList <RepositoryModel>();
            //Filter = applicationService.Account.Filters.GetFilter<RepositoriesFilterModel>(filterKey);

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

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

//			_repositories.FilteringFunction = x => Repositories.Filter.Ascending ? x.OrderBy(y => y.Name) : x.OrderByDescending(y => y.Name);
//            _repositories.GroupingFunction = CreateGroupedItems;
        }
Exemple #12
0
        public MyIssuesViewModel(IApplicationService applicationService)
        {
            _applicationService = applicationService;

            Title  = "My Issues";
            Filter = MyIssuesFilterModel.CreateOpenFilter();

            _selectedFilter = this.WhenAnyValue(x => x.Filter)
                              .Select(x =>
            {
                if (x == null || _openFilter.Equals(x))
                {
                    return(0);
                }
                return(_closedFilter.Equals(x) ? 1 : -1);
            })
                              .ToProperty(this, x => x.SelectedFilter);

            this.WhenAnyValue(x => x.Filter).Skip(1).Subscribe(_ =>
            {
                IssuesBacking.Clear();
                LoadCommand.ExecuteIfCan();
            });
        }
        public StumbleViewModel(IApplicationService applicationService, INetworkActivityService networkActivity,
                                IFeaturesService featuresService, IDefaultValueService defaultValues)
            : base(applicationService, networkActivity)
        {
            this._applicationService = applicationService;

            var localStumbleCount = 0;

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

            StumbleCommand = ReactiveCommand.CreateAsyncTask(LoadCommand.IsExecuting.Select(x => !x), x => StumbleRepository());
            StumbleCommand.Subscribe(x =>
            {
                if (!featuresService.ProEditionEnabled)
                {
                    var stumbleTimes = defaultValues.Get <nint>(StumbleKey) + 1;
                    defaultValues.Set(StumbleKey, stumbleTimes);

                    if (localStumbleCount > 0 && stumbleTimes % 50 == 0)
                    {
                        GoToPurchaseCommand.ExecuteIfCan();
                    }
                }

                localStumbleCount++;

                Reset();
                RepositoryIdentifier = new RepositoryIdentifierModel(x.Repository.Owner, x.Repository.Name);
                LoadCommand.ExecuteIfCan();
            });
            StumbleCommand.TriggerNetworkActivity(networkActivity);

            DislikeCommand.Subscribe(_ => StumbleCommand.ExecuteIfCan());
            LikeCommand.Subscribe(_ => StumbleCommand.ExecuteIfCan());
        }
Exemple #14
0
        public NotificationsViewModel(ISessionService applicationService)
        {
            _applicationService = applicationService;
            Title = "Notifications";

            _showEditButton = this.WhenAnyValue(x => x.ActiveFilter)
                              .Select(x => x != AllFilter)
                              .ToProperty(this, x => x.ShowEditButton);

            var groupedNotifications = new ReactiveList <NotificationGroupViewModel>();

            GroupedNotifications = groupedNotifications;

            Notifications = _notifications.CreateDerivedCollection(y => new NotificationItemViewModel(y, GoToNotification, DeleteNotification));
            Notifications.Changed.Where(x => x.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
            .Select(_ => Notifications)
            .Subscribe(notifications => groupedNotifications.Reset(notifications.GroupBy(x => x.Notification.Repository.FullName).Select(x => {
                var items = notifications.CreateDerivedCollection(y => y, filter: y => y.Notification.Repository.FullName == x.Key);
                return(new NotificationGroupViewModel(x.Key, items));
            })));

            _anyItemsSelected = Notifications.Changed
                                .SelectMany(x => Notifications)
                                .Select(x => x.WhenAnyValue(y => y.IsSelected))
                                .Merge()
                                .Select(x => Notifications.Select(y => y.IsSelected).Any(y => y))
                                .ToProperty(this, x => x.IsAnyItemsSelected);

            ReadSelectedCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                if (GroupedNotifications.SelectMany(x => x.Notifications).All(x => !x.IsSelected))
                {
                    var request = new Octokit.MarkAsReadRequest {
                        LastReadAt = DateTimeOffset.Now
                    };
                    applicationService.GitHubClient.Notification.MarkAsRead(request).ToBackground();
                    _notifications.Clear();
                }
                else
                {
                    var selected = GroupedNotifications.SelectMany(x => x.Notifications)
                                   .Where(x => x.IsSelected && x.Notification.Unread).ToList();

                    var tasks = selected
                                .Select(t => _applicationService.GitHubClient.Notification.MarkAsRead(int.Parse(t.Id)));

                    Task.WhenAll(tasks).ToBackground();

                    _notifications.RemoveAll(selected.Select(y => y.Notification));
                }
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => {
                var all           = ActiveFilter == AllFilter;
                var participating = ActiveFilter == ParticipatingFilter;
                var req           = new Octokit.NotificationsRequest {
                    All = all, Participating = participating, Since = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(365))
                };
                _notifications.Reset(await applicationService.GitHubClient.Notification.GetAllForCurrent(req));
            });

            _notifications.CountChanged
            .Where(_ => ActiveFilter == UnreadFilter)
            .Subscribe(_notificationCount.OnNext);

            this.WhenAnyValue(x => x.ActiveFilter).Skip(1).Subscribe(x =>
            {
                _notifications.Clear();
                LoadCommand.ExecuteIfCan();
            });
        }
        public NotificationsViewModel(IApplicationService applicationService)
        {
            _applicationService = applicationService;

            Title = "Notifications";

            var whenNotificationsChange =
                _notifications.Changed.Select(_ => Unit.Default)
                .Merge(_notifications.ItemChanged.Select(_ => Unit.Default));

            _groupedNotifications = whenNotificationsChange.Select(_ =>
                                                                   _notifications.GroupBy(x => x.Repository.FullName)
                                                                   .Select(x => new NotificationGroupViewModel(x.Key, new ReactiveList <NotificationModel>(x), __ => { })))
                                    .ToProperty(this, t => t.GroupedNotifications);

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var req = applicationService.Client.Notifications.GetAll(all: Filter.All, participating: Filter.Participating);
                return(this.RequestModel(req, t as bool?, response => _notifications.Reset(response.Data)));
            });

            GoToNotificationCommand = ReactiveCommand.Create();
            GoToNotificationCommand.OfType <NotificationModel>().Subscribe(GoToNotification);


            var canReadAll = _notifications.CountChanged.Select(x => x > 0).CombineLatest(
                this.WhenAnyValue(x => x.ShownIndex).Select(x => x != 2), (x, y) => x & y);

            ReadAllCommand = ReactiveCommand.CreateAsyncTask(canReadAll, async t =>
            {
                try
                {
                    if (!_notifications.Any())
                    {
                        return;
                    }
                    await applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkAsRead());
                    _notifications.Clear();
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to mark all notifications as read. Please try again.", e);
                }
            });

            ReadRepositoriesCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                try
                {
                    var repo = t as string;
                    if (repo == null)
                    {
                        return;
                    }
                    var repoId = new RepositoryIdentifier(repo);
                    await applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkRepoAsRead(repoId.Owner, repoId.Name));
                    _notifications.RemoveAll(_notifications.Where(x => string.Equals(x.Repository.FullName, repo, StringComparison.OrdinalIgnoreCase)).ToList());
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to mark repositories' notifications as read. Please try again.", e);
                }
            });

            this.WhenAnyValue(x => x.ShownIndex).Subscribe(x =>
            {
                switch (x)
                {
                case 0:
                    Filter = NotificationsFilterModel.CreateUnreadFilter();
                    break;

                case 1:
                    Filter = NotificationsFilterModel.CreateParticipatingFilter();
                    break;

                default:
                    Filter = NotificationsFilterModel.CreateAllFilter();
                    break;
                }
            });

            this.WhenAnyValue(x => x.Filter).Skip(1).Subscribe(x => LoadCommand.ExecuteIfCan());
        }
Exemple #16
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(x => LoadCommand.ExecuteIfCan());
                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);
            });
        }
Exemple #17
0
        public PullRequestViewModel(IApplicationService applicationService, IMarkdownService markdownService, IActionMenuService actionMenuService)
        {
            _applicationService = applicationService;
            _markdownService    = markdownService;

            GoToUrlCommand = this.CreateUrlCommand();

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

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

            GoToCommitsCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = CreateViewModel <PullRequestCommitsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.PullRequestId   = Id;
                ShowViewModel(vm);
            });

            GoToFilesCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm           = CreateViewModel <PullRequestFilesViewModel>();
                vm.Username      = RepositoryOwner;
                vm.Repository    = RepositoryName;
                vm.PullRequestId = Id;
                ShowViewModel(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             = 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);
                ShowViewModel(vm);
            });

            GoToLabelsCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)).WithSubscription(_ =>
            {
                var vm             = CreateViewModel <IssueLabelsViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.IssueId         = 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");
//                });
                ShowViewModel(vm);
            });

            GoToMilestoneCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)).WithSubscription(_ =>
            {
                var vm             = 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");
//                });
                ShowViewModel(vm);
            });

            GoToAssigneeCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Issue).Select(x => x != null)).WithSubscription(_ =>
            {
                var vm             = 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");
//                });
                ShowViewModel(vm);
            });

            GoToAddCommentCommand = ReactiveCommand.Create().WithSubscription(_ =>
            {
                var vm             = CreateViewModel <IssueCommentViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.Id = Id;
                vm.CommentAdded.Subscribe(Comments.Add);
                ShowViewModel(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;
            });
        }
Exemple #18
0
        public NotificationsViewModel(IApplicationService applicationService)
        {
            _applicationService = applicationService;
            _notifications      = new ReactiveList <NotificationModel>();
            Notifications       = _notifications.CreateDerivedCollection(x => x);

            LoadCommand = ReactiveCommand.CreateAsyncTask(t =>
            {
                var req = applicationService.Client.Notifications.GetAll(all: Filter.All, participating: Filter.Participating);
                return(this.RequestModel(req, t as bool?, response => _notifications.Reset(response.Data)));
            });

            GoToNotificationCommand = ReactiveCommand.Create();
            GoToNotificationCommand.OfType <NotificationModel>().Subscribe(GoToNotification);

            ReadAllCommand = ReactiveCommand.CreateAsyncTask(
                this.WhenAnyValue(x => x.ShownIndex).Select(x => x != 2), async t =>
            {
                try
                {
                    if (!Notifications.Any())
                    {
                        return;
                    }
                    await applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkAsRead());
                    _notifications.Clear();
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to mark all notifications as read. Please try again.", e);
                }
            });

            ReadRepositoriesCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                try
                {
                    var repo = t as string;
                    if (repo == null)
                    {
                        return;
                    }
                    var repoId = new RepositoryIdentifier(repo);
                    await applicationService.Client.ExecuteAsync(applicationService.Client.Notifications.MarkRepoAsRead(repoId.Owner, repoId.Name));
                    _notifications.RemoveAll(Notifications.Where(x => string.Equals(x.Repository.FullName, repo, StringComparison.OrdinalIgnoreCase)).ToList());
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to mark repositories' notifications as read. Please try again.", e);
                }
            });

            this.WhenAnyValue(x => x.ShownIndex).Subscribe(x =>
            {
                switch (x)
                {
                case 0:
                    Filter = NotificationsFilterModel.CreateUnreadFilter();
                    break;

                case 1:
                    Filter = NotificationsFilterModel.CreateParticipatingFilter();
                    break;

                default:
                    Filter = NotificationsFilterModel.CreateAllFilter();
                    break;
                }
            });

            this.WhenAnyValue(x => x.Filter).Skip(1).Subscribe(x => LoadCommand.ExecuteIfCan());
        }
Exemple #19
0
        public SourceTreeViewModel(IApplicationService applicationService)
        {
            Branch = "master";
            Path   = string.Empty;

            var content = new ReactiveList <ContentModel>();

            Content = content.CreateDerivedCollection(
                x => CreateSourceItemViewModel(x),
                filter: x => x.Name.ContainsKeyword(SearchKeyword),
                signalReset: this.WhenAnyValue(x => x.SearchKeyword));

            _canAddFile = this.WhenAnyValue(x => x.TrueBranch, y => y.PushAccess)
                          .Select(x => x.Item1 && x.Item2.HasValue && x.Item2.Value)
                          .ToProperty(this, x => x.CanAddFile);

            this.WhenAnyValue(x => x.Path, y => y.RepositoryName, (x, y) => new { Path = x, Repo = y })
            .Subscribe(x =>
            {
                if (string.IsNullOrEmpty(x.Path))
                {
                    Title = string.IsNullOrEmpty(x.Repo) ? "Source" : x.Repo;
                }
                else
                {
                    var split = x.Path.TrimEnd('/').Split('/');
                    Title     = split[split.Length - 1];
                }
            });

            GoToAddFileCommand = ReactiveCommand.Create(
                this.WhenAnyValue(x => x.PushAccess, x => x.TrueBranch)
                .Select(x => x.Item1.HasValue && x.Item1.Value && x.Item2))
                                 .WithSubscription(_ =>
            {
                var vm             = this.CreateViewModel <CreateFileViewModel>();
                vm.RepositoryOwner = RepositoryOwner;
                vm.RepositoryName  = RepositoryName;
                vm.Branch          = Branch;
                vm.Path            = Path;
                vm.SaveCommand.Subscribe(z => LoadCommand.ExecuteIfCan());
                NavigateTo(vm);
            });

            LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                if (!PushAccess.HasValue)
                {
                    applicationService.GitHubClient.Repository.Get(RepositoryOwner, RepositoryName)
                    .ToBackground(x => PushAccess = x.Permissions.Push);
                }

                var path = Path;
                if (string.Equals(path, "/", StringComparison.OrdinalIgnoreCase))
                {
                    path = string.Empty;
                }

                var request  = applicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContent(Path, Branch);
                var data     = new List <ContentModel>();
                var response = await applicationService.Client.ExecuteAsync(request);
                data.AddRange(response.Data);
                while (response.More != null)
                {
                    response = await applicationService.Client.ExecuteAsync(response.More);
                    data.AddRange(response.Data);
                }
                content.Reset(data.OrderBy(y => y.Type).ThenBy(y => y.Name));
            });
        }
        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());
            });
        }