Skip to content

Commit eee0535

Browse files
Vignesh-SF3580PureWeen
authored andcommitted
[iOS] Fixed Shell Navigating event showing same current and target values (#25749)
### Issue Detail In the Shell Navigating event, the Current and Target locations are the same. ### Root Cause The route comparison logic did not properly check the navigation state, leading to redundant route values in routeStack. ### Description of Change Updated the route validation logic to correctly compare and update routeStack. If the new route matches the existing one at the expected position, redundant elements in routeStack are removed to maintain an accurate navigation state. ### Tested the behaviour in the following platforms - [x] Android - [x] Windows - [x] iOS - [x] Mac ### Issues Fixed Fixes #25599 ### Screenshots | Before Issue Fix | After Issue Fix | |----------|----------| | <video width="300" height="600" src="https://github.com/user-attachments/assets/2706b3c6-f289-4eb7-9f4a-9475898bff87"> | <video width="300" height="600" src="https://github.com/user-attachments/assets/86c72f4c-f6b3-4a7c-b39c-2245fa93d885">) |
1 parent 5ad59f0 commit eee0535

3 files changed

Lines changed: 180 additions & 0 deletions

File tree

src/Controls/src/Core/Shell/ShellNavigationManager.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,24 @@ public static ShellNavigationState GetNavigationState(ShellItem shellItem, Shell
575575
}
576576
}
577577

578+
#if IOS || MACCATALYST
579+
if (Shell.Current?.CurrentState?.Location is not null)
580+
{
581+
var currentRoute = Shell.Current?.CurrentState?.Location?.ToString();
582+
if (!string.IsNullOrEmpty(currentRoute))
583+
{
584+
var currentPaths = new List<string>(currentRoute.Split('/'));
585+
// Indices 0 and 1 of both routeStack and currentPaths are dummy/empty values
586+
// The first meaningful route segment is at index 2.
587+
if (currentPaths.Count == routeStack.Count && currentPaths.Count > 3 && currentPaths[2] == routeStack[2])
588+
{
589+
// Current route is same as the new route, so remove the last elements of the routeStack
590+
routeStack.RemoveRange(3, routeStack.Count - 3);
591+
}
592+
}
593+
}
594+
#endif
595+
578596
if (routeStack.Count > 0)
579597
routeStack.Insert(0, "/");
580598

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 25599_1, "Shell Navigating event shows identical Current and Target on tab click", PlatformAffected.iOS | PlatformAffected.macOS)]
4+
public class Issue25599_1 : Shell
5+
{
6+
public Issue25599_1()
7+
{
8+
var tabBar = new TabBar();
9+
10+
var homeTab = new Tab
11+
{
12+
Title = "Home"
13+
};
14+
var homeShellContent = new ShellContent
15+
{
16+
ContentTemplate = new DataTemplate(() => new Issue25599_1MainPage()),
17+
Route = "MainPage"
18+
};
19+
homeTab.Items.Add(homeShellContent);
20+
tabBar.Items.Add(homeTab);
21+
22+
var settingsTab = new Tab
23+
{
24+
Title = "Settings"
25+
};
26+
var settingsShellContent = new ShellContent
27+
{
28+
ContentTemplate = new DataTemplate(() => new Issue25599_1DetailPage()),
29+
Route = "DetailPage"
30+
};
31+
settingsTab.Items.Add(settingsShellContent);
32+
tabBar.Items.Add(settingsTab);
33+
Items.Add(tabBar);
34+
}
35+
}
36+
37+
public class Issue25599_1MainPage : ContentPage
38+
{
39+
Label navigatedLabel;
40+
Label navigatingCurrentLabel;
41+
Label navigatingTargetLabel;
42+
43+
public Issue25599_1MainPage()
44+
{
45+
var layout = new StackLayout
46+
{
47+
Padding = 20,
48+
Spacing = 10
49+
};
50+
51+
var button = new Button
52+
{
53+
Text = "Push Detail Page",
54+
AutomationId = "PushButton"
55+
};
56+
button.Clicked += async (s, e) =>
57+
{
58+
await Navigation.PushAsync(new Issue25599_1DetailPage());
59+
};
60+
61+
navigatingCurrentLabel = new Label
62+
{
63+
Text = "Navigating Current: Not triggered",
64+
AutomationId = "MainPageNavigatingCurrentLabel",
65+
FontSize = 12,
66+
LineBreakMode = LineBreakMode.WordWrap
67+
};
68+
69+
navigatingTargetLabel = new Label
70+
{
71+
Text = "Navigating Target: Not triggered",
72+
AutomationId = "MainPageNavigatingTargetLabel",
73+
FontSize = 12,
74+
LineBreakMode = LineBreakMode.WordWrap
75+
};
76+
77+
navigatedLabel = new Label
78+
{
79+
Text = "Navigated: Not triggered",
80+
AutomationId = "MainPageNavigatedLabel",
81+
FontSize = 12,
82+
LineBreakMode = LineBreakMode.WordWrap
83+
};
84+
85+
layout.Add(button);
86+
layout.Add(navigatingCurrentLabel);
87+
layout.Add(navigatingTargetLabel);
88+
layout.Add(navigatedLabel);
89+
90+
Content = layout;
91+
Title = "MainPage";
92+
93+
if (Application.Current?.MainPage is Issue25599_1 shell)
94+
{
95+
shell.Navigating += OnNavigating;
96+
shell.Navigated += OnNavigated;
97+
}
98+
}
99+
100+
void OnNavigating(object sender, ShellNavigatingEventArgs e)
101+
{
102+
navigatingCurrentLabel.Text = $"{e.Current?.Location}";
103+
navigatingTargetLabel.Text = $"{e.Target?.Location}";
104+
}
105+
106+
void OnNavigated(object sender, ShellNavigatedEventArgs e)
107+
{
108+
navigatedLabel.Text = $"Navigated:\nPrevious: {e.Previous?.Location}\nCurrent: {e.Current?.Location}";
109+
}
110+
}
111+
112+
public class Issue25599_1DetailPage : ContentPage
113+
{
114+
public Issue25599_1DetailPage()
115+
{
116+
var layout = new StackLayout
117+
{
118+
Padding = 20,
119+
Spacing = 10,
120+
HeightRequest = 100
121+
};
122+
123+
var popButton = new Button
124+
{
125+
Text = "Pop",
126+
AutomationId = "PopButton"
127+
};
128+
129+
layout.Add(popButton);
130+
Content = layout;
131+
Title = "DetailPage";
132+
133+
}
134+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#if TEST_FAILS_ON_WINDOWS && TEST_FAILS_ON_ANDROID // Clicking an already selected tab does not trigger navigation events on Windows or Android
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
8+
public class Issue25599_1 : _IssuesUITest
9+
{
10+
public Issue25599_1(TestDevice testDevice) : base(testDevice)
11+
{
12+
}
13+
public override string Issue => "Shell Navigating event shows identical Current and Target on tab click";
14+
15+
[Test]
16+
[Category(UITestCategories.Shell)]
17+
public void VerifyNavigatingEventOnTabReselection()
18+
{
19+
App.WaitForElement("PushButton");
20+
App.Tap("PushButton");
21+
App.WaitForElement("PopButton");
22+
App.TapTab("Home");
23+
var currentLabel = App.WaitForElement("MainPageNavigatingCurrentLabel");
24+
var targetLabel = App.WaitForElement("MainPageNavigatingTargetLabel");
25+
Assert.That(currentLabel.GetText(), Is.Not.EqualTo(targetLabel.GetText()));
26+
}
27+
}
28+
#endif

0 commit comments

Comments
 (0)