/// <summary>
        /// Create a standard <see cref="PageReadRequest"/> for a specified <see cref="IPagedDataProvider{T}"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="provider"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public static PageReadRequest CreateReadRequest <T>(this IPagedDataProvider <T> provider, int offset)
        {
            var alignedOffset = provider.PageAlignedOffset(offset);

            return(new PageReadRequest()
            {
                Offset = alignedOffset, Take = provider.MaxPageSize
            });
        }
Exemple #2
0
        protected PagedSourceListViewModelBase(IScarletCommandBuilder commandBuilder, SynchronizationContext synchronizationContext, Func <TViewModel, string> selector, IPagedDataProvider <TViewModel> pagedDataProvider)
            : base(synchronizationContext, selector)
        {
            CommandBuilder = commandBuilder ?? throw new ArgumentNullException(nameof(commandBuilder));
            Dispatcher     = commandBuilder.Dispatcher ?? throw new ArgumentNullException(nameof(IScarletCommandBuilder.Dispatcher));
            CommandManager = commandBuilder.CommandManager ?? throw new ArgumentNullException(nameof(IScarletCommandBuilder.CommandManager));

            _pagedDataProvider = pagedDataProvider ?? throw new ArgumentNullException(nameof(pagedDataProvider));

            SelectedItems = new ObservableCollection <TViewModel>();
            BusyStack     = new ObservableBusyStack((hasItems) => IsBusy = hasItems);
            PageSize      = 50;

            _loadCommand = commandBuilder
                           .Create(Load, CanLoad)
                           .WithSingleExecution()
                           .WithBusyNotification(BusyStack)
                           .WithAsyncCancellation()
                           .Build();

            _refreshCommand = commandBuilder
                              .Create(Refresh, CanRefresh)
                              .WithSingleExecution()
                              .WithBusyNotification(BusyStack)
                              .WithAsyncCancellation()
                              .Build();

            _unloadCommand = commandBuilder
                             .Create(Unload, CanUnload)
                             .WithSingleExecution()
                             .WithBusyNotification(BusyStack)
                             .WithAsyncCancellation()
                             .Build();

            NextCommand = commandBuilder
                          .Create(Next, CanNext)
                          .WithSingleExecution()
                          .WithBusyNotification(BusyStack)
                          .WithAsyncCancellation()
                          .Build();

            PreviousCommand = commandBuilder
                              .Create(Previous, CanPrevious)
                              .WithSingleExecution()
                              .WithBusyNotification(BusyStack)
                              .WithAsyncCancellation()
                              .Build();

            FirstCommand = commandBuilder
                           .Create(First, CanFirst)
                           .WithSingleExecution()
                           .WithBusyNotification(BusyStack)
                           .WithAsyncCancellation()
                           .Build();

            LastCommand = commandBuilder
                          .Create(Last, CanLast)
                          .WithSingleExecution()
                          .WithBusyNotification(BusyStack)
                          .WithAsyncCancellation()
                          .Build();
        }
        /// <summary>
        /// Utility function to calculate a page aligned offset for a <see cref="IPagedDataProvider{T}"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="provider"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public static int PageAlignedOffset <T>(this IPagedDataProvider <T> provider, int offset)
        {
            var chunkSize = provider.MaxPageSize;

            return((int)Math.Floor(((float)offset) / chunkSize) * chunkSize);
        }