-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathConductorWithCollectionAllActive.cs
More file actions
206 lines (182 loc) · 8.94 KB
/
ConductorWithCollectionAllActive.cs
File metadata and controls
206 lines (182 loc) · 8.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace Caliburn.Micro
{
public partial class Conductor<T>
{
/// <summary>
/// An implementation of <see cref="IConductor"/> that holds on many items.
/// </summary>
public partial class Collection
{
/// <summary>
/// An implementation of <see cref="IConductor"/> that holds on to many items which are all activated.
/// </summary>
public class AllActive : ConductorBase<T>
{
private readonly BindableCollection<T> _items = new BindableCollection<T>();
private readonly bool _openPublicItems;
/// <summary>
/// Initializes a new instance of the <see cref="Conductor<T>.Collection.AllActive"/> class.
/// </summary>
/// <param name="openPublicItems">if set to <c>true</c> opens public items that are properties of this class.</param>
public AllActive(bool openPublicItems)
: this()
{
_openPublicItems = openPublicItems;
}
/// <summary>
/// Initializes a new instance of the <see cref="Conductor<T>.Collection.AllActive"/> class.
/// </summary>
public AllActive()
{
_items.CollectionCleared += (s, e) =>
{
e.ClearedItems.OfType<IChild>().Apply(x => x.Parent = null);
};
_items.CollectionChanged += (s, e) =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
e.NewItems.OfType<IChild>().Apply(x => x.Parent = this);
break;
case NotifyCollectionChangedAction.Remove:
e.OldItems.OfType<IChild>().Apply(x => x.Parent = null);
break;
case NotifyCollectionChangedAction.Replace:
e.NewItems.OfType<IChild>().Apply(x => x.Parent = this);
e.OldItems.OfType<IChild>().Apply(x => x.Parent = null);
break;
case NotifyCollectionChangedAction.Reset:
_items.OfType<IChild>().Apply(x => x.Parent = this);
break;
}
};
}
/// <summary>
/// Gets the items that are currently being conducted.
/// </summary>
public IObservableCollection<T> Items => _items;
/// <summary>
/// Called when activating.
/// </summary>
protected override Task OnActivateAsync(CancellationToken cancellationToken)
{
return Task.WhenAll(_items.OfType<IActivate>().Select(x => x.ActivateAsync(cancellationToken)));
}
/// <summary>
/// Called when deactivating.
/// </summary>
/// <param name="close">Indicates whether this instance will be closed.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
protected override async Task OnDeactivateAsync(bool close, CancellationToken cancellationToken)
{
foreach (var deactivate in _items.OfType<IDeactivate>())
{
await deactivate.DeactivateAsync(close, cancellationToken);
}
if (close)
_items.Clear();
}
/// <summary>
/// Called to check whether or not this instance can close.
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task<bool> CanCloseAsync(CancellationToken cancellationToken)
{
var closeResult = await CloseStrategy.ExecuteAsync(_items.ToList(), cancellationToken);
if (!closeResult.CloseCanOccur && closeResult.Children.Any())
{
foreach (var deactivate in closeResult.Children.OfType<IDeactivate>())
{
await deactivate.DeactivateAsync(true, cancellationToken);
}
_items.RemoveRange(closeResult.Children);
}
return closeResult.CloseCanOccur;
}
/// <summary>
/// Called when initializing.
/// </summary>
protected override async Task OnInitializeAsync(CancellationToken cancellationToken)
{
if (_openPublicItems)
await Task.WhenAll(GetType().GetRuntimeProperties()
.Where(x => x.Name != "Parent" && typeof(T).GetTypeInfo().IsAssignableFrom(x.PropertyType.GetTypeInfo()))
.Select(x => x.GetValue(this, null))
.Cast<T>()
.Select(i => ActivateItemAsync(i, cancellationToken)));
}
/// <summary>
/// Activates the specified item.
/// </summary>
/// <param name="item">The item to activate.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken)
{
if (item == null)
return;
item = EnsureItem(item);
if (IsActive)
await ScreenExtensions.TryActivateAsync(item, cancellationToken);
OnActivationProcessed(item, true);
}
/// <summary>
/// Deactivates the specified item.
/// </summary>
/// <param name="item">The item to close.</param>
/// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken)
{
if (item == null)
return;
if (close)
{
var closeResult = await CloseStrategy.ExecuteAsync(new[] { item }, CancellationToken.None);
if (closeResult.CloseCanOccur)
await CloseItemCoreAsync(item, cancellationToken);
}
else
await ScreenExtensions.TryDeactivateAsync(item, false, cancellationToken);
}
/// <summary>
/// Gets the children.
/// </summary>
/// <returns>The collection of children.</returns>
public override IEnumerable<T> GetChildren()
{
return _items;
}
private async Task CloseItemCoreAsync(T item, CancellationToken cancellationToken)
{
await ScreenExtensions.TryDeactivateAsync(item, true, cancellationToken);
_items.Remove(item);
}
/// <summary>
/// Ensures that an item is ready to be activated.
/// </summary>
/// <param name="newItem">The item that is about to be activated.</param>
/// <returns>The item to be activated.</returns>
protected override T EnsureItem(T newItem)
{
var index = _items.IndexOf(newItem);
if (index == -1)
_items.Add(newItem);
else
newItem = _items[index];
return base.EnsureItem(newItem);
}
}
}
}
}