From 12e4213f9e7d3fdd3f31c4c5a4d46024ec64502c Mon Sep 17 00:00:00 2001 From: Alex Macocian Date: Mon, 26 Aug 2024 21:59:28 +0200 Subject: [PATCH] NavigationService improvements --- .../ControlFlow/INavigationService.cs | 8 +++++--- .../ControlFlow/NavigationService.cs | 17 ++++++++++++++--- Net.Maui.Extensions/Net.Maui.Extensions.csproj | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Net.Maui.Extensions/ControlFlow/INavigationService.cs b/Net.Maui.Extensions/ControlFlow/INavigationService.cs index 8ad7179..17d84aa 100644 --- a/Net.Maui.Extensions/ControlFlow/INavigationService.cs +++ b/Net.Maui.Extensions/ControlFlow/INavigationService.cs @@ -2,10 +2,12 @@ public interface INavigationService { - void GoTo(bool animated = false) + ContentPage? GetCurrent(); + + T GoTo() where T : ContentPage; - void GoBack(bool animated = false); + void GoBack(); - void GoBackToRoot(bool animated = false); + void GoBackToRoot(); } diff --git a/Net.Maui.Extensions/ControlFlow/NavigationService.cs b/Net.Maui.Extensions/ControlFlow/NavigationService.cs index a0407f8..7659bd5 100644 --- a/Net.Maui.Extensions/ControlFlow/NavigationService.cs +++ b/Net.Maui.Extensions/ControlFlow/NavigationService.cs @@ -22,7 +22,7 @@ internal sealed class NavigationService : INavigationService this.logger = logger.ThrowIfNull(); } - public void GoBack(bool animated = false) + public void GoBack() { var scopedLogger = this.logger.CreateScopedLogger(); if (this.navigationStack.Count <= 1) @@ -40,7 +40,7 @@ internal sealed class NavigationService : INavigationService this.ShowCurrentPage(); } - public void GoBackToRoot(bool animated = false) + public void GoBackToRoot() { var scopedLogger = this.logger.CreateScopedLogger(); scopedLogger.LogDebug("Going back to root"); @@ -52,7 +52,7 @@ internal sealed class NavigationService : INavigationService this.ShowCurrentPage(); } - public void GoTo(bool animated = false) where T : ContentPage + public T GoTo() where T : ContentPage { var scopedLogger = this.logger.CreateScopedLogger(); scopedLogger.LogDebug($"Going to {typeof(T).Name}"); @@ -60,6 +60,17 @@ internal sealed class NavigationService : INavigationService var context = this.GetScopedPageAndProvider(); this.navigationStack.Push(context); this.ShowCurrentPage(); + return context.Page?.ThrowIfNull().Cast().ThrowIfNull()!; + } + + public ContentPage? GetCurrent() + { + if (this.navigationStack.TryPeek(out var context)) + { + return context.Page?.ThrowIfNull(); + } + + return default; } public void SetNavigationRoot(ExtendedApplication extendedApplication, Type rootPageType) diff --git a/Net.Maui.Extensions/Net.Maui.Extensions.csproj b/Net.Maui.Extensions/Net.Maui.Extensions.csproj index 4d52827..59fa6b7 100644 --- a/Net.Maui.Extensions/Net.Maui.Extensions.csproj +++ b/Net.Maui.Extensions/Net.Maui.Extensions.csproj @@ -20,7 +20,7 @@ enable enable true - 0.2.4 + 0.2.5 Alexandru Macocian LICENSE true