Skip to content

Commit

Permalink
chore: migrating NavigationResult to Prism.Core
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Aug 26, 2023
1 parent fb9bbf9 commit ee829f5
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 160 deletions.
2 changes: 1 addition & 1 deletion e2e/Forms/src/HelloRegions/ViewModels/RegionDemoBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
Expand Down
18 changes: 0 additions & 18 deletions src/Forms/Prism.Forms/Navigation/INavigationResult.cs

This file was deleted.

32 changes: 0 additions & 32 deletions src/Forms/Prism.Forms/Navigation/NavigationException.cs

This file was deleted.

42 changes: 19 additions & 23 deletions src/Forms/Prism.Forms/Navigation/PageNavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public virtual Task<INavigationResult> GoBackAsync(INavigationParameters paramet
/// <returns>If <c>true</c> a go back operation was successful. If <c>false</c> the go back operation failed.</returns>
protected async virtual Task<INavigationResult> GoBackInternal(INavigationParameters parameters, bool? useModalNavigation, bool animated)
{
var result = new NavigationResult();
Page page = null;
try
{
Expand All @@ -102,8 +101,10 @@ protected async virtual Task<INavigationResult> GoBackInternal(INavigationParame
var canNavigate = await PageUtilities.CanNavigateAsync(page, segmentParameters);
if (!canNavigate)
{
result.Exception = new NavigationException(NavigationException.IConfirmNavigationReturnedFalse, page);
return result;
return new NavigationResult
{
Exception = new NavigationException(NavigationException.IConfirmNavigationReturnedFalse, page)
};
}

bool useModalForDoPop = UseModalGoBack(page, useModalNavigation);
Expand All @@ -116,22 +117,22 @@ protected async virtual Task<INavigationResult> GoBackInternal(INavigationParame
PageUtilities.OnNavigatedTo(previousPage, segmentParameters);
PageUtilities.DestroyPage(poppedPage);

result.Success = true;
return result;
return new NavigationResult();
}
}
catch (Exception ex)
{
result.Exception = ex;
return result;
return new NavigationResult { Exception = ex };
}
finally
{
NavigationSource = PageNavigationSource.Device;
}

result.Exception = GetGoBackException(page, _applicationProvider.MainPage);
return result;
return new NavigationResult
{
Exception = GetGoBackException(page, _applicationProvider.MainPage)
};
}

private static Exception GetGoBackException(Page currentPage, Page mainPage)
Expand Down Expand Up @@ -196,7 +197,6 @@ public virtual Task<INavigationResult> GoBackToRootAsync(INavigationParameters p
/// <remarks>Only works when called from a View within a NavigationPage</remarks>
protected async virtual Task<INavigationResult> GoBackToRootInternal(INavigationParameters parameters)
{
var result = new NavigationResult();
Page page = null;
try
{
Expand All @@ -209,8 +209,10 @@ protected async virtual Task<INavigationResult> GoBackToRootInternal(INavigation
var canNavigate = await PageUtilities.CanNavigateAsync(page, parameters);
if (!canNavigate)
{
result.Exception = new NavigationException(NavigationException.IConfirmNavigationReturnedFalse, page);
return result;
return new NavigationResult
{
Exception = new NavigationException(NavigationException.IConfirmNavigationReturnedFalse, page)
};
}

List<Page> pagesToDestroy = page.Navigation.NavigationStack.ToList(); // get all pages to destroy
Expand All @@ -228,13 +230,11 @@ protected async virtual Task<INavigationResult> GoBackToRootInternal(INavigation

PageUtilities.OnNavigatedTo(root, parameters);

result.Success = true;
return result;
return new NavigationResult();
}
catch (Exception ex)
{
result.Exception = ex;
return result;
return new NavigationResult { Exception = ex };
}
}

Expand Down Expand Up @@ -341,7 +341,6 @@ public virtual Task<INavigationResult> NavigateAsync(Uri uri, INavigationParamet
/// </example>
protected async virtual Task<INavigationResult> NavigateInternal(Uri uri, INavigationParameters parameters, bool? useModalNavigation, bool animated)
{
var result = new NavigationResult();
try
{
NavigationSource = PageNavigationSource.NavigationService;
Expand All @@ -351,21 +350,18 @@ protected async virtual Task<INavigationResult> NavigateInternal(Uri uri, INavig
if (uri.IsAbsoluteUri)
{
await ProcessNavigationForAbsoulteUri(navigationSegments, parameters, useModalNavigation, animated);
result.Success = true;
return result;
return new NavigationResult();
}
else
{
await ProcessNavigation(GetCurrentPage(), navigationSegments, parameters, useModalNavigation, animated);
result.Success = true;
return result;
return new NavigationResult();
}

}
catch (Exception ex)
{
result.Exception = ex;
return result;
return new NavigationResult { Exception = ex };
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static async Task<INavigationResult> SelectTabAsync(this INavigationServi
return new NavigationResult { Exception = ex };
}

return new NavigationResult { Success = true };
return new NavigationResult();
}
}
}
12 changes: 0 additions & 12 deletions src/Maui/Prism.Maui/Navigation/INavigationResult.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/Maui/Prism.Maui/Navigation/NavigationResult.cs

This file was deleted.

32 changes: 32 additions & 0 deletions src/Prism.Core/Navigation/INavigationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using Prism.Regions;

namespace Prism.Navigation;

#nullable enable
/// <summary>
/// Provides a wrapper for a Navigation Result
/// </summary>
public interface INavigationResult
{
/// <summary>
/// Indicates that the navigation was successful and no Navigation Errors occurred
/// </summary>
bool Success { get; }

/// <summary>
/// If <c>true</c> this indicates that the Navigation Event was cancelled.
/// </summary>
bool Cancelled { get; }

/// <summary>
/// The Exception if one occurred.
/// </summary>
Exception? Exception { get; }

/// <summary>
/// If the <see cref="INavigationResult"/> is the result of Region Navigation
/// This will provide the associate <see cref="NavigationContext"/>
/// </summary>
NavigationContext? Context { get; }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Prism.Navigation;

public class NavigationException : Exception
Expand Down Expand Up @@ -25,7 +27,7 @@ public NavigationException(string message)
{
}

public NavigationException(string message, VisualElement view)
public NavigationException(string message, object view)
: this(message, view, null)
{
}
Expand All @@ -40,18 +42,24 @@ public NavigationException(string message, string navigationKey, Exception inner
{
}

public NavigationException(string message, VisualElement view, Exception innerException)
public NavigationException(string message, object view, Exception innerException)
: this(message, null, view, innerException)
{
}

public NavigationException(string message, string navigationKey, VisualElement view, Exception innerException) : base(message, innerException)
public NavigationException(string message, string navigationKey, object view, Exception innerException) : base(message, innerException)
{
View = view;
NavigationKey = navigationKey;
}

public VisualElement View { get; }
/// <summary>
/// The View Instance
/// </summary>
public object View { get; }

/// <summary>
/// The key used for the failed navigation
/// </summary>
public string NavigationKey { get; }
}
}
65 changes: 65 additions & 0 deletions src/Prism.Core/Navigation/NavigationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using Prism.Regions;

namespace Prism.Navigation;

#nullable enable
/// <summary>
/// Default implementation for the <see cref="INavigationResult"/>
/// </summary>
public record NavigationResult : INavigationResult
{
private readonly bool? _success;

/// <summary>
/// Initializes a new Navigation Result
/// </summary>
public NavigationResult()
{
}

/// <summary>
/// Initializes a new NavigationResult
/// </summary>
/// <param name="success"></param>
public NavigationResult(bool success)
{
_success = success;
}

/// <summary>
/// Initializes a new NavigationResult
/// </summary>
/// <param name="context"></param>
/// <param name="success"></param>
public NavigationResult(NavigationContext context, bool success)
{
Context = context;
_success = success;
}

/// <summary>
/// Initializes a new NavigationResult
/// </summary>
/// <param name="context"></param>
/// <param name="exception"></param>
public NavigationResult(NavigationContext context, Exception exception)
{
Context = context;
Exception = exception;
}

/// <inheritdoc />
public bool Success => _success ?? Exception is null;

/// <inheritdoc />
public bool Cancelled =>
Exception is NavigationException navigationException
&& navigationException.Message == NavigationException.IConfirmNavigationReturnedFalse;

/// <inheritdoc />
public Exception? Exception { get; init; }

/// <inheritdoc />
public NavigationContext? Context { get; init; }
}
Loading

0 comments on commit ee829f5

Please sign in to comment.