forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutedViewHost.cs
More file actions
163 lines (138 loc) · 6.66 KB
/
RoutedViewHost.cs
File metadata and controls
163 lines (138 loc) · 6.66 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using Xamarin.Forms;
using Splat;
using ReactiveUI;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Diagnostics;
using System.Reactive;
namespace ReactiveUI.XamForms
{
public class RoutedViewHost : NavigationPage, IActivatable
{
public static readonly BindableProperty RouterProperty = BindableProperty.Create(
nameof(Router),
typeof(RoutingState),
typeof(RoutedViewHost),
default(RoutingState),
BindingMode.OneWay);
public RoutingState Router {
get { return (RoutingState)GetValue(RouterProperty); }
set { SetValue(RouterProperty, value); }
}
public RoutedViewHost()
{
this.WhenActivated(new Action<Action<IDisposable>>(d => {
bool currentlyPopping = false;
bool popToRootPending = false;
bool userInstigated = false;
d (this.WhenAnyObservable (x => x.Router.NavigationStack.Changed)
.Where(_ => Router.NavigationStack.IsEmpty)
.Select(x => {
// Xamarin Forms does not let us completely clear down the navigation stack
// instead, we have to delay this request momentarily until we receive the new root view
// then, we can insert the new root view first, and then pop to it
popToRootPending = true;
return x;
})
.Subscribe ());
var previousCount = this.WhenAnyObservable(x => x.Router.NavigationStack.CountChanged).StartWith(this.Router.NavigationStack.Count);
var currentCount = previousCount.Skip(1);
d (Observable.Zip(previousCount, currentCount, (previous, current) => new { Delta = previous - current, Current = current })
.Where(_ => !userInstigated)
.Where(x => x.Delta > 0)
.SelectMany(
async x =>
{
// XF doesn't provide a means of navigating back more than one screen at a time apart from navigating right back to the root page
// since we want as sensible an animation as possible, we pop to root if that makes sense. Otherwise, we pop each individual
// screen until the delta is made up, animating only the last one
var popToRoot = x.Current == 1;
currentlyPopping = true;
try
{
if (popToRoot)
{
await this.PopToRootAsync(true);
}
else
{
for (var i = 0; i < x.Delta; ++i)
{
await this.PopAsync(i == x.Delta - 1);
}
}
}
finally
{
currentlyPopping = false;
}
return Unit.Default;
})
.Do(_ => ((IViewFor)this.CurrentPage).ViewModel = Router.GetCurrentViewModel())
.Subscribe());
d(this.WhenAnyObservable(x => x.Router.Navigate)
.SelectMany(_ => PageForViewModel(Router.GetCurrentViewModel()))
.SelectMany(async x => {
if (popToRootPending && this.Navigation.NavigationStack.Count > 0)
{
this.Navigation.InsertPageBefore(x, this.Navigation.NavigationStack[0]);
await this.PopToRootAsync();
}
else
{
await this.PushAsync(x);
}
popToRootPending = false;
return x;
})
.Subscribe());
var poppingEvent = Observable.FromEventPattern<NavigationEventArgs>(x => this.Popped += x, x => this.Popped -= x);
// NB: Catch when the user hit back as opposed to the application
// requesting Back via NavigateBack
d(poppingEvent
.Where(_ => !currentlyPopping && Router != null)
.Subscribe(_ => {
userInstigated = true;
try {
Router.NavigationStack.RemoveAt(Router.NavigationStack.Count - 1);
} finally {
userInstigated = false;
}
((IViewFor)this.CurrentPage).ViewModel = Router.GetCurrentViewModel();
}));
}));
var screen = Locator.Current.GetService<IScreen>();
if (screen == null) throw new Exception("You *must* register an IScreen class representing your App's main Screen");
Router = screen.Router;
this.WhenAnyValue(x => x.Router)
.SelectMany(router => {
return router.NavigationStack.ToObservable()
.Select(x => (Page)ViewLocator.Current.ResolveView(x))
.SelectMany(x => this.PushAsync(x).ToObservable())
.Finally(() => {
var vm = router.GetCurrentViewModel();
if (vm == null) return;
((IViewFor)this.CurrentPage).ViewModel = vm;
this.CurrentPage.Title = vm.UrlPathSegment;
});
})
.Subscribe();
}
protected IObservable<Page> PageForViewModel(IRoutableViewModel vm)
{
if (vm == null) return Observable.Empty<Page>();
var ret = ViewLocator.Current.ResolveView(vm);
if (ret == null) {
var msg = String.Format(
"Couldn't find a View for ViewModel. You probably need to register an IViewFor<{0}>",
vm.GetType().Name);
return Observable.Throw<Page>(new Exception(msg));
}
ret.ViewModel = vm;
var pg = (Page)ret;
pg.Title = vm.UrlPathSegment;
return Observable.Return(pg);
}
}
}