Skip to content

Commit

Permalink
Back to a specific page
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sadegh-sh committed Mar 14, 2024
1 parent b28ca1b commit 1384b88
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Core/Navigation/Nav.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,15 @@ public static async Task Back()
await Go(destinationPage, previousPage.NavParams, transision, clearStack: false, revisiting: revisition);
}

/// <summary>
/// Removes the current page from the stack.
/// </summary>
public static void Pop()
{
if (Stack.None()) throw new InvalidStateException("There is no previous page in the stack to pop.");
Stack.Pop();
}

/// <summary>
/// Warmp-up cachable target page to enable to navigate faster.
/// </summary>
Expand Down
7 changes: 4 additions & 3 deletions Core/ViewModel/ViewModelNavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ partial void Configure()
RealGo = () => Nav.Go(RealPage, Transition);
RealForward = () => Nav.Forward(RealPage, Transition);
RealReplace = () => Nav.Replace(RealPage, Transition);
RealReload = () => Nav.Reload();
RealBack = () => Nav.Back();
RealHidePopup = () => Nav.HidePopUp();
RealReload = Nav.Reload;
RealBack = Nav.Back;
RealPop = Nav.Pop;
RealHidePopup = Nav.HidePopUp;
RealShowPopup = () => Nav.ShowPopUp(RealModal, Transition);
}
}
Expand Down
18 changes: 18 additions & 0 deletions Mvvm/Mvvm/ViewModel.Nav.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ public abstract partial class ViewModel

public static bool CanGoBack() => Stack.Any();

public static Task Back<TDestination>(PageTransition transition = PageTransition.SlideBack)
where TDestination : FullScreen
{
return Back(The<TDestination>(), transition);
}

public static async Task Back(FullScreen target, PageTransition transition = PageTransition.SlideBack)
{
if (Stack.None()) throw new InvalidStateException("There is no previous page in the stack to go back to.");

if (Stack.Lacks(target)) throw new InvalidStateException("Stack lacks the provided page.");

while (Stack.Pop() != target)
ViewModelNavigation.Pop();

await new ViewModelNavigation(target, transition).Back();
}

public static Task Back(PageTransition transition = PageTransition.SlideBack)
{
if (Stack.None()) throw new InvalidStateException("There is no previous page in the stack to go back to.");
Expand Down
3 changes: 3 additions & 0 deletions Mvvm/Mvvm/ViewModelNavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ partial class ViewModelNavigation
public Func<Task> RealGo, RealBack, RealForward, RealReplace, RealShowPopup;
public static Func<Task> RealReload;
Func<Task> RealHidePopup = () => Task.CompletedTask;
public static Action RealPop;
readonly ViewModel Target;

public ViewModelNavigation(ViewModel target, PageTransition transition)
Expand Down Expand Up @@ -39,6 +40,8 @@ public ViewModelNavigation(ViewModel target, PageTransition transition)

public Task Back() => RunFullPage(RealBack);

public static void Pop() => RealPop();

public async Task HidePopUp()
{
ViewModel.Modals.TryPop(out var modal);
Expand Down
2 changes: 1 addition & 1 deletion Zebble/Zebble.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RootNamespace>Zebble</RootNamespace>
<PackageId>Zebble</PackageId>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<Version>5.0.6.0</Version>
<Version>5.0.7.0</Version>
<PackOnBuild>true</PackOnBuild>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
Expand Down

0 comments on commit 1384b88

Please sign in to comment.