Skip to content

Commit

Permalink
Merge pull request #2098 from unoplatform/dev/nr/fixingbacknav
Browse files Browse the repository at this point in the history
fix: free viewmodel and prevent caching of page on back
  • Loading branch information
nickrandolph authored Dec 7, 2023
2 parents 0e8c152 + f24a898 commit eee74a7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/Uno.Extensions.Navigation.UI/Navigators/FrameNavigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public override void ControlInitialize()

if (Control is not null)
{
Control.Navigating += Frame_Navigating;
Control.Navigated += Frame_Navigated;
}
}
Expand Down Expand Up @@ -249,6 +250,22 @@ viewType is null ||
return responseRoute;
}

private void Frame_Navigating(object sender, NavigatingCancelEventArgs e)
{
if( e.NavigationMode==NavigationMode.Back &&
!e.Cancel &&
Control?.Content is Page currentPage)
{
if (Logger.IsEnabled(LogLevel.Debug)) Logger.LogDebugMessage($"Frame has navigating to previous page");

// Force ViewModel to be unset
currentPage.DataContext = null;
// Force page to be disposed
currentPage.NavigationCacheMode = NavigationCacheMode.Disabled;
}
}


private void Frame_Navigated(object sender, NavigationEventArgs e)
{
if (Logger.IsEnabled(LogLevel.Debug)) Logger.LogDebugMessage($"Frame has navigated to page '{e.SourcePageType.Name}'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task GoToTwo()

public async Task GoToTwoData()
{
await Navigator.NavigateDataAsync(this, data:new TwoModel(new ReactiveWidget("From Two",56)));
await Navigator.NavigateDataAsync(this, data:new TwoModel(new ReactiveWidget("From One",56)));
}

public async Task GoToThree()
Expand All @@ -21,7 +21,7 @@ public async Task GoToThree()

public async Task GoToThreeData()
{
await Navigator.NavigateDataAsync(this, data: new ThreeModel(new ReactiveWidget("From Three", 56)));
await Navigator.NavigateDataAsync(this, data: new ThreeModel(new ReactiveWidget("From One", 56)));
}

public async Task ShowDialog()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uen="using:Uno.Extensions.Navigation.UI"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
NavigationCacheMode="Required"
mc:Ignorable="d">

<Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uen="using:Uno.Extensions.Navigation.UI"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
NavigationCacheMode="Required"
mc:Ignorable="d">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,21 @@ public void When_ReactiveDependsOnWithData()

App.WaitElement("OnePageToTwoPageViewModelButton");
var screenBefore = TakeScreenshot("When_PageNavigationViewModel_Before");

// Iterate through pages 1 to 3 - this caused an issue
// where NavigationCacheMode is set to required
// see https://github.com/unoplatform/uno.extensions/issues/2097
App.WaitThenTap("OnePageToTwoPageViewModelButton");
App.WaitThenTap("TwoPageToThreePageViewModelButton");
App.WaitThenTap("ThreePageBackViewModelButton");
App.WaitThenTap("TwoPageBackViewModelButton");
App.WaitElement("OnePageToTwoPageViewModelButton");


App.WaitThenTap("OnePageToThreePageDataButton");
App.WaitElement("ThreePageWidgetNameTextBlock");
var text = App.GetText("ThreePageWidgetNameTextBlock");
Assert.AreEqual("From Three", text);
Assert.AreEqual("From One", text);

App.WaitThenTap("ThreePageBackViewModelButton");
text = App.GetText("TwoPageWidgetNameTextBlock");
Expand Down

0 comments on commit eee74a7

Please sign in to comment.