Skip to content

Commit

Permalink
Merge pull request #3101 from berhir/remove-legacy-gobackto
Browse files Browse the repository at this point in the history
Add generic GoBack and remove legacy GoBackTo
  • Loading branch information
dansiegel authored Mar 26, 2024
2 parents 33b709e + acee603 commit 2a25770
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 95 deletions.
4 changes: 2 additions & 2 deletions src/Maui/Prism.Maui/Navigation/Builder/INavigationBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Prism.Navigation.Builder;
namespace Prism.Navigation.Builder;

public interface INavigationBuilder
{
Expand All @@ -12,7 +12,7 @@ public interface INavigationBuilder
INavigationBuilder UseAbsoluteNavigation(bool absolute);
INavigationBuilder UseRelativeNavigation();

Task<INavigationResult> GoBackTo<TViewModel>(INavigationParameters parameters);
Task<INavigationResult> GoBackAsync<TViewModel>();
Task<INavigationResult> NavigateAsync();
Task NavigateAsync(Action<Exception> onError);
Task NavigateAsync(Action onSuccess, Action<Exception> onError);
Expand Down
6 changes: 3 additions & 3 deletions src/Maui/Prism.Maui/Navigation/Builder/NavigationBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Prism.Common;
using Prism.Common;
using Prism.Mvvm;

namespace Prism.Navigation.Builder;
Expand Down Expand Up @@ -53,10 +53,10 @@ public INavigationBuilder AddParameter(string key, object value)
return this;
}

public async Task<INavigationResult> GoBackTo<TViewModel>(INavigationParameters parameters)
public async Task<INavigationResult> GoBackAsync<TViewModel>()
{
var name = NavigationBuilderExtensions.GetNavigationKey<TViewModel>(this);
return await _navigationService.GoBackToAsync(name, parameters);
return await _navigationService.GoBackAsync(name, _navigationParameters);
}

public Task<INavigationResult> NavigateAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Prism.Common;
using Prism.Common;
using Prism.Navigation.Builder;

namespace Prism.Navigation;
Expand All @@ -13,9 +13,6 @@ public static class NavigationBuilderExtensions
public static INavigationBuilder CreateBuilder(this INavigationService navigationService) =>
new NavigationBuilder(navigationService);

public static Task<INavigationResult> GoBackTo<TViewModel>(this INavigationBuilder builder) =>
builder.GoBackTo<TViewModel>(null);

internal static string GetNavigationKey<TViewModel>(object builder)
{
var vmType = typeof(TViewModel);
Expand Down
10 changes: 1 addition & 9 deletions src/Maui/Prism.Maui/Navigation/INavigationService.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
namespace Prism.Navigation;
namespace Prism.Navigation;

/// <summary>
/// Provides page based navigation for ViewModels.
/// </summary>
public interface INavigationService
{
/// <summary>
/// Navigates to the most recent entry in the back navigation history by popping the calling Page off the navigation stack.
/// </summary>
/// <param name="name">The name of the View to navigate back to</param>
/// <param name="parameters">The navigation parameters</param>
/// <returns><see cref="INavigationResult"/> indicating whether the request was successful or if there was an encountered <see cref="Exception"/>.</returns>
Task<INavigationResult> GoBackToAsync(string name, INavigationParameters parameters);

/// <summary>
/// Navigates to the most recent entry in the back navigation history by popping the calling Page off the navigation stack.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ namespace Prism.Navigation;
/// </summary>
public static class INavigationServiceExtensions
{
/// <summary>
/// Navigates to the most recent entry in the back navigation history by popping the calling Page off the navigation stack.
/// </summary>
/// <param name="navigationService">Service for handling navigation between views</param>
/// <param name="name">The name of the View to navigate back to</param>
/// <returns><see cref="INavigationResult"/> indicating whether the request was successful or if there was an encountered <see cref="Exception"/>.</returns>
public static Task<INavigationResult> GoBackToAsync(this INavigationService navigationService, string name) =>
navigationService.GoBackToAsync(name, null);

/// <summary>
/// Navigates to the most recent entry in the back navigation history by popping the calling Page off the navigation stack.
/// </summary>
Expand Down
64 changes: 0 additions & 64 deletions src/Maui/Prism.Maui/Navigation/PageNavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,70 +66,6 @@ public PageNavigationService(IContainerProvider container,
_pageAccessor = pageAccessor;
}

/// <summary>
/// Navigates to the most recent entry in the back navigation history by popping the calling Page off the navigation stack.
/// </summary>
/// <param name="name">The name of the View to navigate back to</param>
/// <param name="parameters">The navigation parameters</param>
/// <returns><see cref="INavigationResult"/> indicating whether the request was successful or if there was an encountered <see cref="Exception"/>.</returns>
public virtual async Task<INavigationResult> GoBackToAsync(string name, INavigationParameters parameters)
{
await _semaphore.WaitAsync();
try
{
parameters ??= new NavigationParameters();
NavigationSource = PageNavigationSource.NavigationService;
ArgumentException.ThrowIfNullOrEmpty(name);
if (!Registry.IsRegistered(name))
throw new NavigationException(NavigationException.NoPageIsRegistered, name);

var page = GetCurrentPage();
parameters.GetNavigationParametersInternal().Add(KnownInternalParameters.NavigationMode, NavigationMode.Back);

while (page is not null && ViewModelLocator.GetNavigationName(page) != name)
{
if (IsRoot(GetPageFromWindow(), page))
throw new NavigationException(NavigationException.CannotPopApplicationMainPage, page);

var canNavigate = await MvvmHelpers.CanNavigateAsync(page, parameters);
if (!canNavigate)
{
throw new NavigationException(NavigationException.IConfirmNavigationReturnedFalse, page);
}

var useModalForDoPop = UseModalGoBack(page, parameters);
var previousPage = MvvmHelpers.GetOnNavigatedToTarget(page, Window?.Page, useModalForDoPop);

bool? animated = null;
if (!parameters.TryGetValue<bool>(KnownNavigationParameters.Animated, out var globalAnimated))
{
animated = true;
}

var poppedPage = await DoPop(page.Navigation, useModalForDoPop, animated ?? true);
if (poppedPage != null)
{
MvvmHelpers.OnNavigatedFrom(page, parameters);
MvvmHelpers.OnNavigatedTo(previousPage, parameters);
MvvmHelpers.DestroyPage(poppedPage);
}

page = previousPage;
}

return Notify(NavigationRequestType.GoBack, parameters);
}
catch (Exception ex)
{
return Notify(NavigationRequestType.GoBack, parameters, ex);
}
finally
{
NavigationSource = PageNavigationSource.Device;
_semaphore.Release();
}
}

/// <summary>
/// Navigates to the most recent entry in the back navigation history by popping the calling Page off the navigation stack.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public async Task RelativeNavigation_RemovesPage_AndNavigatesModally()
}

[Fact]
public async Task GoBackTo_Name_PopsToSpecifiedView()
public async Task GoBack_Name_PopsToSpecifiedView()
{
var mauiApp = CreateBuilder(prism => prism.CreateWindow("NavigationPage/MockViewA/MockViewB/MockViewC/MockViewD/MockViewE"))
.Build();
Expand All @@ -237,15 +237,15 @@ public async Task GoBackTo_Name_PopsToSpecifiedView()

var result = await navigationPage.CurrentPage.GetContainerProvider()
.Resolve<INavigationService>()
.GoBackToAsync("MockViewC");
.GoBackAsync("MockViewC");

Assert.True(result.Success);

Assert.IsType<MockViewC>(navigationPage.CurrentPage);
}

[Fact]
public async Task GoBackTo_ViewModel_PopsToSpecifiedView()
public async Task GoBack_ViewModel_PopsToSpecifiedView()
{
var mauiApp = CreateBuilder(prism => prism.CreateWindow("NavigationPage/MockViewA/MockViewB/MockViewC/MockViewD/MockViewE"))
.Build();
Expand All @@ -260,7 +260,7 @@ public async Task GoBackTo_ViewModel_PopsToSpecifiedView()
var result = await navigationPage.CurrentPage.GetContainerProvider()
.Resolve<INavigationService>()
.CreateBuilder()
.GoBackTo<MockViewCViewModel>();
.GoBackAsync<MockViewCViewModel>();

Assert.True(result.Success);

Expand Down

0 comments on commit 2a25770

Please sign in to comment.