| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NavigationParameters.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; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Diagnostics.CodeAnalysis; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Reflection; |
| | | 13 | | |
| | | 14 | | namespace MyNet.UI.Navigation.Models; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Represents a lightweight, mutable bag of navigation parameters. |
| | | 18 | | /// </summary> |
| | | 19 | | [SuppressMessage("Naming", "CA1710:Identifiers should have correct suffix", Justification = "NavigationParameters is the |
| | | 20 | | public sealed class NavigationParameters : INavigationParameters, IReadOnlyDictionary<string, object?> |
| | | 21 | | { |
| | | 22 | | private readonly Dictionary<string, object?> _values; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="NavigationParameters"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | public NavigationParameters() |
| | 30 | 28 | | : this([]) |
| | | 29 | | { |
| | 30 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="NavigationParameters"/> class. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="values">Initial values.</param> |
| | | 36 | | public NavigationParameters(IEnumerable<KeyValuePair<string, object?>> values) |
| | | 37 | | { |
| | 45 | 38 | | ArgumentNullException.ThrowIfNull(values); |
| | 45 | 39 | | _values = new(values, StringComparer.Ordinal); |
| | 45 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets an empty parameter bag. |
| | | 44 | | /// </summary> |
| | 0 | 45 | | public static NavigationParameters Empty { get; } = new(); |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | 0 | 48 | | public IEnumerable<string> Keys => _values.Keys; |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | 0 | 51 | | public IEnumerable<object?> Values => _values.Values; |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | 0 | 54 | | public int Count => _values.Count; |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | 0 | 57 | | public object? this[string key] => _values[key]; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Creates a parameter bag from an existing object. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="values">Anonymous object, dictionary, or parameter bag.</param> |
| | | 63 | | /// <returns>A navigation parameter bag.</returns> |
| | | 64 | | public static NavigationParameters From(object? values) |
| | | 65 | | { |
| | | 66 | | switch (values) |
| | | 67 | | { |
| | | 68 | | case null: |
| | 0 | 69 | | return new(); |
| | | 70 | | case NavigationParameters parameters: |
| | 0 | 71 | | return new(parameters); |
| | | 72 | | case IReadOnlyDictionary<string, object?> readOnlyDictionary: |
| | 0 | 73 | | return new(readOnlyDictionary); |
| | | 74 | | case IEnumerable<KeyValuePair<string, object?>> pairs: |
| | 0 | 75 | | return new(pairs); |
| | | 76 | | case IDictionary dictionary: |
| | | 77 | | { |
| | 0 | 78 | | var entries = new List<KeyValuePair<string, object?>>(); |
| | 0 | 79 | | foreach (DictionaryEntry entry in dictionary) |
| | | 80 | | { |
| | 0 | 81 | | if (entry.Key is string key) |
| | 0 | 82 | | entries.Add(new(key, entry.Value)); |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | return new(entries); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | default: |
| | 9 | 89 | | return new(values.GetType() |
| | 9 | 90 | | .GetProperties(BindingFlags.Instance | BindingFlags.Public) |
| | 9 | 91 | | .Where(property => property.CanRead && property.GetIndexParameters().Length == 0) |
| | 9 | 92 | | .Select(property => new KeyValuePair<string, object?>(property.Name, property.GetValue(values)))); |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Adds or replaces a parameter value. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="key">Parameter key.</param> |
| | | 100 | | /// <param name="value">Parameter value.</param> |
| | | 101 | | /// <returns>The same parameter bag for chaining.</returns> |
| | | 102 | | public NavigationParameters Set(string key, object? value) |
| | | 103 | | { |
| | 21 | 104 | | ArgumentException.ThrowIfNullOrWhiteSpace(key); |
| | 21 | 105 | | _values[key] = value; |
| | 21 | 106 | | return this; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets a parameter value with type conversion support when possible. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <typeparam name="T">Expected value type.</typeparam> |
| | | 113 | | /// <param name="key">Parameter key.</param> |
| | | 114 | | /// <param name="defaultValue">Default value when the parameter is absent or incompatible.</param> |
| | | 115 | | /// <returns>The converted value or <paramref name="defaultValue"/>.</returns> |
| | | 116 | | public T? Get<T>(string key, T? defaultValue = default) |
| | 24 | 117 | | => TryGetValue(key, out T? value) ? value : defaultValue; |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Tries to retrieve a typed parameter value. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <typeparam name="T">Expected value type.</typeparam> |
| | | 123 | | /// <param name="key">Parameter key.</param> |
| | | 124 | | /// <param name="value">Resolved value when found.</param> |
| | | 125 | | /// <returns><see langword="true"/> when the value exists and can be read.</returns> |
| | | 126 | | public bool TryGetValue<T>(string key, out T? value) |
| | | 127 | | { |
| | 39 | 128 | | ArgumentException.ThrowIfNullOrWhiteSpace(key); |
| | | 129 | | |
| | 39 | 130 | | if (!_values.TryGetValue(key, out var rawValue)) |
| | | 131 | | { |
| | 3 | 132 | | value = default; |
| | 3 | 133 | | return false; |
| | | 134 | | } |
| | | 135 | | |
| | 36 | 136 | | return NavigationParameterConversions.TryConvert(rawValue, out value); |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <inheritdoc /> |
| | 0 | 140 | | public bool ContainsKey(string key) => _values.ContainsKey(key); |
| | | 141 | | |
| | | 142 | | /// <inheritdoc /> |
| | 0 | 143 | | public bool TryGetValue(string key, out object? value) => _values.TryGetValue(key, out value); |
| | | 144 | | |
| | | 145 | | /// <inheritdoc /> |
| | 9 | 146 | | public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() => _values.GetEnumerator(); |
| | | 147 | | |
| | 3 | 148 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 149 | | } |
| | | 150 | | |