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
131 lines (113 loc) · 4.72 KB
/
RoutingState.cs
File metadata and controls
131 lines (113 loc) · 4.72 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
using System;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Runtime.Serialization;
using System.Windows.Input;
using Splat;
namespace ReactiveUI
{
/// <summary>
/// RoutingState manages the ViewModel Stack and allows ViewModels to
/// navigate to other ViewModels.
/// </summary>
[DataContract]
public class RoutingState : ReactiveObject
{
[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; }
}
/// <summary>
/// Navigates back to the previous element in the stack.
/// </summary>
[IgnoreDataMember]
public ReactiveCommand<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<object> 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<object> 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()
{
NavigateBack = ReactiveCommand.Create(
NavigationStack.CountChanged.StartWith(_NavigationStack.Count).Select(x => x > 1),
_ => Observable.Return(Unit.Default));
NavigateBack.Subscribe(_ =>
NavigationStack.RemoveAt(NavigationStack.Count - 1));
Navigate = new ReactiveCommand<object>(Observable.Return(true), x => Observable.Return(x));
Navigate.Subscribe(x => {
var vm = x as IRoutableViewModel;
if (vm == null) {
throw new Exception("Navigate must be called on an IRoutableViewModel");
}
NavigationStack.Add(vm);
});
NavigateAndReset = new ReactiveCommand<object>(Observable.Return(true), x => Observable.Return(x));
NavigateAndReset.Subscribe(x => {
NavigationStack.Clear();
Navigate.ExecuteAsync(x);
});
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();
}
/// <summary>
/// Creates a ReactiveCommand which will, when invoked, navigate to the
/// type specified by the type parameter via looking it up in the
/// Dependency Resolver.
/// </summary>
public static IReactiveCommand<object> NavigateCommandFor<T>(this RoutingState This)
where T : IRoutableViewModel
{
var ret = new ReactiveCommand<object>(This.Navigate.CanExecuteObservable, x => Observable.Return(x));
ret.Select(_ => (IRoutableViewModel)Locator.Current.GetService<T>()).InvokeCommand(This.Navigate);
return ret;
}
}
}