Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "GoBack" method which allows you to close the app on final page #50

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ ServiceResolver.Resolve(IExampleService);
```

## Navigation service
`INavigationService` is automatically registered by `.UseBurkusMvvm(...)`. You can use it to: push pages, pop pages, pop to the root page, replace the top page of the app, reset the navigation stack, switch tabs, and more.
`INavigationService` is automatically registered by `.UseBurkusMvvm(...)`. You can use it to: push pages, pop pages, pop to the root page, go back, replace the top page of the app, reset the navigation stack, switch tabs, and more.
See the [INavigationService interface in the repository](https://github.com/BurkusCat/Burkus.Mvvm.Maui/blob/main/src/Abstractions/INavigationService.cs) for all possible navigation method options.

This is a simple navigation example where we push a "`TestPage`" onto the navigation stack:
Expand Down
9 changes: 9 additions & 0 deletions samples/DemoApp/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions samples/DemoApp/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@
<value>Change Username (no animation)</value>
<comment>Translate</comment>
</data>
<data name="Home_Button_Exit" xml:space="preserve">
<value>Exit</value>
<comment>Translate</comment>
</data>
<data name="Home_Button_FlyoutPageDemo" xml:space="preserve">
<value>Flyout Page demo</value>
<comment>Translate</comment>
Expand Down
10 changes: 10 additions & 0 deletions samples/DemoApp/ViewModels/HomeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,15 @@ private async Task GoToFlyoutPageDemo()
await navigationService.Navigate($"{nameof(DemoFlyoutPage)}", navigationParameters);
}

/// <summary>
/// Exit the application.
/// </summary>
[RelayCommand]
private async Task Exit()
{
// Pop the homepage off the stack, closing the app.
await navigationService.GoBack();
}

#endregion Commands
}
5 changes: 5 additions & 0 deletions samples/DemoApp/Views/HomePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
Command="{Binding GoToFlyoutPageDemoCommand}"
Style="{StaticResource PrimaryButton}"
Text="{x:Static properties:Resources.Home_Button_FlyoutPageDemo}" />
<Button
Margin="0,40,0,0"
Command="{Binding ExitCommand}"
Style="{StaticResource PrimaryButton}"
Text="{x:Static properties:Resources.Home_Button_Exit}" />
</VerticalStackLayout>
</ScrollView>
</burkus:BurkusContentPage>
13 changes: 13 additions & 0 deletions src/Burkus.Mvvm.Maui/Abstractions/INavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ Task Push<T>(NavigationParameters navigationParameters)

#region Advanced navigation methods

/// <summary>
/// Pops the top page of the navigation stack or closes the app if it is the last page.
/// </summary>
/// <returns>A completed task</returns>
Task GoBack();

/// <summary>
/// Pops the top page of the navigation stack or closes the app if it is the last page.
/// </summary>
/// <param name="navigationParameters">Navigation parameters to pass</param>
/// <returns>A completed task</returns>
Task GoBack(NavigationParameters navigationParameters);

/// <summary>
/// Replace the top page of the stack with a new page.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Burkus.Mvvm.Maui/Models/Pages/BackButtonNavigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal static bool HandleBackButtonPressed()
{
var navigationService = ServiceResolver.Resolve<INavigationService>();

_ = navigationService.Pop();
_ = navigationService.GoBack();

// On Android and Windows, prevent the default back button behaviour
return true;
Expand Down
29 changes: 29 additions & 0 deletions src/Burkus.Mvvm.Maui/Services/NavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ await HandleNavigation<Page>(async () =>

#region Advanced navigation methods

public async Task GoBack()
{
await GoBack(new NavigationParameters());
}

public async Task GoBack(NavigationParameters navigationParameters)
{
await HandleNavigation<Page>(async () =>
{
if (navigationParameters.UseModalNavigation)
{
_ = await Application.Current.MainPage.Navigation.PopModalAsync(navigationParameters.UseAnimatedNavigation);
}
else
{
if (Application.Current.MainPage.Navigation.NavigationStack.Count <= 1)
{
// quit the app as this page is the last one
Application.Current.Quit();
}
else
{
_ = await Application.Current.MainPage.Navigation.PopAsync(navigationParameters.UseAnimatedNavigation);
}
}
},
navigationParameters);
}

public async Task ReplaceTopPage<T>()
where T : Page
{
Expand Down
13 changes: 13 additions & 0 deletions tests/DemoApp.UnitTests/ViewModels/HomeViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,17 @@ public void AddMultiplePagesCommand_WhenCalled_NavigatesToSeveralPages()
// Assert
mockNavigationService.Received().Navigate("DemoTabsPage/RegisterPage/UriTestPage");
}

[Fact]
public void ExitCommand_WhenCalled_ClosesTheApp()
{
// Arrange
var viewModel = ViewModel;

// Act
viewModel.ExitCommand.Execute(null);

// Assert
mockNavigationService.Received().GoBack();
}
}
Loading