forked from PeteGoo/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutingState.cs
More file actions
84 lines (69 loc) · 2.7 KB
/
RoutingState.cs
File metadata and controls
84 lines (69 loc) · 2.7 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
using System;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Runtime.Serialization;
using ReactiveUI.Serialization;
using ReactiveUI.Xaml;
namespace ReactiveUI.Routing
{
[DataContract]
public class RoutingState : ModelBase
{
[IgnoreDataMember] SerializedCollection<IRoutableViewModel> _NavigationStack;
[DataMember]
public SerializedCollection<IRoutableViewModel> NavigationStack {
get { return _NavigationStack; }
protected set { _NavigationStack = value; }
}
[IgnoreDataMember]
public ReactiveCommand NavigateBack { get; protected set; }
[IgnoreDataMember]
public ReactiveCommand NavigateForward { get; protected set; }
[IgnoreDataMember]
public ReactiveCommand Navigate { get; protected set; }
[IgnoreDataMember]
public IObservable<IRoutableViewModel> CurrentViewModel { get; protected set; }
[DataMember]
string _AutoSaveContract;
public RoutingState() : this(null)
{
}
public RoutingState(string autoSaveContract)
{
_NavigationStack = new SerializedCollection<IRoutableViewModel>();
_AutoSaveContract = autoSaveContract;
setupRx();
}
[OnDeserialized]
void setupRx(StreamingContext sc) { setupRx(); }
void setupRx()
{
NavigateBack = new ReactiveCommand(
NavigationStack.CollectionCountChanged.StartWith(_NavigationStack.Count).Select(x => x > 0));
NavigateBack.Subscribe(_ =>
NavigationStack.RemoveAt(NavigationStack.Count - 1));
NavigateForward = new ReactiveCommand();
NavigateForward.Subscribe(x =>
NavigationStack.Insert(NavigationStack.Count, (IRoutableViewModel)x));
Navigate = new ReactiveCommand();
Navigate.Subscribe(x => {
NavigationStack.Clear();
NavigationStack.Add((IRoutableViewModel) x);
});
if (_AutoSaveContract != null) {
Observable.Merge(Navigate, NavigateForward, NavigateBack)
.Subscribe(_ => RxStorage.Engine.CreateSyncPoint(this, _AutoSaveContract));
}
CurrentViewModel = NavigationStack.CollectionCountChanged
.Select(_ => NavigationStack.LastOrDefault())
.Multicast(new ReplaySubject<IRoutableViewModel>(1)).PermaRef();
}
public T FindViewModelInStack<T>()
where T : IRoutableViewModel
{
return NavigationStack.Reverse().OfType<T>().FirstOrDefault();
}
}
}