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

Implement NavigateFromAsync method without recreating a page. #3182

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions src/Maui/Prism.Maui/Navigation/INavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public interface INavigationService
/// </example>
Task<INavigationResult> NavigateAsync(Uri uri, INavigationParameters parameters);

/// <summary>
/// Initiates navigation from the <paramref name="viewName"/> using the specified <paramref name="route"/>.
/// </summary>
/// <param name="viewName">The name of the View to navigate from</param>
/// <param name="route">The route Uri to navigate from that view</param>
/// <param name="parameters">The navigation parameters</param>
/// <returns>If <c>true</c> a navigate from operation was successful. If <c>false</c> the navigate from operation failed.</returns>
Task<INavigationResult> NavigateFromAsync(string viewName, Uri route, INavigationParameters parameters);

/// <summary>
/// Selects a Tab of the TabbedPage parent and Navigates to a specified Uri
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ public static Task<INavigationResult> NavigateAsync(this INavigationService navi
return navigationService.NavigateAsync(name, GetNavigationParameters(parameters));
}

/// <summary>
/// Initiates navigation to the target specified by the <paramref name="viewName"/> from the <paramref name="route"/>.
/// </summary>
/// <param name="viewName">The name of the View to navigate to</param>
/// <param name="route">The route Uri to navigate to</param>
/// <returns>If <c>true</c> a navigate from operation was successful. If <c>false</c> the navigate from operation failed.</returns>
public static Task<INavigationResult> NavigateFromAsync(this INavigationService navigationService, string viewName, Uri route) =>
navigationService.NavigateFromAsync(viewName, route, new NavigationParameters());
Comment on lines +115 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need some overloads that take a string for the route as well.


/// <summary>
/// Provides an easy to use way to provide an Error Callback without using await NavigationService
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions src/Maui/Prism.Maui/Navigation/PageNavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,44 @@ public virtual async Task<INavigationResult> NavigateAsync(Uri uri, INavigationP
}
}

/// <inheritdoc />
public virtual async Task<INavigationResult> NavigateFromAsync(string viewName, Uri route, INavigationParameters parameters)
{
await WaitForPendingNavigationRequests();

try
{
parameters ??= new NavigationParameters();

NavigationSource = PageNavigationSource.NavigationService;

var routeSegments = UriParsingHelper.GetUriSegments(route);

// Find a page that matches the viewName.
var page = GetCurrentPage();
while (page != null)
{
if (page is not null && ViewModelLocator.GetNavigationName(page) == viewName)
break;
page = page.GetParentPage();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parent page? Shouldn't this be the page before the current page in the stack and if it is the root page then we get the parent page?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AmrAlSayed0 is correct. While we do care about the parent in some scenarios, what we really need is the previous page in the navigation stack.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still some issues remaining, but I have made corrections to use the NavigationStack as described in the following link.

// Find a page that matches the viewName.
var currentPage = GetCurrentPage();
var navigationPages = currentPage.Navigation.NavigationStack.ToList();
navigationPages.Reverse();
var foundPage = navigationPages.FirstOrDefault(page => ViewModelLocator.GetNavigationName(page) == viewName);
if (foundPage is null)
{
// Find a page from parents.
var page = currentPage;
while (page != null)
{
if (page is not null && ViewModelLocator.GetNavigationName(page) == viewName)
break;
page = page.GetParentPage();
}
currentPage = page;
}
else
{
// Insert RemovePageSegment.
var removePageCount = navigationPages.IndexOf(foundPage);
var tempQueue = new Queue<string>();
for (int i = 0; i < removePageCount; i++)
{
tempQueue.Enqueue(RemovePageSegment);
}
while(navigationSegments.Count > 0)
{
tempQueue.Enqueue(navigationSegments.Dequeue());
}
navigationSegments = tempQueue;
}

}

await ProcessNavigation(page, routeSegments, parameters, null, null);

return Notify(route, parameters);
}
catch (Exception ex)
{
return Notify(route, parameters, ex);
}
finally
{
_lastNavigate = DateTime.Now;
NavigationSource = PageNavigationSource.Device;
_semaphore.Release();
}
}

/// <summary>
/// Selects a Tab of the TabbedPage parent.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,57 @@ public async Task Navigation_Animation_IsFalse()
Assert.False(push.Animated);
}

[Fact]
public async Task Navigation_FromPageUsingRoute()
{
var mauiApp = CreateBuilder(prism => prism.CreateWindow("MockHome/NavigationPage/MockViewA"))
.Build();
var window = GetWindow(mauiApp);

var pageNavigatingFrom = (MockHome)window.Page;
var navigationPage = (NavigationPage)pageNavigatingFrom.Detail;
Assert.IsType<MockViewA>(navigationPage.CurrentPage);

var result = await navigationPage.CurrentPage.GetContainerProvider()
.Resolve<INavigationService>()
.NavigateFromAsync("MockHome", UriParsingHelper.Parse("NavigationPage/MockViewB"), null);

Assert.True(result.Success);

// MockHome(FlyoutPage) has not been replaced.
var pageNavigatedFrom = (MockHome)window.Page;
Assert.Equal(pageNavigatingFrom, pageNavigatedFrom);

// Navigation should be succeeded.
navigationPage = (NavigationPage)pageNavigatedFrom.Detail;
Assert.IsType<MockViewB>(navigationPage.CurrentPage);
}

[Fact]
public async Task Navigation_FromIntermediatePageUsingRoute()
{
var mauiApp = CreateBuilder(prism => prism.CreateWindow("MockHome/NavigationPage/MockViewA"))
.Build();
var window = GetWindow(mauiApp);

var mockHome = (MockHome)window.Page;
var pageNavigatingFrom = (NavigationPage)mockHome.Detail;
Assert.IsType<MockViewA>(pageNavigatingFrom.CurrentPage);

var result = await pageNavigatingFrom.CurrentPage.GetContainerProvider()
.Resolve<INavigationService>()
.NavigateFromAsync("NavigationPage", UriParsingHelper.Parse("MockViewB"), null);

Assert.True(result.Success);

// NavigationPage has not been replaced.
var pageNavigatedFrom = (NavigationPage)mockHome.Detail;
Assert.Equal(pageNavigatingFrom, pageNavigatedFrom);

// Navigation should be succeeded.
Assert.IsType<MockViewB>(pageNavigatedFrom.CurrentPage);
}

[Theory]
[InlineData("MockViewA", "MockViewB", null)]
[InlineData("NavigationPage/MockViewA", "MockViewB?useModalNavigation=true", true)]
Expand Down
Loading