forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutingState.cs
More file actions
executable file
·148 lines (127 loc) · 5.14 KB
/
RoutingState.cs
File metadata and controls
executable file
·148 lines (127 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
147
148
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MS-PL license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Runtime.Serialization;
namespace ReactiveUI
{
/// <summary>
/// RoutingState manages the ViewModel Stack and allows ViewModels to
/// navigate to other ViewModels.
/// </summary>
[DataContract]
public class RoutingState : ReactiveObject
{
static RoutingState()
{
RxApp.EnsureInitialized();
}
[DataMember] ReactiveList<IRoutableViewModel> _NavigationStack;
/// <summary>
/// Represents the current navigation stack, the last element in the
/// collection being the currently visible ViewModel.
/// </summary>
[IgnoreDataMember]
public ReactiveList<IRoutableViewModel> NavigationStack {
get { return _NavigationStack; }
protected set { _NavigationStack = value; }
}
[IgnoreDataMember]
IScheduler scheduler;
/// <summary>
/// The scheduler used for commands. Defaults to <c>RxApp.MainThreadScheduler</c>.
/// </summary>
[IgnoreDataMember]
public IScheduler Scheduler {
get { return this.scheduler; }
set {
if (this.scheduler != value) {
this.scheduler = value;
this.setupRx();
}
}
}
/// <summary>
/// Navigates back to the previous element in the stack.
/// </summary>
[IgnoreDataMember]
public ReactiveCommand<Unit, Unit> NavigateBack { get; protected set; }
/// <summary>
/// Navigates to the a new element in the stack - the Execute parameter
/// must be a ViewModel that implements IRoutableViewModel.
/// </summary>
[IgnoreDataMember]
public ReactiveCommand<IRoutableViewModel, IRoutableViewModel> Navigate { get; protected set; }
/// <summary>
/// Navigates to a new element and resets the navigation stack (i.e. the
/// new ViewModel will now be the only element in the stack) - the
/// Execute parameter must be a ViewModel that implements
/// IRoutableViewModel.
/// </summary>
[IgnoreDataMember]
public ReactiveCommand<IRoutableViewModel, IRoutableViewModel> NavigateAndReset { get; protected set; }
[IgnoreDataMember]
public IObservable<IRoutableViewModel> CurrentViewModel { get; protected set; }
public RoutingState()
{
_NavigationStack = new ReactiveList<IRoutableViewModel>();
setupRx();
}
[OnDeserialized]
void setupRx(StreamingContext sc) { setupRx(); }
void setupRx()
{
var scheduler = this.scheduler ?? RxApp.MainThreadScheduler;
var countAsBehavior = Observable.Concat(
Observable.Defer(() => Observable.Return(_NavigationStack.Count)),
NavigationStack.CountChanged);
NavigateBack =
ReactiveCommand.CreateFromObservable(() => {
NavigationStack.RemoveAt(NavigationStack.Count - 1);
return Observables.Unit;
},
countAsBehavior.Select(x => x > 1),
scheduler);
Navigate = ReactiveCommand.CreateFromObservable<IRoutableViewModel, IRoutableViewModel>(x => {
var vm = x as IRoutableViewModel;
if (vm == null) {
throw new Exception("Navigate must be called on an IRoutableViewModel");
}
NavigationStack.Add(vm);
return Observable.Return(x);
},
outputScheduler: scheduler);
NavigateAndReset = ReactiveCommand.CreateFromObservable<IRoutableViewModel, IRoutableViewModel>(x => {
NavigationStack.Clear();
return Navigate.Execute(x);
},
outputScheduler: scheduler);
CurrentViewModel = Observable.Concat(
Observable.Defer(() => Observable.Return(NavigationStack.LastOrDefault())),
NavigationStack.Changed.Select(_ => NavigationStack.LastOrDefault()));
}
}
public static class RoutingStateMixins
{
/// <summary>
/// Locate the first ViewModel in the stack that matches a certain Type.
/// </summary>
/// <returns>The matching ViewModel or null if none exists.</returns>
public static T FindViewModelInStack<T>(this RoutingState This)
where T : IRoutableViewModel
{
return This.NavigationStack.Reverse().OfType<T>().FirstOrDefault();
}
/// <summary>
/// Returns the currently visible ViewModel
/// </summary>
public static IRoutableViewModel GetCurrentViewModel(this RoutingState This)
{
return This.NavigationStack.LastOrDefault();
}
}
}