Skip to content

Commit

Permalink
improve back button for settings views
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi committed Dec 25, 2023
1 parent 2204959 commit fe47679
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Totoro.Core/Contracts/INavigationAware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ public interface INavigationAware

Task OnNavigatedFrom();
}

public interface IHandleNavigation
{
void GoBack();
bool CanHandle();
}
5 changes: 4 additions & 1 deletion Totoro.Core/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Totoro.Core.ViewModels;

public class SettingsViewModel : NavigatableViewModel
public class SettingsViewModel : NavigatableViewModel, IHandleNavigation
{
private readonly ITrackingServiceContext _trackingServiceContext;

Expand Down Expand Up @@ -117,6 +117,9 @@ public SettingsViewModel(ISettings settings,
UpdateConnectionStatus();
}

public bool CanHandle() => BreadCrumbBar.BreadCrumbs.Count > 0;
public void GoBack() => BreadCrumbBar.BreadCrumbs.RemoveAt(BreadCrumbBar.BreadCrumbs.Count - 1);

private void UpdateConnectionStatus()
{
IsMalConnected = _trackingServiceContext.IsTrackerAuthenticated(ListServiceType.MyAnimeList);
Expand Down
15 changes: 11 additions & 4 deletions Totoro.WinUI/Services/NavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,18 @@ public bool GoBack()
{
var vmBeforeNavigation = _frame.GetPageViewModel();

_frame.GoBack();

if (vmBeforeNavigation is INavigationAware navigationAware)
if(vmBeforeNavigation is IHandleNavigation ihn && ihn.CanHandle())
{
navigationAware.OnNavigatedFrom();
ihn.GoBack();
}
else
{
_frame.GoBack();

if (vmBeforeNavigation is INavigationAware navigationAware)
{
navigationAware.OnNavigatedFrom();
}
}

return true;
Expand Down

0 comments on commit fe47679

Please sign in to comment.