-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppShell.xaml.cs
More file actions
146 lines (130 loc) · 5.14 KB
/
Copy pathAppShell.xaml.cs
File metadata and controls
146 lines (130 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Linq;
namespace NuLigaViewer
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute("settings", typeof(Pages.SettingsPage));
Routing.RegisterRoute("teampairing", typeof(Pages.TeamPairingPage));
Routing.RegisterRoute("playerdetails", typeof(Pages.PlayerPage));
}
private const string _lastGameDayRoute = "//league/lastgameday";
private const string _toptenRoute = "//league/topten";
private const string _gamedaysRoute = "//team/gamedays";
private const string _playersRoute = "//team/players";
private const string _clubplayersRoute = "//team/clubplayers";
protected async override void OnNavigating(ShellNavigatingEventArgs e)
{
base.OnNavigating(e);
if (e.Source != ShellNavigationSource.ShellSectionChanged || e.Current is null || e.Target is null || e.Target.Location == _pageBeingPopped?.Location)
{
return;
}
var targetPath = e.Target.Location?.ToString() ?? string.Empty;
var levels = targetPath.Trim('/').Count(c => c == '/') - 1;
if (levels < 1)
{
return;
}
if (!(targetPath.StartsWith(_lastGameDayRoute)
|| targetPath.StartsWith(_toptenRoute)
|| targetPath.StartsWith(_gamedaysRoute)
|| targetPath.StartsWith(_playersRoute)
|| targetPath.StartsWith(_clubplayersRoute)))
{
return;
}
if (e.Cancel())
{
try
{
if (targetPath.StartsWith(_lastGameDayRoute))
{
ClearStackFromTabSubpages(_lastGameDayRoute);
await Shell.Current.GoToAsync(_lastGameDayRoute);
}
else if (targetPath.StartsWith(_toptenRoute))
{
ClearStackFromTabSubpages(_toptenRoute);
await Shell.Current.GoToAsync(_toptenRoute);
}
else if (targetPath.StartsWith(_gamedaysRoute))
{
ClearStackFromTabSubpages(_gamedaysRoute);
await Shell.Current.GoToAsync(_gamedaysRoute);
}
else if (targetPath.StartsWith(_playersRoute))
{
ClearStackFromTabSubpages(_playersRoute);
await Shell.Current.GoToAsync(_playersRoute);
}
else if (targetPath.StartsWith(_clubplayersRoute))
{
ClearStackFromTabSubpages(_clubplayersRoute);
await Shell.Current.GoToAsync(_clubplayersRoute);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
private static void ClearStackFromTabSubpages(string baseRoute)
{
var reversedUris = Uri.ToArray();
Uri.Clear();
for (var i = reversedUris.Length - 1; i >= 0; i--)
{
var uriString = reversedUris[i].Location.ToString();
if (!uriString.StartsWith(baseRoute) || uriString.Equals(baseRoute))
{
Uri.Push(reversedUris[i]);
}
}
}
protected override void OnNavigated(ShellNavigatedEventArgs args)
{
base.OnNavigated(args);
if (Uri == null || args.Previous == null)
{
return;
}
if (args.Current != null && args.Current.Location.ToString() == "//home")
{
Uri.Clear();
return;
}
if (_pageBeingPopped == null || _pageBeingPopped.Location != args.Current?.Location)
{
Uri.Push(args.Previous);
_pageBeingPopped = null;
}
}
protected override bool OnBackButtonPressed()
{
if (Uri != null && Uri.Count > 0)
{
Dispatcher.Dispatch(async () =>
{
await GoBackInStack();
});
return true;
}
return base.OnBackButtonPressed();
}
public async static Task GoBackInStack()
{
if (Uri != null && Uri.Count > 0)
{
var previousPage = Uri.Pop();
_pageBeingPopped = previousPage;
await Shell.Current.GoToAsync(previousPage);
}
}
private static Stack<ShellNavigationState> Uri { get; } = new Stack<ShellNavigationState>();
private static ShellNavigationState? _pageBeingPopped { get; set; }
}
}