-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathNavigationView.cs
218 lines (189 loc) · 8.01 KB
/
NavigationView.cs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2021 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Threading.Tasks;
namespace Sextant.Maui;
/// <summary>
/// The main navigation view.
/// </summary>
public class NavigationView : NavigationPage, IView, IEnableLogger
{
private readonly ISubject<NavigationSource> _navigationSource =
new BehaviorSubject<NavigationSource>(NavigationSource.Device);
private readonly IScheduler _backgroundScheduler;
private readonly IViewLocator _viewLocator;
private readonly IFullLogger _logger;
/// <summary>
/// Initializes a new instance of the <see cref="NavigationView"/> class.
/// </summary>
public NavigationView()
: this(RxApp.MainThreadScheduler, RxApp.TaskpoolScheduler, ViewLocator.Current)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="NavigationView"/> class.
/// </summary>
/// <param name="mainScheduler">The main scheduler to scheduler UI tasks on.</param>
/// <param name="backgroundScheduler">The background scheduler.</param>
/// <param name="viewLocator">The view locator which will find views associated with view models.</param>
/// <param name="rootPage">The starting root page.</param>
public NavigationView(IScheduler mainScheduler, IScheduler backgroundScheduler, IViewLocator viewLocator, Page rootPage)
: base(rootPage)
{
_backgroundScheduler = backgroundScheduler;
MainThreadScheduler = mainScheduler;
_viewLocator = viewLocator;
_logger = this.Log();
PagePopped =
Observable
.FromEvent<EventHandler<NavigationEventArgs>, IViewModel>(
handler =>
{
void Handler(object? sender, NavigationEventArgs args)
{
if (args.Page.BindingContext is IViewModel viewModel)
{
handler(viewModel);
}
}
return Handler;
},
x => Popped += x,
x => Popped -= x);
Behaviors.Add(new NavigationPageSystemPopBehavior(_navigationSource.AsObservable()));
}
/// <summary>
/// Initializes a new instance of the <see cref="NavigationView"/> class.
/// </summary>
/// <param name="mainScheduler">The main scheduler to scheduler UI tasks on.</param>
/// <param name="backgroundScheduler">The background scheduler.</param>
/// <param name="viewLocator">The view locator which will find views associated with view models.</param>
public NavigationView(IScheduler mainScheduler, IScheduler backgroundScheduler, IViewLocator viewLocator)
{
MainThreadScheduler = mainScheduler;
_backgroundScheduler = backgroundScheduler;
_viewLocator = viewLocator;
_logger = this.Log();
PagePopped =
Observable
.FromEvent<EventHandler<NavigationEventArgs>, IViewModel>(
handler =>
{
void Handler(object? sender, NavigationEventArgs args)
{
if (args.Page.BindingContext is IViewModel viewModel)
{
handler(viewModel);
}
}
return Handler;
},
x => Popped += x,
x => Popped -= x);
Behaviors.Add(new NavigationPageSystemPopBehavior(_navigationSource.AsObservable()));
}
/// <inheritdoc />
public IScheduler MainThreadScheduler { get; }
/// <inheritdoc />
public IObservable<IViewModel> PagePopped { get; }
/// <inheritdoc />
public IObservable<Unit> PopModal() =>
Navigation
.PopModalAsync()
.ToObservable()
.Select(_ => Unit.Default)
.ObserveOn(MainThreadScheduler); // XF completes the pop operation on a background thread :/
/// <inheritdoc />
public IObservable<Unit> PopPage(bool animate)
{
_navigationSource.OnNext(NavigationSource.NavigationService);
return Navigation
.PopAsync(animate)
.ToObservable()
.Select(_ => Unit.Default)
.ObserveOn(MainThreadScheduler)
.Finally(() => _navigationSource.OnNext(NavigationSource.Device));
}
/// <inheritdoc />
public IObservable<Unit> PopToRootPage(bool animate) =>
Navigation
.PopToRootAsync(animate)
.ToObservable()
.Select(_ => Unit.Default)
.ObserveOn(MainThreadScheduler);
/// <inheritdoc />
public IObservable<Unit> PushModal(IViewModel modalViewModel, string? contract, bool withNavigationPage = true) =>
Observable
.Start(
() =>
{
var page = LocatePageFor(modalViewModel, contract);
SetPageTitle(page, modalViewModel.Id);
return withNavigationPage ? new NavigationPage(page) : page;
},
CurrentThreadScheduler.Instance)
.ObserveOn(CurrentThreadScheduler.Instance)
.SelectMany(
page =>
Navigation
.PushModalAsync(page)
.ToObservable());
/// <inheritdoc />
public IObservable<Unit> PushPage(
IViewModel viewModel,
string? contract,
bool resetStack,
bool animate) =>
Observable
.Start(
() =>
{
var page = LocatePageFor(viewModel, contract);
SetPageTitle(page, viewModel.Id);
return page;
},
CurrentThreadScheduler.Instance)
.ObserveOn(CurrentThreadScheduler.Instance)
.SelectMany(
page =>
{
if (resetStack)
{
if (Navigation.NavigationStack.Count == 0)
{
return Navigation.PushAsync(page, false).ToObservable();
}
// XF does not allow us to pop to a new root page. Instead, we need to inject the new root page and then pop to it.
Navigation
.InsertPageBefore(page, Navigation.NavigationStack[0]);
return Navigation
.PopToRootAsync(false)
.ToObservable();
}
return Navigation
.PushAsync(page, animate)
.ToObservable();
});
private static void SetPageTitle(Page page, string? resourceKey) =>
// var title = Localize.GetString(resourceKey);
// TODO: ensure resourceKey isn't null and is localized.
page.Title = resourceKey;
private Page LocatePageFor(object viewModel, string? contract)
{
var view = _viewLocator.ResolveView(viewModel, contract) ?? throw new InvalidOperationException(
$"No view could be located for type '{viewModel.GetType().FullName}', contract '{contract}'. Be sure Splat has an appropriate registration.");
if (view is not Page page)
{
throw new InvalidOperationException(
$"Resolved view '{view.GetType().FullName}' for type '{viewModel.GetType().FullName}', contract '{contract}' is not a Page.");
}
view.ViewModel = viewModel;
return page;
}
}