Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add GetValue methods to INavigationParameter #308

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ namespace Sextant
{
System.IObservable<System.Reactive.Unit> WhenNavigatingTo(Sextant.INavigationParameter parameter);
}
public interface INavigationParameter : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.IEnumerable { }
public interface INavigationParameter : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.IEnumerable
{
T GetValue<T>(string key);
bool TryGetValue<T>(string key, out T value);
}
[System.Obsolete("Please use the IViewModel interface.")]
public interface IPageViewModel : Sextant.IViewModel { }
public interface IParameterViewStackService : Sextant.IViewStackService
Expand Down Expand Up @@ -100,6 +104,8 @@ namespace Sextant
public class NavigationParameter : System.Collections.Generic.Dictionary<string, object>, Sextant.INavigationParameter, System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.IEnumerable
{
public NavigationParameter() { }
public T GetValue<T>(string key) { }
public bool TryGetValue<T>(string key, out T value) { }
}
public sealed class ParameterViewStackService : Sextant.ParameterViewStackServiceBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ namespace Sextant
{
System.IObservable<System.Reactive.Unit> WhenNavigatingTo(Sextant.INavigationParameter parameter);
}
public interface INavigationParameter : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.IEnumerable { }
public interface INavigationParameter : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.IEnumerable
{
T GetValue<T>(string key);
bool TryGetValue<T>(string key, out T value);
}
[System.Obsolete("Please use the IViewModel interface.")]
public interface IPageViewModel : Sextant.IViewModel { }
public interface IParameterViewStackService : Sextant.IViewStackService
Expand Down Expand Up @@ -100,6 +104,8 @@ namespace Sextant
public class NavigationParameter : System.Collections.Generic.Dictionary<string, object>, Sextant.INavigationParameter, System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.Generic.IDictionary<string, object>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>, System.Collections.IEnumerable
{
public NavigationParameter() { }
public T GetValue<T>(string key) { }
public bool TryGetValue<T>(string key, out T value) { }
}
public sealed class ParameterViewStackService : Sextant.ParameterViewStackServiceBase
{
Expand Down
55 changes: 55 additions & 0 deletions src/Sextant.Tests/Navigation/NavigationParameterTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Tests <see cref="NavigationParameter"/> and extensions.
/// </summary>
public class NavigationParameterTests
{
/// <summary>
/// Tests the method gets a value.
/// </summary>
[Fact]
public void Should_Get_Value()
{
// Given
NavigationParameter sut = new();
sut.Add("key", TimeSpan.Zero);

// When
var result = sut.GetValue<TimeSpan>("key");

// Then
result
.Should()
.Be(TimeSpan.Zero);
}

/// <summary>
/// Tests the method tries to gets a value.
/// </summary>
[Fact]
public void Should_Try_Get_Value()
{
// Given
NavigationParameter sut = new();
sut.Add("key", TimeSpan.Zero);

// When
var result = sut.TryGetValue<TimeSpan>("key", out var value);

// Then
result
.Should()
.BeTrue();
}
}
}
18 changes: 17 additions & 1 deletion src/Sextant/Abstractions/INavigationParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,21 @@ namespace Sextant
/// </summary>
public interface INavigationParameter : IDictionary<string, object>
{
/// <summary>
/// Gets the value from the navigation parameter.
/// </summary>
/// <param name="key">The key.</param>
/// <typeparam name="T">The type parameter.</typeparam>
/// <returns>The value.</returns>
T GetValue<T>(string key);

/// <summary>
/// Gets the value from the navigation parameter with the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <typeparam name="T">The type of the parameter.</typeparam>
/// <returns>A value indicating whether the value exists.</returns>
bool TryGetValue<T>(string key, out T value);
}
}
}
24 changes: 24 additions & 0 deletions src/Sextant/Navigation/NavigationParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Sextant
{
Expand All @@ -17,5 +18,28 @@ namespace Sextant
[SuppressMessage("Design", "CA2237: Mark ISerializable types with SerializableAttribute.", Justification = "Deliberate usage")]
public class NavigationParameter : Dictionary<string, object>, INavigationParameter
{
/// <inheritdoc />
public T GetValue<T>(string key)
{
if (TryGetValue(key, out var result))
{
return (T)result;
}

return default!;
}

/// <inheritdoc />
public bool TryGetValue<T>(string key, out T value)
{
if (TryGetValue(key, out var result))
{
value = (T)result;
return true;
}

value = default!;
return false;
}
}
}