| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ImportItemsProvider.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.ObjectModel; |
| | | 10 | | |
| | | 11 | | namespace MyNet.UI.ViewModels.Import; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Maintains import items loaded from one source at a time. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <typeparam name="T">The import item type.</typeparam> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// Initializes a new instance of the <see cref="ImportItemsProvider{T}"/> class. |
| | | 19 | | /// </remarks> |
| | | 20 | | /// <param name="sources">The available import sources.</param> |
| | | 21 | | public sealed class ImportItemsProvider<T>(IReadOnlyCollection<IImportSourceViewModel<T>> sources) |
| | | 22 | | where T : ImportItemViewModel |
| | | 23 | | { |
| | | 24 | | private IImportSourceViewModel<T>? _lastSourceLoaded; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets available import sources. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public IReadOnlyCollection<IImportSourceViewModel<T>> Sources { get; } = sources ?? throw new ArgumentNullException( |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets loaded items. |
| | | 33 | | /// </summary> |
| | 0 | 34 | | public ObservableCollection<T> Items { get; } = []; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Loads items from the specified source. |
| | | 38 | | /// </summary> |
| | | 39 | | public void LoadSource(IImportSourceViewModel<T> source) |
| | | 40 | | { |
| | 0 | 41 | | ArgumentNullException.ThrowIfNull(source); |
| | | 42 | | |
| | 0 | 43 | | _lastSourceLoaded = source; |
| | 0 | 44 | | ReplaceItems(source.ProvideItems()); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Reloads items from the last loaded source. |
| | | 49 | | /// </summary> |
| | 0 | 50 | | public void Reload() => _lastSourceLoaded?.Reload(); |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Clears loaded items. |
| | | 54 | | /// </summary> |
| | 0 | 55 | | public void Clear() => Items.Clear(); |
| | | 56 | | |
| | | 57 | | private void ReplaceItems(IEnumerable<T> items) |
| | | 58 | | { |
| | 0 | 59 | | Items.Clear(); |
| | | 60 | | |
| | 0 | 61 | | foreach (var item in items) |
| | 0 | 62 | | Items.Add(item); |
| | 0 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |