public RepositoriesTrendingViewModel(IApplicationService applicationService, TrendingRepository trendingRepository) { ShowRepositoryDescription = applicationService.Account.ShowRepositoryDescriptionInList; Title = "Trending"; var defaultLanguage = LanguageRepository.DefaultLanguage; SelectedLanguage = new LanguageItemViewModel(defaultLanguage.Name, defaultLanguage.Slug); GoToLanguages = ReactiveCommand.Create().WithSubscription(_ => { var vm = CreateViewModel <LanguagesViewModel>(); vm.SelectedLanguage = SelectedLanguage; vm.WhenAnyValue(x => x.SelectedLanguage).Skip(1).Subscribe(x => { SelectedLanguage = x; vm.DismissCommand.ExecuteIfCan(); }); ShowViewModel(vm); }); var gotoRepository = new Action <RepositoryItemViewModel>(x => { var vm = CreateViewModel <RepositoryViewModel>(); vm.RepositoryOwner = x.Owner; vm.RepositoryName = x.Name; ShowViewModel(vm); }); LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => { Repositories = null; var requests = _times.Select(t => { var language = (SelectedLanguage != null && SelectedLanguage.Slug != null) ? SelectedLanguage.Slug : null; return(new { Time = t, Query = trendingRepository.GetTrendingRepositories(t.Slug, language) }); }).ToArray(); await Task.WhenAll(requests.Select(x => x.Query)); Repositories = requests.Select(r => { var transformedRepos = r.Query.Result.Select(x => new RepositoryItemViewModel(x.Name, x.Owner.Login, x.Owner.AvatarUrl, x.Description, x.StargazersCount, x.ForksCount, true, gotoRepository)); return(new GroupedCollection <RepositoryItemViewModel>(r.Time.Name, new ReactiveList <RepositoryItemViewModel>(transformedRepos))); }).ToList(); }); this.WhenAnyValue(x => x.SelectedLanguage).Skip(1).Subscribe(_ => LoadCommand.ExecuteIfCan()); }
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); }
protected override async Task Load(bool forceCacheInvalidation) { var trendingRepo = new TrendingRepository(); var repos = new List <Tuple <string, IList <RepositoryModel> > >(); var times = new [] { Tuple.Create("Daily", "daily"), Tuple.Create("Weekly", "weekly"), Tuple.Create("Monthly", "monthly"), }; foreach (var t in times) { var repo = await trendingRepo.GetTrendingRepositories(t.Item2, SelectedLanguage.Slug); repos.Add(Tuple.Create(t.Item1, repo)); } Repositories = repos; }