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

Fix backbutton visibility when popping #5893

Merged
merged 1 commit into from
Apr 11, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void IFlyoutBehaviorObserver.OnFlyoutBehaviorChanged(FlyoutBehavior behavior)
{
if (_flyoutBehavior == behavior)
return;

_flyoutBehavior = behavior;

if (Page != null)
Expand Down Expand Up @@ -80,9 +81,9 @@ public ShellToolbarTracker(IShellContext shellContext, AToolbar toolbar, DrawerL
_platformToolbar.SetNavigationOnClickListener(this);
((IShellController)ShellContext.Shell).AddFlyoutBehaviorObserver(this);
ShellContext.Shell.Toolbar.PropertyChanged += OnToolbarPropertyChanged;
ShellContext.Shell.Navigated += OnShellNavigated;
}


void IShellToolbarTracker.SetToolbar(IToolbar toolbar)
{
_ = toolbar ?? throw new ArgumentNullException(nameof(toolbar));
Expand Down Expand Up @@ -183,6 +184,7 @@ protected override void Dispose(bool disposing)

((IShellController)ShellContext.Shell)?.RemoveFlyoutBehaviorObserver(this);
ShellContext.Shell.Toolbar.PropertyChanged -= OnToolbarPropertyChanged;
ShellContext.Shell.Navigated -= OnShellNavigated;
UpdateTitleView(ShellContext.AndroidContext, _platformToolbar, null);

if (_searchView != null)
Expand Down Expand Up @@ -266,6 +268,18 @@ protected virtual void OnPageChanged(Page oldPage, Page newPage)
}
}

void OnShellNavigated(object sender, ShellNavigatedEventArgs e)
{
if (_disposed || Page == null)
return;

if (ShellContext?.Shell?.Toolbar is ShellToolbar shellToolbar &&
Page == ShellContext?.Shell?.CurrentPage)
{
UpdateLeftBarButtonItem();
}
}

BackButtonBehavior _backButtonBehavior = null;
protected virtual void OnPagePropertyChanged(object sender, PropertyChangedEventArgs e)
{
Expand Down
29 changes: 21 additions & 8 deletions src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ void SetupBuilder()
});
}

[Fact(DisplayName = "Back Button Visibility Changes with push/pop")]
public async Task BackButtonVisibilityChangesWithPushPop()
{
SetupBuilder();
var shell = await CreateShellAsync(shell =>
{
shell.CurrentItem = new ContentPage();
});

await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
Assert.False(IsBackButtonVisible(handler));
await shell.Navigation.PushAsync(new ContentPage());
Assert.True(IsBackButtonVisible(handler));
await shell.Navigation.PopAsync();
Assert.False(IsBackButtonVisible(handler));
});
}

[Fact(DisplayName = "Set Has Back Button")]
public async Task SetHasBackButton()
{
Expand All @@ -52,10 +71,7 @@ public async Task SetHasBackButton()
{
return new Shell()
{
Items =
{
new ContentPage()
}
Items = { new ContentPage() }
};
});

Expand Down Expand Up @@ -86,10 +102,7 @@ public async Task DetailsViewUpdates()
{
return new Shell()
{
Items =
{
new ContentPage()
}
Items = { new ContentPage() }
};
});

Expand Down