| | | 1 | | using AmbientServices.Extensions; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | namespace AmbientServices; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// An implementation of <see cref="IAmbientSettingsSet"/> that treats multiple settings sets as a single set. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <pitch>Composes several settings sets into one with override semantics — later (higher-priority) sets hide same-name |
| | | 13 | | /// <pledge><see cref="IAmbientSettingsSet"/></pledge> |
| | | 14 | | /// <pledge> |
| | | 15 | | /// Reads probe the layers from highest priority to lowest and return the first value found. Writes go only to the high |
| | | 16 | | /// </pledge> |
| | | 17 | | /// <plan>An ordered <see cref="List{T}"/> of the composed sets, fixed at construction (which is what makes unsynchroniz |
| | | 18 | | /// </remarks> |
| | | 19 | | public class AmbientSettingsLayers : IAmbientSettingsSet |
| | | 20 | | { |
| | 3 | 21 | | private readonly List<IAmbientSettingsSet> _setsInLowPriorityOrder = new(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Create a settings layer with just an in-memory settings set. |
| | | 25 | | /// </summary> |
| | 2 | 26 | | public AmbientSettingsLayers() |
| | | 27 | | { |
| | | 28 | | // add a mutable set at the top level |
| | 2 | 29 | | _setsInLowPriorityOrder.Add(new BasicAmbientSettingsSet()); |
| | 2 | 30 | | } |
| | | 31 | | /// <summary> |
| | | 32 | | /// Create a settings layer with a specified set of settings sets. |
| | | 33 | | /// If the last (highest priority) set is not mutable, a mutable set will be added so that all settings can be mutat |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="sets">An enumeration of settings sets to add, with the last one being the highest priority, hiding |
| | 3 | 36 | | public AmbientSettingsLayers(IEnumerable<IAmbientSettingsSet?> sets) |
| | | 37 | | { |
| | 3 | 38 | | if (sets == null) throw new ArgumentNullException(nameof(sets)); |
| | 3 | 39 | | _setsInLowPriorityOrder.AddRange(sets.WhereNotNull()); |
| | | 40 | | // if the last set is not mutable, add a mutable set at the top level |
| | 3 | 41 | | if (!_setsInLowPriorityOrder[_setsInLowPriorityOrder.Count-1].SettingsAreMutable) |
| | | 42 | | { |
| | 2 | 43 | | _setsInLowPriorityOrder.Add(new BasicAmbientSettingsSet()); |
| | | 44 | | } |
| | 3 | 45 | | } |
| | | 46 | | /// <summary> |
| | | 47 | | /// Create a settings layer with a specified set of settings sets. |
| | | 48 | | /// If the last (highest priority) set is not mutable, a mutable set will be added so that all settings can be mutat |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="sets">An enumeration of settings sets to add, with the last one being the highest priority, hiding |
| | 3 | 51 | | public AmbientSettingsLayers(params IAmbientSettingsSet?[] sets) : this((IEnumerable<IAmbientSettingsSet?>)sets) |
| | | 52 | | { |
| | 3 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the name of the set of settings so that a settings consumer can know where a changed setting value came fro |
| | | 57 | | /// </summary> |
| | 2 | 58 | | public string SetName => $"Layers[{string.Join(",", _setsInLowPriorityOrder.Select(s => s.SetName))}]"; |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets whether or not the settings set is mutable. |
| | | 61 | | /// </summary> |
| | 2 | 62 | | public bool SettingsAreMutable => true; |
| | | 63 | | /// <summary> |
| | | 64 | | /// Changes the specified setting. |
| | | 65 | | /// For many ambient settings services, the value will only be reflected in memory until the process shuts down, but |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="key">A string that uniquely identifies the setting.</param> |
| | | 68 | | /// <param name="value">The new string value for the setting, or null if the setting should be removed.</param> |
| | | 69 | | /// <returns>Whether or not the setting actually changed.</returns> |
| | | 70 | | public bool ChangeSetting(string key, string? value) |
| | | 71 | | { |
| | 2 | 72 | | IAmbientSettingsSet mutableSet = (IAmbientSettingsSet)_setsInLowPriorityOrder[_setsInLowPriorityOrder.Count-1]; |
| | 2 | 73 | | return mutableSet.ChangeSetting(key, value); |
| | | 74 | | } |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets the current raw value for the setting with the specified key from the first settings set in which it is fou |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="key">A key identifying the setting whose value is to be retrieved.</param> |
| | | 79 | | /// <returns>The setting value, or null if the setting is not set in any of the sets.</returns> |
| | | 80 | | public string? GetRawValue(string key) |
| | | 81 | | { |
| | 2 | 82 | | for (int offset = _setsInLowPriorityOrder.Count - 1; offset >= 0; --offset) |
| | | 83 | | { |
| | 2 | 84 | | IAmbientSettingsSet set = _setsInLowPriorityOrder[offset]; |
| | 2 | 85 | | string? rawValue = set.GetRawValue(key); |
| | 2 | 86 | | if (rawValue != null) return rawValue; |
| | | 87 | | } |
| | 2 | 88 | | return null; |
| | | 89 | | } |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets the current typed value for the setting with the specified key from the first settings set in which it is f |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="key">A key identifying the setting whose value is to be retrieved.</param> |
| | | 94 | | /// <returns>The setting value, or null if the setting is not set in any of the sets.</returns> |
| | | 95 | | public object? GetTypedValue(string key) |
| | | 96 | | { |
| | 3 | 97 | | for (int offset = _setsInLowPriorityOrder.Count - 1; offset >= 0; --offset) |
| | | 98 | | { |
| | 3 | 99 | | IAmbientSettingsSet set = _setsInLowPriorityOrder[offset]; |
| | 3 | 100 | | object? typedValue = set.GetTypedValue(key); |
| | 3 | 101 | | if (typedValue != null) return typedValue; |
| | | 102 | | } |
| | 2 | 103 | | return null; |
| | | 104 | | } |
| | | 105 | | } |