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

GoBack - if there is a page on the modal stack, pop it without requiring UseModalNavigation #62

Open
wants to merge 5 commits into
base: page-visibility-events
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

=======
## [0.3.0] - PREVIEW

### Added
- Workaround to fix window title on WinUI
- `IPageVisibilityEvents` interface
- `MapNavigationParameterAttribute` to map navigation parameters to properties
- Parameters can be marked as required
- `TryGetValue` to `NavigationParameters`

### Changed
- `GoBack` checks if there are pages on modal stack and pop modal page if there is one

### Fixed
- WinUI source generator error
Expand Down
54 changes: 54 additions & 0 deletions src/Burkus.Mvvm.Maui/Models/NavigationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,60 @@ internal NavigationParameters(IDictionary<string, object> dictionary) : base(dic
}
}

// 2024-02-06 - DP - added method
/// <summary>
/// Gets the value associated with the specified parameter name and converts it to the specified type.
/// </summary>
/// <typeparam name="T">The type to which the parameter value should be converted.</typeparam>
/// <param name="parameterName">The name of the parameter whose value is to be retrieved.</param>
/// <returns>
/// A bool value if parameter exists and read successfully
/// </returns>
/// <exception cref="ArgumentException">
/// Thrown when the parameter value cannot be converted to the specified type.
/// </exception>
public bool TryGetValue<T>(string parameterName, out T? returnValue)
{
returnValue = default;

if (!ContainsKey(parameterName))
{
return false;
}

var value = this[parameterName];

if (value == null)
{
return true;
}

try
{
if (typeof(T).IsEnum)
{
returnValue = (T)Enum.Parse(typeof(T), value.ToString());
}
else
{
// get the non-nullable type of T
var underlyingType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);

// convert the value to the non-nullable type
var convertedValue = Convert.ChangeType(value, underlyingType);

// cast the converted value to T
returnValue = (T)convertedValue;
}
}
catch (Exception ex)
{
throw new ArgumentException($"Cannot convert parameter value to type {typeof(T)}", ex);
}

return true;
}

/// <summary>
/// Merge this NavigationParameter object with one or more NavigationParameters.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Burkus.Mvvm.Maui/Services/NavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public async Task GoBack(NavigationParameters navigationParameters)
{
await HandleNavigation<Page>(async () =>
{
if (navigationParameters.UseModalNavigation)
if ((navigationParameters.UseModalNavigation && Application.Current.MainPage.Navigation.ModalStack.Count > 0 ) ||
Application.Current.MainPage.Navigation.ModalStack.Count > 0)
{
_ = await Application.Current.MainPage.Navigation.PopModalAsync(navigationParameters.UseAnimatedNavigation);
}
Expand Down
133 changes: 133 additions & 0 deletions tests/Burkus.Mvvm.Maui.UnitTests/Models/NavigationParametersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,139 @@ public void GetValue_NullableValueParameters_ReturnsCharValue()
Assert.Equal('C', result);
}

// -----------------------------

[Fact]
public void TryGetValue_ExistingBooleanParameter_ReturnsValue()
{
// Arrange
var navigationParameters = new NavigationParameters();
navigationParameters["MyBoolParam"] = true;

// Act
var result = navigationParameters.TryGetValue<bool>("MyBoolParam", out var returnValue);

// Assert
Assert.True(result);
Assert.True(returnValue);
}

[Fact]
public void TryGetValue_NonExistentParameter_ReturnsDefaultValue()
{
// Arrange
var navigationParameters = new NavigationParameters();

// Act
var result = navigationParameters.TryGetValue<bool>("NonExistentParam", out var returnValue);

// Assert
Assert.False(result);
Assert.False(returnValue);
}

[Fact]
public void TryGetValue_InvalidConversion_ThrowsArgumentException()
{
// Arrange
var navigationParameters = new NavigationParameters();
navigationParameters["InvalidParam"] = "InvalidValue";

// Act & Assert
Assert.Throws<ArgumentException>(() => navigationParameters.TryGetValue<int>("InvalidParam", out var returnValue));
}

[Fact]
public void TryGetValue_EnumParameter_ReturnsEnumValue()
{
// Arrange
var navigationParameters = new NavigationParameters();
navigationParameters["EnumParam"] = SampleEnum.Option2;

// Act
var result = navigationParameters.TryGetValue<SampleEnum>("EnumParam", out var returnValue);

// Assert
Assert.True(result);
Assert.Equal(SampleEnum.Option2, returnValue);
}

[Fact]
public void TryGetValue_EnumParameterInvalidValue_ThrowsArgumentException()
{
// Arrange
var navigationParameters = new NavigationParameters();
navigationParameters["EnumParam"] = "InvalidValue";

// Act & Assert
Assert.Throws<ArgumentException>(() => navigationParameters.TryGetValue<SampleEnum>("EnumParam", out var returnValue));
}

[Fact]
public void TryGetValue_ObjectParameter_ReturnsObject()
{
// Arrange
var navigationParameters = new NavigationParameters();
var testObject = new object();

navigationParameters["ObjectParam"] = testObject;

// Act
var result = navigationParameters.TryGetValue<object>("ObjectParam", out var returnValue);

// Assert
Assert.True(result);
Assert.Equal(testObject, returnValue);
}

[Fact]
public void TryGetValue_ObjectParameter_NullValue_ReturnsNull()
{
// Arrange
var navigationParameters = new NavigationParameters();
navigationParameters["ObjectParam"] = null;

// Act
var result = navigationParameters.TryGetValue<object>("ObjectParam", out var returnValue);

// Assert
Assert.True(result);
Assert.Null(returnValue);

}

[Fact]
public void TryGetValue_NullableValueParameters_ReturnsNullValue()
{
// Arrange
var navigationParameters = new NavigationParameters();
navigationParameters["NullableCharParam"] = null;

// Act
var result = navigationParameters.TryGetValue<char?>("NullableCharParam", out var returnValue);

// Assert
Assert.True(result);
Assert.Null(returnValue);
}

[Fact]
public void TryGetValue_NullableValueParameters_ReturnsCharValue()
{
// Arrange
var navigationParameters = new NavigationParameters();
navigationParameters["NullableCharParam"] = 'C';

// Act
var result = navigationParameters.TryGetValue<char?>("NullableCharParam", out var returnValue);

// Assert
Assert.True(result);
Assert.Equal('C', returnValue);
}

// -----------------------------

[Fact]
public void MergeNavigationParameters_WhenCalledWithEmptyDictionaries_ReturnsEmptyDictionary()
{
Expand Down