This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathNavigationViewModel.cs
More file actions
167 lines (142 loc) · 4.94 KB
/
NavigationViewModel.cs
File metadata and controls
167 lines (142 loc) · 4.94 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
163
164
165
166
167
using System;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reactive.Linq;
using GitHub.Extensions;
using ReactiveUI;
namespace GitHub.ViewModels.GitHubPane
{
/// <summary>
/// A view model that supports back/forward navigation of an inner content page.
/// </summary>
[Export(typeof(INavigationViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class NavigationViewModel : ViewModelBase, INavigationViewModel
{
readonly ReactiveList<IPanePageViewModel> history;
readonly ObservableAsPropertyHelper<IPanePageViewModel> content;
int index = -1;
/// <summary>
/// Initializes a new instance of the <see cref="NavigationViewModel"/> class.
/// </summary>
public NavigationViewModel()
{
history = new ReactiveList<IPanePageViewModel>();
history.BeforeItemsAdded.Subscribe(BeforeItemAdded);
history.CollectionChanged += CollectionChanged;
var pos = this.WhenAnyValue(
x => x.Index,
x => x.History.Count,
(i, c) => new { Index = i, Count = c });
content = pos
.Where(x => x.Index < x.Count)
.Select(x => x.Index != -1 ? history[x.Index] : null)
.StartWith((IPanePageViewModel)null)
.ToProperty(this, x => x.Content);
this.WhenAnyValue(x => x.Content)
.Buffer(2, 1)
.Subscribe(x => {
if (x[0] != null && history.Contains(x[0])) x[0].Deactivated();
x[1]?.Activated();
});
NavigateBack = ReactiveCommand.Create(pos.Select(x => x.Index > 0));
NavigateBack.Subscribe(_ => Back());
NavigateForward = ReactiveCommand.Create(pos.Select(x => x.Index < x.Count - 1));
NavigateForward.Subscribe(_ => Forward());
}
/// <inheritdoc/>
public IPanePageViewModel Content => content.Value;
/// <inheritdoc/>
public int Index
{
get { return index; }
set { this.RaiseAndSetIfChanged(ref index, value); }
}
/// <inheritdoc/>
public IReadOnlyReactiveList<IPanePageViewModel> History => history;
/// <inheritdoc/>
public ReactiveCommand<object> NavigateBack { get; }
/// <inheritdoc/>
public ReactiveCommand<object> NavigateForward { get; }
/// <inheritdoc/>
public void NavigateTo(IPanePageViewModel page)
{
Guard.ArgumentNotNull(page, nameof(page));
history.Insert(index + 1, page);
++Index;
if (index < history.Count - 1)
{
history.RemoveRange(index + 1, history.Count - (index + 1));
}
}
/// <inheritdoc/>
public bool Back()
{
if (index == 0)
return false;
--Index;
return true;
}
/// <inheritdoc/>
public bool Forward()
{
if (index >= history.Count - 1)
return false;
++Index;
return true;
}
/// <inheritdoc/>
public void Clear()
{
Index = -1;
history.RemoveRange(0, history.Count);
}
public int RemoveAll(IPanePageViewModel page)
{
var count = 0;
while (history.Remove(page)) ++count;
return count;
}
void BeforeItemAdded(IPanePageViewModel page)
{
if (!history.Contains(page))
{
page.CloseRequested.Subscribe(_ => RemoveAll(page));
}
}
void ItemRemoved(int removedIndex, IPanePageViewModel page)
{
if (removedIndex <= Index)
{
--Index;
}
if (!history.Contains(page))
{
if (Content == page) page.Deactivated();
page.Dispose();
}
}
void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
using (DelayChangeNotifications())
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
break;
case NotifyCollectionChangedAction.Remove:
for (var i = 0; i < e.OldItems.Count; ++i)
{
ItemRemoved(e.OldStartingIndex + i, (IPanePageViewModel)e.OldItems[i]);
}
break;
case NotifyCollectionChangedAction.Reset:
throw new NotSupportedException();
default:
throw new NotImplementedException();
}
}
}
}
}