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()); }
protected BaseRepositoryViewModel(IApplicationService applicationService, INetworkActivityService networkActivity) { LikeCommand = ReactiveCommand.Create(); LikeCommand.Subscribe(_ => { if (StumbledRepository == null) { var repo = CreateStumbledRepository(); repo.Liked = true; applicationService.Account.StumbledRepositories.Insert(repo); StumbledRepository = repo; } else { StumbledRepository.Liked = true; StumbledRepository.Description = Repository.Description; StumbledRepository.Forks = Repository.ForksCount; StumbledRepository.Stars = Repository.WatchersCount; StumbledRepository.ImageUrl = Repository.Owner.AvatarUrl; applicationService.Account.StumbledRepositories.Update(StumbledRepository); } if (applicationService.Account.SyncWithGitHub) { applicationService.Client.Activity.Starring.StarRepo(Repository.Owner.Login, Repository.Name); } Liked = true; }); DislikeCommand = ReactiveCommand.Create(); DislikeCommand.Subscribe(_ => { if (StumbledRepository == null) { var repo = CreateStumbledRepository(); repo.Liked = false; applicationService.Account.StumbledRepositories.Insert(repo); StumbledRepository = repo; } else { StumbledRepository.Liked = false; StumbledRepository.Description = Repository.Description; StumbledRepository.Forks = Repository.ForksCount; StumbledRepository.Stars = Repository.WatchersCount; StumbledRepository.ImageUrl = Repository.Owner.AvatarUrl; applicationService.Account.StumbledRepositories.Update(StumbledRepository); } if (applicationService.Account.SyncWithGitHub) { applicationService.Client.Activity.Starring.RemoveStarFromRepo(Repository.Owner.Login, Repository.Name); } Liked = false; }); this.WhenAnyValue(x => x.RepositoryIdentifier) .Where(x => x != null) .Subscribe(x => { StumbledRepository = applicationService.Account.StumbledRepositories.FindByFullname(x.Owner, x.Name); }); this.WhenAnyValue(x => x.StumbledRepository) .Where(x => x != null) .Subscribe(x => Liked = x.Liked); GoToGitHubCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.Repository).Select(x => x != null)); GoToGitHubCommand.Subscribe(_ => GoToUrlCommand.ExecuteIfCan(Repository.HtmlUrl)); LoadCommand = ReactiveCommand.CreateAsyncTask(this.WhenAnyValue(x => x.RepositoryIdentifier).Select(x => x != null), async t => { Repository = (await applicationService.Client.Repository.Get(RepositoryIdentifier.Owner, RepositoryIdentifier.Name)); ContributorCount = (await applicationService.Client.Repository.GetAllContributors(RepositoryIdentifier.Owner, RepositoryIdentifier.Name)).Count; try { Readme = await applicationService.Client.Repository.GetReadmeHtml(RepositoryIdentifier.Owner, RepositoryIdentifier.Name); } catch (Exception e) { Readme = "<center>There is no readme for this repository :(</center>"; Debug.WriteLine(e.Message + " for " + RepositoryIdentifier.Owner + "/" + RepositoryIdentifier.Name); } }); LoadCommand.TriggerNetworkActivity(networkActivity); }