public RepositoryViewModel(IApplicationService applicationService, IAccountsService accountsService) { ApplicationService = applicationService; _accountsService = accountsService; ToggleStarCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue), t => ToggleStar()); ToggleWatchCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.IsWatched, x => x.HasValue), t => ToggleWatch()); GoToOwnerCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository).Select(x => x != null)); GoToOwnerCommand.Select(_ => Repository.Owner).Subscribe(x => { if (string.Equals(x.Type, "organization", StringComparison.OrdinalIgnoreCase)) { var vm = CreateViewModel <OrganizationViewModel>(); vm.Username = RepositoryOwner; ShowViewModel(vm); } else { var vm = CreateViewModel <UserViewModel>(); vm.Username = RepositoryOwner; ShowViewModel(vm); } }); PinCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository).Select(x => x != null)); PinCommand.Subscribe(x => PinRepository()); GoToForkParentCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && x.Fork && x.Parent != null)); GoToForkParentCommand.Subscribe(x => { var vm = CreateViewModel <RepositoryViewModel>(); vm.RepositoryOwner = Repository.Parent.Owner.Login; vm.RepositoryName = Repository.Parent.Name; vm.Repository = Repository.Parent; ShowViewModel(vm); }); GoToStargazersCommand = ReactiveCommand.Create(); GoToStargazersCommand.Subscribe(_ => { var vm = CreateViewModel <RepositoryStargazersViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToWatchersCommand = ReactiveCommand.Create(); GoToWatchersCommand.Subscribe(_ => { var vm = CreateViewModel <RepositoryWatchersViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToEventsCommand = ReactiveCommand.Create(); GoToEventsCommand.Subscribe(_ => { var vm = CreateViewModel <RepositoryEventsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToIssuesCommand = ReactiveCommand.Create(); GoToIssuesCommand.Subscribe(_ => { var vm = CreateViewModel <IssuesViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToReadmeCommand = ReactiveCommand.Create(); GoToReadmeCommand.Subscribe(_ => { var vm = CreateViewModel <ReadmeViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToBranchesCommand = ReactiveCommand.Create().WithSubscription(_ => { var vm = CreateViewModel <CommitBranchesViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToCommitsCommand = ReactiveCommand.Create(); GoToCommitsCommand.Subscribe(_ => { if (Branches != null && Branches.Count == 1) { var vm = CreateViewModel <ChangesetsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; vm.Branch = Repository == null ? null : Repository.DefaultBranch; ShowViewModel(vm); } else { GoToBranchesCommand.ExecuteIfCan(); } }); GoToPullRequestsCommand = ReactiveCommand.Create(); GoToPullRequestsCommand.Subscribe(_ => { var vm = CreateViewModel <PullRequestsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToSourceCommand = ReactiveCommand.Create(); GoToSourceCommand.Subscribe(_ => { var vm = CreateViewModel <BranchesAndTagsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToContributors = ReactiveCommand.Create(); GoToContributors.Subscribe(_ => { var vm = CreateViewModel <RepositoryContributorsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToForksCommand = ReactiveCommand.Create().WithSubscription(_ => { var vm = CreateViewModel <RepositoryForksViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToReleasesCommand = ReactiveCommand.Create().WithSubscription(_ => { var vm = CreateViewModel <ReleasesViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && !string.IsNullOrEmpty(x.HtmlUrl))); GoToHtmlUrlCommand.Subscribe(_ => GoToUrlCommand.ExecuteIfCan(Repository.HtmlUrl)); LoadCommand = ReactiveCommand.CreateAsyncTask(t => { var forceCacheInvalidation = t as bool?; var t1 = this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Get(), forceCacheInvalidation, response => Repository = response.Data); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetReadme(), forceCacheInvalidation, response => Readme = response.Data).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetBranches(), forceCacheInvalidation, response => Branches = response.Data).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].IsWatching(), forceCacheInvalidation, response => IsWatched = response.Data).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].IsStarred(), forceCacheInvalidation, response => IsStarred = response.Data).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetContributors(), forceCacheInvalidation, response => Contributors = response.Data.Count).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetLanguages(), forceCacheInvalidation, response => Languages = response.Data.Count).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetReleases(), forceCacheInvalidation, response => Releases = response.Data.Count).FireAndForget(); return(t1); }); }
public GistViewModel(IApplicationService applicationService, IShareService shareService) { _applicationService = applicationService; Comments = new ReactiveList <GistCommentModel>(); ShareCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null)); ShareCommand.Subscribe(_ => shareService.ShareUrl(Gist.HtmlUrl)); ToggleStarCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue), async t => { try { if (!IsStarred.HasValue) { return; } var request = IsStarred.Value ? _applicationService.Client.Gists[Id].Unstar() : _applicationService.Client.Gists[Id].Star(); await _applicationService.Client.ExecuteAsync(request); IsStarred = !IsStarred.Value; } catch (Exception e) { throw new Exception("Unable to start gist. Please try again.", e); } }); ForkCommand = ReactiveCommand.CreateAsyncTask(async t => { var data = await _applicationService.Client.ExecuteAsync(_applicationService.Client.Gists[Id].ForkGist()); var forkedGist = data.Data; var vm = CreateViewModel <GistViewModel>(); vm.Id = forkedGist.Id; vm.Gist = forkedGist; ShowViewModel(vm); }); GoToViewableFileCommand = ReactiveCommand.Create(); GoToViewableFileCommand.OfType <GistFileModel>().Subscribe(x => { var vm = CreateViewModel <GistViewableFileViewModel>(); vm.GistFile = x; ShowViewModel(vm); }); GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null && !string.IsNullOrEmpty(x.HtmlUrl))); GoToHtmlUrlCommand.Subscribe(_ => GoToUrlCommand.ExecuteIfCan(Gist.HtmlUrl)); GoToFileSourceCommand = ReactiveCommand.Create(); GoToFileSourceCommand.OfType <GistFileModel>().Subscribe(x => { var vm = CreateViewModel <GistFileViewModel>(); vm.Id = Id; vm.GistFile = x; vm.Filename = x.Filename; ShowViewModel(vm); }); GoToUserCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Gist).Select(x => x != null)); GoToUserCommand.Subscribe(x => { var vm = CreateViewModel <UserViewModel>(); vm.Username = Gist.Owner.Login; ShowViewModel(vm); }); GoToForksCommand = ReactiveCommand.Create(); LoadCommand = ReactiveCommand.CreateAsyncTask(t => { var forceCacheInvalidation = t as bool?; var t1 = this.RequestModel(_applicationService.Client.Gists[Id].Get(), forceCacheInvalidation, response => Gist = response.Data); this.RequestModel(_applicationService.Client.Gists[Id].IsGistStarred(), forceCacheInvalidation, response => IsStarred = response.Data).FireAndForget(); Comments.SimpleCollectionLoad(_applicationService.Client.Gists[Id].GetComments(), forceCacheInvalidation).FireAndForget(); return(t1); }); }
public RepositoryViewModel(IApplicationService applicationService, IAccountsService accountsService) { ApplicationService = applicationService; _accountsService = accountsService; ToggleStarCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.IsStarred).Select(x => x.HasValue), t => ToggleStar()); ToggleWatchCommand = ReactiveCommand.CreateAsyncTask( this.WhenAnyValue(x => x.IsWatched, x => x.HasValue), t => ToggleWatch()); GoToOwnerCommand = ReactiveCommand.Create(); GoToOwnerCommand.Subscribe(_ => { var vm = CreateViewModel <ProfileViewModel>(); vm.Username = RepositoryOwner; ShowViewModel(vm); }); PinCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository).Select(x => x != null)); PinCommand.Subscribe(x => PinRepository()); GoToForkParentCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && x.Fork && x.Parent != null)); GoToForkParentCommand.Subscribe(x => { var vm = CreateViewModel <RepositoryViewModel>(); vm.RepositoryOwner = Repository.Parent.Owner.Login; vm.RepositoryName = Repository.Parent.Name; vm.Repository = Repository.Parent; ShowViewModel(vm); }); GoToStargazersCommand = ReactiveCommand.Create(); GoToStargazersCommand.Subscribe(_ => { var vm = CreateViewModel <RepositoryStargazersViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToWatchersCommand = ReactiveCommand.Create(); GoToWatchersCommand.Subscribe(_ => { var vm = CreateViewModel <RepositoryWatchersViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToEventsCommand = ReactiveCommand.Create(); GoToEventsCommand.Subscribe(_ => { var vm = CreateViewModel <RepositoryEventsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToIssuesCommand = ReactiveCommand.Create(); GoToIssuesCommand.Subscribe(_ => { var vm = CreateViewModel <IssuesViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToReadmeCommand = ReactiveCommand.Create(); GoToReadmeCommand.Subscribe(_ => { var vm = CreateViewModel <ReadmeViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToCommitsCommand = ReactiveCommand.Create(); GoToCommitsCommand.Subscribe(_ => { if (Branches != null && Branches.Count == 1) { var vm = CreateViewModel <ChangesetsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); } else { var vm = CreateViewModel <ChangesetBranchesViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); } }); GoToPullRequestsCommand = ReactiveCommand.Create(); GoToPullRequestsCommand.Subscribe(_ => { var vm = CreateViewModel <PullRequestsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToSourceCommand = ReactiveCommand.Create(); GoToSourceCommand.Subscribe(_ => { var vm = CreateViewModel <BranchesAndTagsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); GoToCollaboratorsCommand = ReactiveCommand.Create(); GoToCollaboratorsCommand.Subscribe(_ => { var vm = CreateViewModel <RepositoryCollaboratorsViewModel>(); vm.RepositoryOwner = RepositoryOwner; vm.RepositoryName = RepositoryName; ShowViewModel(vm); }); this.WhenAnyValue(x => x.Repository).Subscribe(x => { if (x == null) { RepositorySize = null; } else { if (x.Size / 1024f < 1) { RepositorySize = string.Format("{0:0.##}KB", x.Size); } else if ((x.Size / 1024f / 1024f) < 1) { RepositorySize = string.Format("{0:0.##}MB", x.Size / 1024f); } else { RepositorySize = string.Format("{0:0.##}GB", x.Size / 1024f / 1024f); } } }); GoToHtmlUrlCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository, x => x != null && !string.IsNullOrEmpty(x.HtmlUrl))); GoToHtmlUrlCommand.Subscribe(_ => GoToUrlCommand.ExecuteIfCan(Repository.HtmlUrl)); LoadCommand = ReactiveCommand.CreateAsyncTask(t => { var forceCacheInvalidation = t as bool?; var t1 = this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].Get(), forceCacheInvalidation, response => Repository = response.Data); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetReadme(), forceCacheInvalidation, response => Readme = response.Data).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetBranches(), forceCacheInvalidation, response => Branches = response.Data).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].IsWatching(), forceCacheInvalidation, response => IsWatched = response.Data).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].IsStarred(), forceCacheInvalidation, response => IsStarred = response.Data).FireAndForget(); this.RequestModel(ApplicationService.Client.Users[RepositoryOwner].Repositories[RepositoryName].GetCollaborators(), forceCacheInvalidation, response => Collaborators = response.Data.Count).FireAndForget(); return(t1); }); }