diff --git a/src/Sextant.Tests/API/ApiApprovalTests.Sextant.net5.0.approved.txt b/src/Sextant.Tests/API/ApiApprovalTests.Sextant.net5.0.approved.txt index a452da6d..a492fa86 100644 --- a/src/Sextant.Tests/API/ApiApprovalTests.Sextant.net5.0.approved.txt +++ b/src/Sextant.Tests/API/ApiApprovalTests.Sextant.net5.0.approved.txt @@ -47,7 +47,11 @@ namespace Sextant { System.IObservable WhenNavigatingTo(Sextant.INavigationParameter parameter); } - public interface INavigationParameter : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { } + public interface INavigationParameter : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable + { + T GetValue(string key); + bool TryGetValue(string key, out T value); + } [System.Obsolete("Please use the IViewModel interface.")] public interface IPageViewModel : Sextant.IViewModel { } public interface IParameterViewStackService : Sextant.IViewStackService @@ -100,6 +104,8 @@ namespace Sextant public class NavigationParameter : System.Collections.Generic.Dictionary, Sextant.INavigationParameter, System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { public NavigationParameter() { } + public T GetValue(string key) { } + public bool TryGetValue(string key, out T value) { } } public sealed class ParameterViewStackService : Sextant.ParameterViewStackServiceBase { diff --git a/src/Sextant.Tests/API/ApiApprovalTests.Sextant.netcoreapp3.1.approved.txt b/src/Sextant.Tests/API/ApiApprovalTests.Sextant.netcoreapp3.1.approved.txt index 6692d066..94739326 100644 --- a/src/Sextant.Tests/API/ApiApprovalTests.Sextant.netcoreapp3.1.approved.txt +++ b/src/Sextant.Tests/API/ApiApprovalTests.Sextant.netcoreapp3.1.approved.txt @@ -47,7 +47,11 @@ namespace Sextant { System.IObservable WhenNavigatingTo(Sextant.INavigationParameter parameter); } - public interface INavigationParameter : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { } + public interface INavigationParameter : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable + { + T GetValue(string key); + bool TryGetValue(string key, out T value); + } [System.Obsolete("Please use the IViewModel interface.")] public interface IPageViewModel : Sextant.IViewModel { } public interface IParameterViewStackService : Sextant.IViewStackService @@ -100,6 +104,8 @@ namespace Sextant public class NavigationParameter : System.Collections.Generic.Dictionary, Sextant.INavigationParameter, System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { public NavigationParameter() { } + public T GetValue(string key) { } + public bool TryGetValue(string key, out T value) { } } public sealed class ParameterViewStackService : Sextant.ParameterViewStackServiceBase { diff --git a/src/Sextant.Tests/Navigation/NavigationParameterTests.cs b/src/Sextant.Tests/Navigation/NavigationParameterTests.cs new file mode 100644 index 00000000..9c16d925 --- /dev/null +++ b/src/Sextant.Tests/Navigation/NavigationParameterTests.cs @@ -0,0 +1,55 @@ +// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved. +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using FluentAssertions; +using Xunit; + +namespace Sextant.Tests +{ + /// + /// Tests and extensions. + /// + public class NavigationParameterTests + { + /// + /// Tests the method gets a value. + /// + [Fact] + public void Should_Get_Value() + { + // Given + NavigationParameter sut = new(); + sut.Add("key", TimeSpan.Zero); + + // When + var result = sut.GetValue("key"); + + // Then + result + .Should() + .Be(TimeSpan.Zero); + } + + /// + /// Tests the method tries to gets a value. + /// + [Fact] + public void Should_Try_Get_Value() + { + // Given + NavigationParameter sut = new(); + sut.Add("key", TimeSpan.Zero); + + // When + var result = sut.TryGetValue("key", out var value); + + // Then + result + .Should() + .BeTrue(); + } + } +} diff --git a/src/Sextant/Abstractions/INavigationParameter.cs b/src/Sextant/Abstractions/INavigationParameter.cs index d8e0fdd6..0e13326a 100644 --- a/src/Sextant/Abstractions/INavigationParameter.cs +++ b/src/Sextant/Abstractions/INavigationParameter.cs @@ -12,5 +12,21 @@ namespace Sextant /// public interface INavigationParameter : IDictionary { + /// + /// Gets the value from the navigation parameter. + /// + /// The key. + /// The type parameter. + /// The value. + T GetValue(string key); + + /// + /// Gets the value from the navigation parameter with the specified key. + /// + /// The key. + /// The value. + /// The type of the parameter. + /// A value indicating whether the value exists. + bool TryGetValue(string key, out T value); } -} \ No newline at end of file +} diff --git a/src/Sextant/Navigation/NavigationParameter.cs b/src/Sextant/Navigation/NavigationParameter.cs index 12feda1d..3fb20c91 100644 --- a/src/Sextant/Navigation/NavigationParameter.cs +++ b/src/Sextant/Navigation/NavigationParameter.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Linq; namespace Sextant { @@ -17,5 +18,28 @@ namespace Sextant [SuppressMessage("Design", "CA2237: Mark ISerializable types with SerializableAttribute.", Justification = "Deliberate usage")] public class NavigationParameter : Dictionary, INavigationParameter { + /// + public T GetValue(string key) + { + if (TryGetValue(key, out var result)) + { + return (T)result; + } + + return default!; + } + + /// + public bool TryGetValue(string key, out T value) + { + if (TryGetValue(key, out var result)) + { + value = (T)result; + return true; + } + + value = default!; + return false; + } } }