| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="PagingEngine.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Reactive.Subjects; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Observable.Collections.Paging; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Maintains the active paging window applied after filtering and sorting. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class PagingEngine : IDisposable |
| | | 16 | | { |
| | 186 | 17 | | private readonly BehaviorSubject<(int Page, int PageSize)?> _state = new(null); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets an observable paging state consumed by the collection pipeline. |
| | | 21 | | /// A <see langword="null"/> value disables paging. |
| | | 22 | | /// </summary> |
| | 372 | 23 | | public IObservable<(int Page, int PageSize)?> State => _state; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets a value indicating whether paging is currently active. |
| | | 27 | | /// </summary> |
| | | 28 | | public bool IsEnabled { get; private set; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Sets the active page window. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="page">The one-based page index.</param> |
| | | 34 | | /// <param name="pageSize">The page size.</param> |
| | | 35 | | public void Set(int page, int pageSize) |
| | | 36 | | { |
| | 60 | 37 | | if (pageSize <= 0) |
| | | 38 | | { |
| | 0 | 39 | | Clear(); |
| | 0 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | |
| | 60 | 43 | | IsEnabled = true; |
| | 60 | 44 | | _state.OnNext((Math.Max(page, 1), pageSize)); |
| | 60 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Disables paging and exposes the full filtered collection through the paged view. |
| | | 49 | | /// </summary> |
| | | 50 | | public void Clear() |
| | | 51 | | { |
| | 54 | 52 | | IsEnabled = false; |
| | 54 | 53 | | _state.OnNext(null); |
| | 54 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | 156 | 57 | | public void Dispose() => _state.Dispose(); |
| | | 58 | | } |
| | | 59 | | |