| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="PreferencesService.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.Linq; |
| | | 10 | | using MyNet.Collections; |
| | | 11 | | |
| | | 12 | | namespace MyNet.UI.Services; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Service that manages persistent user preferences by delegating to multiple settings groups. |
| | | 16 | | /// Implements <see cref="IPersistentPreferencesService"/> and <see cref="IDisposable"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// Initializes a new instance of the <see cref="PreferencesService"/> class. |
| | | 20 | | /// </remarks> |
| | | 21 | | /// <param name="groups">The collection of settings groups to manage.</param> |
| | | 22 | | public class PreferencesService(IEnumerable<IPersistentSettingsService> groups) : IPersistentPreferencesService, IDispos |
| | | 23 | | { |
| | 0 | 24 | | private readonly IList<IPersistentSettingsService> _groups = [.. groups]; |
| | | 25 | | private bool _disposedValue; |
| | | 26 | | |
| | | 27 | | /// <inheritdoc/> |
| | 0 | 28 | | public void Reload() => _groups.ForEach(x => x.Reload()); |
| | | 29 | | |
| | | 30 | | /// <inheritdoc/> |
| | 0 | 31 | | public void Reset() => _groups.ForEach(x => x.Reset()); |
| | | 32 | | |
| | | 33 | | /// <inheritdoc/> |
| | 0 | 34 | | public void Save() => _groups.ForEach(x => x.Save()); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Releases resources used by the service and disposes all disposable settings groups. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="disposing">Indicates whether the method is called from Dispose.</param> |
| | | 40 | | protected virtual void Dispose(bool disposing) |
| | | 41 | | { |
| | 0 | 42 | | if (_disposedValue) |
| | 0 | 43 | | return; |
| | 0 | 44 | | if (disposing) |
| | | 45 | | { |
| | 0 | 46 | | _groups.OfType<IDisposable>().ForEach(x => x.Dispose()); |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | _disposedValue = true; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Disposes the service and its managed resources. |
| | | 54 | | /// </summary> |
| | | 55 | | public void Dispose() |
| | | 56 | | { |
| | 0 | 57 | | Dispose(disposing: true); |
| | 0 | 58 | | GC.SuppressFinalize(this); |
| | 0 | 59 | | } |
| | | 60 | | } |
| | | 61 | | |