From ee829f51a8ce4c0854b9797f7122a80e1d444143 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Sat, 26 Aug 2023 10:29:52 -0600 Subject: [PATCH] chore: migrating NavigationResult to Prism.Core --- .../HelloRegions/ViewModels/RegionDemoBase.cs | 2 +- .../Navigation/INavigationResult.cs | 18 ----- .../Navigation/NavigationException.cs | 32 --------- .../Navigation/PageNavigationService.cs | 42 ++++++------ .../INavigationServiceExtensions.cs | 2 +- .../Navigation/INavigationResult.cs | 12 ---- .../Prism.Maui/Navigation/NavigationResult.cs | 12 ---- .../Navigation/INavigationResult.cs | 32 +++++++++ .../Navigation/NavigationException.cs | 18 +++-- src/Prism.Core/Navigation/NavigationResult.cs | 65 +++++++++++++++++++ src/Prism.Core/Regions/NavigationResult.cs | 51 --------------- .../Regions/RegionNavigationJournal.cs | 10 +-- 12 files changed, 136 insertions(+), 160 deletions(-) delete mode 100644 src/Forms/Prism.Forms/Navigation/INavigationResult.cs delete mode 100644 src/Forms/Prism.Forms/Navigation/NavigationException.cs delete mode 100644 src/Maui/Prism.Maui/Navigation/INavigationResult.cs delete mode 100644 src/Maui/Prism.Maui/Navigation/NavigationResult.cs create mode 100644 src/Prism.Core/Navigation/INavigationResult.cs rename src/{Maui/Prism.Maui => Prism.Core}/Navigation/NavigationException.cs (85%) create mode 100644 src/Prism.Core/Navigation/NavigationResult.cs delete mode 100644 src/Prism.Core/Regions/NavigationResult.cs diff --git a/e2e/Forms/src/HelloRegions/ViewModels/RegionDemoBase.cs b/e2e/Forms/src/HelloRegions/ViewModels/RegionDemoBase.cs index d0a6c21d8b..0d0bfd46b6 100644 --- a/e2e/Forms/src/HelloRegions/ViewModels/RegionDemoBase.cs +++ b/e2e/Forms/src/HelloRegions/ViewModels/RegionDemoBase.cs @@ -1,4 +1,4 @@ -using System; +using System; using Prism.Commands; using Prism.Mvvm; using Prism.Navigation; diff --git a/src/Forms/Prism.Forms/Navigation/INavigationResult.cs b/src/Forms/Prism.Forms/Navigation/INavigationResult.cs deleted file mode 100644 index f76eda462b..0000000000 --- a/src/Forms/Prism.Forms/Navigation/INavigationResult.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -namespace Prism.Navigation -{ - public interface INavigationResult - { - bool Success { get; } - - Exception Exception { get; } - } - - public class NavigationResult : INavigationResult - { - public bool Success { get; set; } - - public Exception Exception { get; set; } - } -} diff --git a/src/Forms/Prism.Forms/Navigation/NavigationException.cs b/src/Forms/Prism.Forms/Navigation/NavigationException.cs deleted file mode 100644 index 19159706a0..0000000000 --- a/src/Forms/Prism.Forms/Navigation/NavigationException.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using Xamarin.Forms; - -namespace Prism.Navigation -{ - public class NavigationException : Exception - { - public const string CannotPopApplicationMainPage = "Cannot Pop Application MainPage"; - public const string CannotGoBackFromRoot = "Cannot GoBack from NavigationPage Root."; - public const string GoBackToRootRequiresNavigationPage = "GoBackToRootAsync can only be called when the calling Page is within a NavigationPage."; - public const string RelativeNavigationRequiresNavigationPage = "Removing views using the relative '../' syntax while navigating is only supported within a NavigationPage"; - public const string IConfirmNavigationReturnedFalse = "IConfirmNavigation returned false"; - public const string NoPageIsRegistered = "No Page has been registered with the provided key"; - public const string ErrorCreatingPage = "An error occurred while resolving the page. This is most likely the result of invalid XAML or other type initialization exception"; - public const string UnknownException = "An unknown error occurred. You may need to specify whether to Use Modal Navigation or not."; - - public NavigationException() - { - } - - public NavigationException(string message, Page page) : this(message, page, null) - { - } - - public NavigationException(string message, Page page, Exception innerException) : base(message, innerException) - { - Page = page; - } - - public Page Page { get; } - } -} diff --git a/src/Forms/Prism.Forms/Navigation/PageNavigationService.cs b/src/Forms/Prism.Forms/Navigation/PageNavigationService.cs index 103078447b..dd387c818a 100644 --- a/src/Forms/Prism.Forms/Navigation/PageNavigationService.cs +++ b/src/Forms/Prism.Forms/Navigation/PageNavigationService.cs @@ -86,7 +86,6 @@ public virtual Task GoBackAsync(INavigationParameters paramet /// If true a go back operation was successful. If false the go back operation failed. protected async virtual Task GoBackInternal(INavigationParameters parameters, bool? useModalNavigation, bool animated) { - var result = new NavigationResult(); Page page = null; try { @@ -102,8 +101,10 @@ protected async virtual Task 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); @@ -116,22 +117,22 @@ protected async virtual Task 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) @@ -196,7 +197,6 @@ public virtual Task GoBackToRootAsync(INavigationParameters p /// Only works when called from a View within a NavigationPage protected async virtual Task GoBackToRootInternal(INavigationParameters parameters) { - var result = new NavigationResult(); Page page = null; try { @@ -209,8 +209,10 @@ protected async virtual Task 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 pagesToDestroy = page.Navigation.NavigationStack.ToList(); // get all pages to destroy @@ -228,13 +230,11 @@ protected async virtual Task 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 }; } } @@ -341,7 +341,6 @@ public virtual Task NavigateAsync(Uri uri, INavigationParamet /// protected async virtual Task NavigateInternal(Uri uri, INavigationParameters parameters, bool? useModalNavigation, bool animated) { - var result = new NavigationResult(); try { NavigationSource = PageNavigationSource.NavigationService; @@ -351,21 +350,18 @@ protected async virtual Task 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 { diff --git a/src/Forms/Prism.Forms/Navigation/TabbedPages/INavigationServiceExtensions.cs b/src/Forms/Prism.Forms/Navigation/TabbedPages/INavigationServiceExtensions.cs index 7aa531391a..69a8d6de10 100644 --- a/src/Forms/Prism.Forms/Navigation/TabbedPages/INavigationServiceExtensions.cs +++ b/src/Forms/Prism.Forms/Navigation/TabbedPages/INavigationServiceExtensions.cs @@ -80,7 +80,7 @@ public static async Task SelectTabAsync(this INavigationServi return new NavigationResult { Exception = ex }; } - return new NavigationResult { Success = true }; + return new NavigationResult(); } } } diff --git a/src/Maui/Prism.Maui/Navigation/INavigationResult.cs b/src/Maui/Prism.Maui/Navigation/INavigationResult.cs deleted file mode 100644 index f40131b5cf..0000000000 --- a/src/Maui/Prism.Maui/Navigation/INavigationResult.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Prism.Navigation; - -public interface INavigationResult -{ - bool Success => Exception is null; - - bool Cancelled => - Exception is NavigationException navigationException - && navigationException.Message == NavigationException.IConfirmNavigationReturnedFalse; - - Exception Exception { get; } -} diff --git a/src/Maui/Prism.Maui/Navigation/NavigationResult.cs b/src/Maui/Prism.Maui/Navigation/NavigationResult.cs deleted file mode 100644 index e561748c49..0000000000 --- a/src/Maui/Prism.Maui/Navigation/NavigationResult.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Prism.Navigation; - -public record NavigationResult : INavigationResult -{ - public bool Success => Exception is null; - - public bool Cancelled => - Exception is NavigationException navigationException - && navigationException.Message == NavigationException.IConfirmNavigationReturnedFalse; - - public Exception Exception { get; init; } -} diff --git a/src/Prism.Core/Navigation/INavigationResult.cs b/src/Prism.Core/Navigation/INavigationResult.cs new file mode 100644 index 0000000000..aef44b44b3 --- /dev/null +++ b/src/Prism.Core/Navigation/INavigationResult.cs @@ -0,0 +1,32 @@ +using System; +using Prism.Regions; + +namespace Prism.Navigation; + +#nullable enable +/// +/// Provides a wrapper for a Navigation Result +/// +public interface INavigationResult +{ + /// + /// Indicates that the navigation was successful and no Navigation Errors occurred + /// + bool Success { get; } + + /// + /// If true this indicates that the Navigation Event was cancelled. + /// + bool Cancelled { get; } + + /// + /// The Exception if one occurred. + /// + Exception? Exception { get; } + + /// + /// If the is the result of Region Navigation + /// This will provide the associate + /// + NavigationContext? Context { get; } +} diff --git a/src/Maui/Prism.Maui/Navigation/NavigationException.cs b/src/Prism.Core/Navigation/NavigationException.cs similarity index 85% rename from src/Maui/Prism.Maui/Navigation/NavigationException.cs rename to src/Prism.Core/Navigation/NavigationException.cs index 848c671432..f1d39146bb 100644 --- a/src/Maui/Prism.Maui/Navigation/NavigationException.cs +++ b/src/Prism.Core/Navigation/NavigationException.cs @@ -1,3 +1,5 @@ +using System; + namespace Prism.Navigation; public class NavigationException : Exception @@ -25,7 +27,7 @@ public NavigationException(string message) { } - public NavigationException(string message, VisualElement view) + public NavigationException(string message, object view) : this(message, view, null) { } @@ -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; } + /// + /// The View Instance + /// + public object View { get; } + /// + /// The key used for the failed navigation + /// public string NavigationKey { get; } -} \ No newline at end of file +} diff --git a/src/Prism.Core/Navigation/NavigationResult.cs b/src/Prism.Core/Navigation/NavigationResult.cs new file mode 100644 index 0000000000..69198f3504 --- /dev/null +++ b/src/Prism.Core/Navigation/NavigationResult.cs @@ -0,0 +1,65 @@ +using System; +using Prism.Regions; + +namespace Prism.Navigation; + +#nullable enable +/// +/// Default implementation for the +/// +public record NavigationResult : INavigationResult +{ + private readonly bool? _success; + + /// + /// Initializes a new Navigation Result + /// + public NavigationResult() + { + } + + /// + /// Initializes a new NavigationResult + /// + /// + public NavigationResult(bool success) + { + _success = success; + } + + /// + /// Initializes a new NavigationResult + /// + /// + /// + public NavigationResult(NavigationContext context, bool success) + { + Context = context; + _success = success; + } + + /// + /// Initializes a new NavigationResult + /// + /// + /// + public NavigationResult(NavigationContext context, Exception exception) + { + Context = context; + Exception = exception; + } + + /// + public bool Success => _success ?? Exception is null; + + /// + public bool Cancelled => + Exception is NavigationException navigationException + && navigationException.Message == NavigationException.IConfirmNavigationReturnedFalse; + + /// + public Exception? Exception { get; init; } + + /// + public NavigationContext? Context { get; init; } +} diff --git a/src/Prism.Core/Regions/NavigationResult.cs b/src/Prism.Core/Regions/NavigationResult.cs deleted file mode 100644 index 82bc6ed764..0000000000 --- a/src/Prism.Core/Regions/NavigationResult.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; - -namespace Prism.Regions -{ - /// - /// Represents the result of navigating to a URI. - /// - public class NavigationResult - { - /// - /// Initializes a new instance of the class. - /// - /// The context. - /// The result. - public NavigationResult(NavigationContext context, bool? result) - { - Context = context; - Result = result; - } - - /// - /// Initializes a new instance of the class. - /// - /// The context. - /// The error. - public NavigationResult(NavigationContext context, Exception error) - { - Context = context; - Error = error; - Result = false; - } - - /// - /// Gets the result. - /// - /// The result. - public bool? Result { get; private set; } - - /// - /// Gets an exception that occurred while navigating. - /// - /// The exception. - public Exception Error { get; private set; } - - /// - /// Gets the navigation context. - /// - /// The navigation context. - public NavigationContext Context { get; private set; } - } -} diff --git a/src/Prism.Core/Regions/RegionNavigationJournal.cs b/src/Prism.Core/Regions/RegionNavigationJournal.cs index 75ec8dcd21..bce7af269a 100644 --- a/src/Prism.Core/Regions/RegionNavigationJournal.cs +++ b/src/Prism.Core/Regions/RegionNavigationJournal.cs @@ -141,16 +141,16 @@ public void Clear() private void InternalNavigate(IRegionNavigationJournalEntry entry, Action callback) { - this.isNavigatingInternal = true; - this.NavigationTarget.RequestNavigate( + isNavigatingInternal = true; + NavigationTarget.RequestNavigate( entry.Uri, nr => { - this.isNavigatingInternal = false; + isNavigatingInternal = false; - if (nr.Result.HasValue) + if (nr.Success) { - callback(nr.Result.Value); + callback(nr.Success); } }, entry.Parameters);