| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NavigationParametersExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | |
| | | 9 | | #pragma warning disable IDE0130 |
| | | 10 | | namespace MyNet.UI.Navigation.Models; |
| | | 11 | | #pragma warning restore IDE0130 |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides convenience helpers for working with <see cref="INavigationParameters"/>. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class NavigationParametersExtensions |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Converts arbitrary values to a <see cref="NavigationParameters"/> bag. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="parameters">Parameter source.</param> |
| | | 22 | | /// <returns>A normalized parameter bag.</returns> |
| | | 23 | | public static NavigationParameters ToNavigationParameters(this object? parameters) |
| | 12 | 24 | | => parameters switch |
| | 12 | 25 | | { |
| | 3 | 26 | | null => new(), |
| | 3 | 27 | | NavigationParameters navigationParameters => new(navigationParameters), |
| | 0 | 28 | | INavigationParameters typedParameters => typedParameters as NavigationParameters ?? NavigationParameters.Fro |
| | 3 | 29 | | IEnumerable<KeyValuePair<string, object?>> pairs => new(pairs), |
| | 3 | 30 | | _ => NavigationParameters.From(parameters) |
| | 12 | 31 | | }; |
| | | 32 | | |
| | | 33 | | extension(INavigationParameters? parameters) |
| | | 34 | | { |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets a typed value from a parameter bag when it is backed by <see cref="NavigationParameters"/>. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <typeparam name="T">Expected value type.</typeparam> |
| | | 39 | | /// <param name="key">Parameter key.</param> |
| | | 40 | | /// <param name="defaultValue">Fallback value.</param> |
| | | 41 | | /// <returns>The typed value or <paramref name="defaultValue"/>.</returns> |
| | | 42 | | public T? Get<T>(string key, T? defaultValue = default) |
| | 9 | 43 | | => parameters is NavigationParameters bag ? bag.Get(key, defaultValue) : defaultValue; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Tries to get a typed value from a parameter bag when it is backed by <see cref="NavigationParameters"/>. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <typeparam name="T">Expected value type.</typeparam> |
| | | 49 | | /// <param name="key">Parameter key.</param> |
| | | 50 | | /// <param name="value">Resolved value.</param> |
| | | 51 | | /// <returns><see langword="true"/> when the parameter exists.</returns> |
| | | 52 | | public bool TryGetValue<T>(string key, out T? value) |
| | | 53 | | { |
| | 3 | 54 | | if (parameters is NavigationParameters bag) |
| | 0 | 55 | | return bag.TryGetValue(key, out value); |
| | | 56 | | |
| | 3 | 57 | | value = default; |
| | 3 | 58 | | return false; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |