Skip to content

Commit

Permalink
Fix Navigation.SetPageType method, and add support for generic type…
Browse files Browse the repository at this point in the history
… pages
  • Loading branch information
albyrock87 committed May 23, 2024
1 parent 2e10359 commit 654f014
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Source/Nalu.Maui.Navigation/NavigationInfo/Navigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class Navigation : BindableObject, IList<INavigationSegment>, IN
[TypeConverter(typeof(TypeTypeConverter))]
public static void SetPageType(BindableObject bindable, Type? value)
{
if (value?.GetType().IsSubclassOf(typeof(Page)) != true)
if (value?.IsSubclassOf(typeof(Page)) != true)
{
throw new InvalidOperationException("PageType must be a type that inherits from Page.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,25 @@ public static string GetSegmentName(Type type)
{
if (!_typeSegmentNames.TryGetValue(type, out var segmentName))
{
segmentName = type.GetCustomAttribute<NavigationSegmentAttribute>()?.SegmentName ?? type.Name;
segmentName = type.GetCustomAttribute<NavigationSegmentAttribute>()?.SegmentName ?? GetTypeName(type);
_typeSegmentNames.Add(type, segmentName);
}

return segmentName;
}

private static string GetTypeName(Type type)
{
var name = type.Name;
if (!type.IsGenericType)
{
return name;
}

var index = name.IndexOf('`');
name = name[..index];
var genericArguments = type.GetGenericArguments();
var genericArgumentsNames = genericArguments.Select(GetTypeName);
return $"{name}-{string.Join("-", genericArgumentsNames)}";
}
}
52 changes: 52 additions & 0 deletions Tests/Nalu.Maui.Test/Navigation/NavigationSegmentAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace Nalu.Maui.Test.Navigation;

public class NavigationSegmentAttributeTests
{
[NavigationSegment("Hello")]
private class WithAttribute;

private class WithoutAttribute;

private class WithGenericAttribute<T>;

[Fact(DisplayName = "NavigationSegmentAttribute.GetSegmentName, when no attribute, returns type name")]
public void NavigationSegmentAttributeGetSegmentNameWhenNoAttributeReturnsTypeName()
{
// Arrange
var type = typeof(WithoutAttribute);

// Act
var result = NavigationSegmentAttribute.GetSegmentName(type);

// Assert
result.Should().Be(nameof(WithoutAttribute));
}

[Fact(DisplayName = "NavigationSegmentAttribute.GetSegmentName, when attribute, returns attribute value")]
public void NavigationSegmentAttributeGetSegmentNameWhenAttributeReturnsAttributeValue()
{
// Arrange
var type = typeof(WithAttribute);

// Act
var result = NavigationSegmentAttribute.GetSegmentName(type);

// Assert
result.Should().Be("Hello");
}

[Fact(DisplayName = "NavigationSegmentAttribute.GetSegmentName, when generic type, returns type name")]
public void NavigationSegmentAttributeGetSegmentNameWhenGenericTypeReturnsTypeName()
{
// Arrange
var type = typeof(WithGenericAttribute<WithoutAttribute>);

// Act
var result = NavigationSegmentAttribute.GetSegmentName(type);
var uri = new Uri($"//{result}");

// Assert
result.Should().Be("WithGenericAttribute-WithoutAttribute");
uri.IsAbsoluteUri.Should().BeTrue();
}
}

0 comments on commit 654f014

Please sign in to comment.