Exemplo n.º 1
0
        public RepositoriesViewModel(
            Uri repositoriesUri,
            bool showOwner = true,
            IApplicationService applicationService = null,
            IAlertDialogService dialogService      = null)
        {
            _applicationService = applicationService ?? Locator.Current.GetService <IApplicationService>();
            _dialogService      = dialogService ?? Locator.Current.GetService <IAlertDialogService>();

            NextPage = repositoriesUri;

            var showDescription = _applicationService.Account.ShowRepositoryDescriptionInList;

            RepositoryItemSelected = ReactiveCommand.Create <RepositoryItemViewModel, RepositoryItemViewModel>(x => x);

            var repositoryItems = _internalItems.CreateDerivedCollection(
                x => new RepositoryItemViewModel(x, showOwner, showDescription, GoToRepository));

            var searchUpdated = this.WhenAnyValue(x => x.SearchText)
                                .Throttle(TimeSpan.FromMilliseconds(400), RxApp.MainThreadScheduler);

            Items = repositoryItems
                    .CreateDerivedCollection(
                x => x,
                x => x.Name.ContainsKeyword(SearchText),
                signalReset: searchUpdated);

            LoadCommand = ReactiveCommand.CreateFromTask(async t =>
            {
                _internalItems.Clear();
                var parameters         = new Dictionary <string, string>();
                parameters["per_page"] = 75.ToString();
                var items = await RetrieveRepositories(repositoriesUri, parameters);
                _internalItems.AddRange(items);
                return(items.Count > 0);
            });

            var canLoadMore = this.WhenAnyValue(x => x.NextPage).Select(x => x != null);

            LoadMoreCommand = ReactiveCommand.CreateFromTask(async _ =>
            {
                var items = await RetrieveRepositories(NextPage);
                _internalItems.AddRange(items);
                return(items.Count > 0);
            }, canLoadMore);

            LoadCommand.Select(_ => _internalItems.Count == 0)
            .ToProperty(this, x => x.IsEmpty, out _isEmpty, true);

            LoadCommand.ThrownExceptions.Subscribe(LoadingError);

            LoadMoreCommand.ThrownExceptions.Subscribe(LoadingError);

            _hasMore = this.WhenAnyValue(x => x.NextPage)
                       .Select(x => x != null)
                       .ToProperty(this, x => x.HasMore);
        }