| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="VirtualizedListViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Collections.Specialized; |
| | | 10 | | using System.Linq; |
| | | 11 | | using MyNet.UI.ViewModels.List.Factories; |
| | | 12 | | |
| | | 13 | | namespace MyNet.UI.ViewModels.List; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides a list view model exposing a virtualized visible window over list items. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="T">The item type.</typeparam> |
| | | 19 | | public class VirtualizedListViewModel<T> : ListViewModel<T>, IVirtualizedListViewModel<T> |
| | | 20 | | where T : notnull |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="VirtualizedListViewModel{T}"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | public VirtualizedListViewModel(IListDataProvider<T> dataProvider, ListViewModelOptions<T>? options = null) |
| | 15 | 26 | | : base(dataProvider, options) |
| | | 27 | | { |
| | 15 | 28 | | if (Items is INotifyCollectionChanged itemsNotify) |
| | | 29 | | { |
| | 15 | 30 | | Disposables.Add( |
| | 15 | 31 | | System.Reactive.Linq.Observable.FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionCh |
| | 15 | 32 | | h => itemsNotify.CollectionChanged += h, |
| | 15 | 33 | | h => itemsNotify.CollectionChanged -= h) |
| | 15 | 34 | | .Subscribe(_ => NotifyPropertyChanged(nameof(VisibleItems)))); |
| | | 35 | | } |
| | 15 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public int VisibleStartIndex |
| | | 40 | | { |
| | | 41 | | get; |
| | | 42 | | set |
| | | 43 | | { |
| | 9 | 44 | | if (!SetProperty(ref field, Math.Max(0, value))) |
| | 3 | 45 | | return; |
| | | 46 | | |
| | 6 | 47 | | NotifyPropertyChanged(nameof(VisibleItems)); |
| | 6 | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public int VisibleCount |
| | | 53 | | { |
| | | 54 | | get; |
| | | 55 | | set |
| | | 56 | | { |
| | 9 | 57 | | if (!SetProperty(ref field, Math.Max(1, value))) |
| | 0 | 58 | | return; |
| | | 59 | | |
| | 9 | 60 | | NotifyPropertyChanged(nameof(VisibleItems)); |
| | 9 | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | 15 | 64 | | = 50; |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public IReadOnlyList<T> VisibleItems |
| | | 68 | | { |
| | | 69 | | get |
| | | 70 | | { |
| | 9 | 71 | | if (Items.Count == 0) |
| | 0 | 72 | | return []; |
| | | 73 | | |
| | 9 | 74 | | var start = Math.Min(VisibleStartIndex, Items.Count - 1); |
| | 9 | 75 | | var count = Math.Min(VisibleCount, Items.Count - start); |
| | | 76 | | |
| | 9 | 77 | | return |
| | 9 | 78 | | [ |
| | 9 | 79 | | .. Items |
| | 9 | 80 | | .Skip(start) |
| | 9 | 81 | | .Take(count) |
| | 9 | 82 | | ]; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |