Skip to content

Commit 68de9f8

Browse files
rajamattkazo0
andauthored
fix: ios navbar lifecycle (#1331)
* fix: ios navbar lifecycle * chore: fix tests * chore: test * chore: test * chore: wip * chore: fix * chore: fix test --------- Co-authored-by: Steve Bilogan <[email protected]>
1 parent 08a527e commit 68de9f8

14 files changed

+227
-22
lines changed

samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/App.xaml.Navigation.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,17 @@ private Shell BuildShell()
109109
#if USE_UITESTS
110110
private void ForceSampleNavigation(string sampleName)
111111
{
112+
var backdoorParts = sampleName.Split("-");
113+
var title = backdoorParts.FirstOrDefault();
114+
var designName = backdoorParts.Length > 1 ? backdoorParts[1] : string.Empty;
115+
112116
var sample = GetSamples()
113-
.FirstOrDefault(x => string.Equals(x.Title, sampleName, StringComparison.OrdinalIgnoreCase));
117+
.FirstOrDefault(x => string.Equals(x.Title, title, StringComparison.OrdinalIgnoreCase));
118+
119+
if (Enum.TryParse<Design>(designName, out var design))
120+
{
121+
SamplePageLayout.SetPreferredDesign(design);
122+
}
114123

115124
if (sample == null)
116125
{
@@ -207,7 +216,7 @@ object GetCategoryItemIconSource(SampleCategory category)
207216
case SampleCategory.Controls: return Icons.Controls.Control;
208217
case SampleCategory.Tests: return Icons.Tests.Test;
209218

210-
case SampleCategory.Helpers:
219+
case SampleCategory.Helpers:
211220
default: return Icons.Placeholder;
212221
}
213222
}

samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Content/NestedSamples/FluentNavigationBarSampleNestedPage1.xaml

+39-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@
88
xmlns:utu="using:Uno.Toolkit.UI"
99
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
1010
mc:Ignorable="d ios">
11+
<Page.Resources>
12+
<Style x:Key="SmallFlyoutPresenterStyle"
13+
TargetType="FlyoutPresenter">
14+
<Setter Property="MinHeight" Value="300" />
15+
<Setter Property="MinWidth" Value="300" />
16+
</Style>
17+
</Page.Resources>
1118

1219
<Grid>
1320

1421
<Grid.RowDefinitions>
1522
<RowDefinition Height="Auto" />
1623
<RowDefinition Height="*" />
24+
<RowDefinition Height="*" />
1725
</Grid.RowDefinitions>
1826
<utu:NavigationBar Content="First Page"
1927
MainCommandMode="Action"
@@ -22,7 +30,8 @@
2230
<utu:NavigationBar.MainCommand>
2331
<AppBarButton Click="NavigateBack"
2432
Label="Close"
25-
Style="{StaticResource DefaultAppBarButtonStyle}">
33+
AutomationProperties.AutomationId="FluentPage1NavBarMainCommand"
34+
Style="{StaticResource DefaultMainCommandStyle}">
2635
<AppBarButton.Icon>
2736
<BitmapIcon UriSource="ms-appx:///Assets/CloseIcon.png" />
2837
</AppBarButton.Icon>
@@ -72,5 +81,34 @@
7281
<Button Click="NavigateToNextPage"
7382
Content="Navigate To Second Page" />
7483
</StackPanel>
84+
85+
<StackPanel Grid.Row="2"
86+
VerticalAlignment="Stretch">
87+
<Button AutomationProperties.AutomationId="OpenPage2FlyoutButton"
88+
Content="Open Page2 in flyout"
89+
Click="OpenPage2Flyout"
90+
Grid.Row="2">
91+
<Button.Flyout>
92+
<Flyout FlyoutPresenterStyle="{StaticResource SmallFlyoutPresenterStyle}">
93+
<Grid>
94+
<Frame x:Name="Page2FlyoutFrame" />
95+
</Grid>
96+
</Flyout>
97+
</Button.Flyout>
98+
</Button>
99+
100+
<Button AutomationProperties.AutomationId="OpenPage3FlyoutButton"
101+
Content="Open Page3 in flyout"
102+
Click="OpenPage3Flyout"
103+
Grid.Row="2">
104+
<Button.Flyout>
105+
<Flyout FlyoutPresenterStyle="{StaticResource SmallFlyoutPresenterStyle}">
106+
<Grid>
107+
<Frame x:Name="Page3FlyoutFrame" />
108+
</Grid>
109+
</Flyout>
110+
</Button.Flyout>
111+
</Button>
112+
</StackPanel>
75113
</Grid>
76114
</Page>

samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Content/NestedSamples/FluentNavigationBarSampleNestedPage1.xaml.cs

+4
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ public FluentNavigationBarSampleNestedPage()
3131
private void NavigateToNextPage(object sender, RoutedEventArgs e) => Frame.Navigate(typeof(FluentNavigationBarSampleNestedPage2));
3232

3333
private void NavigateBack(object sender, RoutedEventArgs e) => Shell.GetForCurrentView().BackNavigateFromNestedSample();
34+
35+
private void OpenPage2Flyout(object sender, RoutedEventArgs e) => Page2FlyoutFrame.Navigate(typeof(FluentNavigationBarSampleNestedPage2));
36+
37+
private void OpenPage3Flyout(object sender, RoutedEventArgs e) => Page3FlyoutFrame.Navigate(typeof(FluentNavigationBarSampleNestedPage3));
3438
}

samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Content/NestedSamples/FluentNavigationBarSampleNestedPage2.xaml

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
<utu:NavigationBar Content="Second Page"
1717
AutomationProperties.AutomationId="FluentPage2NavBar"
1818
Style="{StaticResource DefaultNavigationBar}">
19+
<utu:NavigationBar.MainCommand>
20+
<AppBarButton Click="NavigateBack"
21+
Label="Close"
22+
AutomationProperties.AutomationId="FluentPage2NavBarMainCommand"
23+
Style="{StaticResource DefaultMainCommandStyle}">
24+
<AppBarButton.Icon>
25+
<BitmapIcon UriSource="ms-appx:///Assets/CloseIcon.png" />
26+
</AppBarButton.Icon>
27+
</AppBarButton>
28+
</utu:NavigationBar.MainCommand>
1929
<utu:NavigationBar.PrimaryCommands>
2030
<AppBarButton Label="More"
2131
Style="{StaticResource DefaultAppBarButtonStyle}" />
@@ -40,6 +50,10 @@
4050
to navigate back.
4151
</TextBlock>
4252

53+
<Button AutomationProperties.AutomationId="NavigateToThirdButton"
54+
Click="NavigateToThird"
55+
Content="Navigate to Third" />
56+
4357
<Button Click="NavigateBack"
4458
Content="Navigate Back" />
4559
</StackPanel>

samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Content/NestedSamples/FluentNavigationBarSampleNestedPage2.xaml.cs

+2
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public FluentNavigationBarSampleNestedPage2()
2424
}
2525

2626
private void NavigateBack(object sender, RoutedEventArgs e) => Frame.GoBack();
27+
28+
private void NavigateToThird(object sender, RoutedEventArgs e) => Frame.Navigate(typeof(FluentNavigationBarSampleNestedPage3));
2729
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Page x:Class="Uno.Toolkit.Samples.Content.NestedSamples.FluentNavigationBarSampleNestedPage3"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="using:Uno.Toolkit.Samples.Content.NestedSamples"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:utu="using:Uno.Toolkit.UI"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
mc:Ignorable="d"
9+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
10+
11+
<Grid>
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="Auto" />
14+
<RowDefinition Height="*" />
15+
</Grid.RowDefinitions>
16+
<utu:NavigationBar Content="Third Page"
17+
AutomationProperties.AutomationId="FluentPage3NavBar"
18+
Style="{StaticResource DefaultNavigationBar}">
19+
<utu:NavigationBar.MainCommand>
20+
<AppBarButton Click="NavigateBack"
21+
Label="Back"
22+
AutomationProperties.AutomationId="FluentPage3NavBarMainCommand"
23+
Style="{StaticResource DefaultMainCommandStyle}">
24+
<AppBarButton.Icon>
25+
<BitmapIcon UriSource="ms-appx:///Assets/CloseIcon.png" />
26+
</AppBarButton.Icon>
27+
</AppBarButton>
28+
</utu:NavigationBar.MainCommand>
29+
</utu:NavigationBar>
30+
31+
<StackPanel Grid.Row="1">
32+
<Button Click="NavigateBack" Content="Navigate back" />
33+
</StackPanel>
34+
</Grid>
35+
</Page>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Runtime.InteropServices.WindowsRuntime;
6+
using Uno.Toolkit.UI;
7+
using Windows.Foundation;
8+
using Windows.Foundation.Collections;
9+
#if IS_WINUI
10+
using Microsoft.UI.Xaml;
11+
using Microsoft.UI.Xaml.Controls;
12+
#else
13+
using Windows.UI.Xaml;
14+
using Windows.UI.Xaml.Controls;
15+
#endif
16+
17+
namespace Uno.Toolkit.Samples.Content.NestedSamples;
18+
19+
public sealed partial class FluentNavigationBarSampleNestedPage3 : Page
20+
{
21+
public FluentNavigationBarSampleNestedPage3()
22+
{
23+
this.InitializeComponent();
24+
}
25+
26+
private void NavigateBack(object sender, RoutedEventArgs e) => Frame.GoBack();
27+
}

samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Controls/SamplePageLayout.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ void OnScrolled(object sender, ScrollViewerViewChangedEventArgs e)
145145
}
146146
}
147147

148+
/// <summary>
149+
/// Changes the preferred design.
150+
/// This doesn't change the current UI. It only affects the next created sample.
151+
/// </summary>
152+
/// <param name="design">The desired design.</param>
153+
public static void SetPreferredDesign(Design design)
154+
{
155+
_design = design;
156+
}
157+
158+
148159
private void UpdateSampleDataContext()
149160
{
150161
if (_sampleContainer is null) return;
@@ -219,7 +230,7 @@ private void UpdateLayoutMode(Design? transitionTo = null)
219230
private double GetRelativeOffset()
220231
{
221232
#if NETFX_CORE
222-
// On UWP we can count on finding a ScrollContentPresenter.
233+
// On UWP we can count on finding a ScrollContentPresenter.
223234
var scp = VisualTreeHelperEx.GetFirstDescendant<ScrollContentPresenter>(_scrollViewer);
224235
var content = scp?.Content as FrameworkElement;
225236
var transform = _scrollingTabs.TransformToVisual(content);

samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Uno.Toolkit.Samples.Shared.projitems

+7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
</Compile>
8686
<Compile Include="$(MSBuildThisFileDirectory)Content\NestedSamples\FluentNavigationBarSampleNestedPage1.xaml.cs" />
8787
<Compile Include="$(MSBuildThisFileDirectory)Content\NestedSamples\FluentNavigationBarSampleNestedPage2.xaml.cs" />
88+
<Compile Include="$(MSBuildThisFileDirectory)Content\NestedSamples\FluentNavigationBarSampleNestedPage3.xaml.cs">
89+
<DependentUpon>FluentNavigationBarSampleNestedPage3.xaml</DependentUpon>
90+
</Compile>
8891
<Compile Include="$(MSBuildThisFileDirectory)Content\NestedSamples\M3MaterialBottomBarSampleNestedPage.xaml.cs">
8992
<DependentUpon>M3MaterialBottomBarSampleNestedPage.xaml</DependentUpon>
9093
</Compile>
@@ -344,6 +347,10 @@
344347
<SubType>Designer</SubType>
345348
<Generator>MSBuild:Compile</Generator>
346349
</Page>
350+
<Page Include="$(MSBuildThisFileDirectory)Content\NestedSamples\FluentNavigationBarSampleNestedPage3.xaml">
351+
<SubType>Designer</SubType>
352+
<Generator>MSBuild:Compile</Generator>
353+
</Page>
347354
<Page Include="$(MSBuildThisFileDirectory)Content\NestedSamples\M3MaterialBottomBarSampleNestedPage.xaml">
348355
<SubType>Designer</SubType>
349356
<Generator>MSBuild:Compile</Generator>

src/Uno.Toolkit.UI/Controls/NavigationBar/NativeNavigationBarPresenter.Android.cs

+6
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ public partial class NativeNavigationBarPresenter
3232

3333
public NativeNavigationBarPresenter()
3434
{
35+
Loaded += OnLoaded;
3536
Unloaded += OnUnloaded;
3637
}
3738

39+
private void OnLoaded(object sender, RoutedEventArgs e)
40+
{
41+
OnOwnerChanged();
42+
}
43+
3844
private void OnUnloaded(object sender, RoutedEventArgs e)
3945
{
4046
_mainCommandClickHandler.Disposable = null;

src/Uno.Toolkit.UI/Controls/NavigationBar/NativeNavigationBarPresenter.iOS.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ public partial class NativeNavigationBarPresenter
4141

4242
public NativeNavigationBarPresenter()
4343
{
44+
Loaded += OnLoaded;
4445
Unloaded += OnUnloaded;
4546
}
4647

48+
private void OnLoaded(object sender, RoutedEventArgs e)
49+
{
50+
OnOwnerChanged();
51+
}
52+
4753
private void OnUnloaded(object sender, RoutedEventArgs e)
4854
{
4955
_statusBarSubscription.Disposable = null;
@@ -141,8 +147,6 @@ protected override Size MeasureOverride(Size size)
141147

142148
return measuredSize;
143149
}
144-
145-
146150
}
147151
}
148152
#endif

src/Uno.Toolkit.UITest/Controls/NavigationBar/Given_NavigationBar.cs

-14
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,5 @@ public void NavBar_Page_Can_Go_Back()
107107

108108
App.WaitForNoElement("M3Page2NavBar", "Timed out waiting for no Page 2 Nav Bar");
109109
}
110-
111-
[Test]
112-
[AutoRetry]
113-
public void NavBar_AppBarButton_With_Icon_Click()
114-
{
115-
NavigateToNestedSample("FluentNavigationBarSampleNestedPage");
116-
117-
PlatformHelpers.On(
118-
iOS: () => App.Tap("FluentPage1NavBarPrimaryCommand3"),
119-
Android: () => App.Tap("FluentPage1NavBarPrimaryCommand3")
120-
);
121-
122-
App.WaitForElement("FluentPage2NavBar", "Timed out waiting for Page 2 Nav Bar");
123-
}
124110
}
125111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using Uno.Toolkit.UITest.Extensions;
8+
using Uno.Toolkit.UITest.Framework;
9+
using Uno.UITest;
10+
using Uno.UITest.Helpers;
11+
using Uno.UITest.Helpers.Queries;
12+
using Uno.UITests.Helpers;
13+
14+
namespace Uno.Toolkit.UITest.Controls.NavigationBar
15+
{
16+
[ActivePlatforms(Platform.Android, Platform.iOS)]
17+
[TestFixture]
18+
public class Given_NavigationBar_Fluent : TestBase
19+
{
20+
protected override string SampleName => "NavigationBar-Fluent";
21+
22+
[Test]
23+
[AutoRetry]
24+
public void NavBar_AppBarButton_With_Icon_Click()
25+
{
26+
NavigateToNestedSample("FluentNavigationBarSampleNestedPage");
27+
28+
PlatformHelpers.On(
29+
iOS: () => App.FastTap("FluentPage1NavBarPrimaryCommand3"),
30+
Android: () => App.FastTap("FluentPage1NavBarPrimaryCommand3")
31+
);
32+
33+
App.WaitForElement("FluentPage2NavBar", "Timed out waiting for Page 2 Nav Bar");
34+
}
35+
36+
[Test]
37+
[AutoRetry]
38+
public void NavBar_Can_Close_Flyout_With_MainCommand()
39+
{
40+
NavigateToNestedSample("FluentNavigationBarSampleNestedPage");
41+
App.FastTap("OpenPage2FlyoutButton");
42+
App.WaitForElement("FluentPage2NavBar", "Timed out waiting for Page 2 Nav Bar");
43+
App.FastTap("NavigateToThirdButton");
44+
App.WaitForElement("FluentPage3NavBar", "Timed out waiting for Page 3 Nav Bar");
45+
46+
PlatformHelpers.On(
47+
iOS: () => App.FastTap("BackButton"),
48+
Android: () => App.FastTap(q => q.Marked("FluentPage3NavBar").Descendant("AppCompatImageButton"))
49+
);
50+
51+
App.WaitForElement("FluentPage2NavBar", "Timed out waiting for Page 2 Nav Bar");
52+
53+
PlatformHelpers.On(
54+
iOS: () => App.FastTap("FluentPage2NavBarMainCommand"),
55+
Android: () => App.FastTap(q => q.Marked("FluentPage2NavBar").Descendant("AppCompatImageButton"))
56+
);
57+
58+
App.WaitForElement("FluentPage1NavBar", "Timed out waiting for Page 1 Nav Bar");
59+
}
60+
}
61+
}

src/Uno.Toolkit.UITest/TestBase.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ protected void NavigateBackFromNestedSample()
130130
InvokeBackdoor("browser:SampleRunner|NavBackFromNestedPage");
131131
}
132132

133-
protected void NavigateToSample(string sample)
133+
protected void NavigateToSample(string sample, string? design = null)
134134
{
135-
InvokeBackdoor("browser:SampleRunner|ForceNavigation", sample);
135+
var navDestination = string.Join("-", sample, design ?? "Material");
136+
InvokeBackdoor("browser:SampleRunner|ForceNavigation", navDestination);
136137
}
137138

138139
protected void NavigateToNestedSample(string pageName)

0 commit comments

Comments
 (0)