/
ResourceMgtViewModel.cs
316 lines (263 loc) · 11.2 KB
/
ResourceMgtViewModel.cs
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// ====================================================================================================================
// Copyright (c) 2012 IdeaBlade
// ====================================================================================================================
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ====================================================================================================================
// USE OF THIS SOFTWARE IS GOVERENED BY THE LICENSING TERMS WHICH CAN BE FOUND AT
// http://cocktail.ideablade.com/licensing
// ====================================================================================================================
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using System.Windows.Threading;
using Caliburn.Micro;
using Cocktail;
using Common;
using Common.Messages;
using Common.Toolbar;
using DomainServices;
using IdeaBlade.EntityModel;
namespace TempHire.ViewModels.StaffingResource
{
[Export]
public class ResourceMgtViewModel : Conductor<IScreen>, IDiscoverableViewModel,
IHandle<EntityChangedMessage>, INavigationTarget
{
private readonly IDialogManager _dialogManager;
private readonly ExportFactory<StaffingResourceNameEditorViewModel> _nameEditorFactory;
private readonly INavigator _navigator;
private readonly DispatcherTimer _selectionChangeTimer;
private readonly IToolbarManager _toolbar;
private IScreen _retainedActiveItem;
private ToolbarGroup _toolbarGroup;
[ImportingConstructor]
public ResourceMgtViewModel(StaffingResourceSearchViewModel searchPane,
ExportFactory<StaffingResourceNameEditorViewModel> nameEditorFactory,
IDialogManager dialogManager, IToolbarManager toolbar)
{
EventFns.Subscribe(this);
SearchPane = searchPane;
_nameEditorFactory = nameEditorFactory;
_dialogManager = dialogManager;
_toolbar = toolbar;
_navigator = new Navigator(this);
PropertyChanged += OnPropertyChanged;
_selectionChangeTimer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 0, 0, 200)};
_selectionChangeTimer.Tick += OnSelectionChangeElapsed;
}
public StaffingResourceSearchViewModel SearchPane { get; private set; }
public bool CanAdd
{
get { return ActiveDetail == null || ActiveDetail.IsReadOnly; }
}
public bool CanDelete
{
get { return ActiveStaffingResource != null && ActiveDetail.IsReadOnly; }
}
public bool CanSave
{
get
{
return ActiveStaffingResource != null && ActiveUnitOfWork.HasChanges() &&
!ActiveStaffingResource.EntityFacts.EntityState.IsDeleted();
}
}
public bool CanCancel
{
get { return ActiveDetail != null && !ActiveDetail.IsReadOnly; }
}
public bool CanRefreshData
{
get { return ActiveStaffingResource != null && !ActiveStaffingResource.EntityFacts.EntityState.IsAdded(); }
}
public bool CanEdit
{
get { return ActiveStaffingResource != null && ActiveDetail.IsReadOnly; }
}
private IResourceMgtUnitOfWork ActiveUnitOfWork
{
get { return ActiveDetail != null ? ActiveDetail.UnitOfWork : null; }
}
private StaffingResourceDetailViewModel ActiveDetail
{
get { return ActiveItem as StaffingResourceDetailViewModel; }
}
private DomainModel.StaffingResource ActiveStaffingResource
{
get { return ActiveDetail != null ? ActiveDetail.StaffingResource : null; }
}
#region IHandle<EntityChangedMessage> Members
public void Handle(EntityChangedMessage message)
{
if (ActiveStaffingResource == null || !ActiveUnitOfWork.HasEntity(message.Entity))
return;
UpdateCommands();
}
#endregion
public ResourceMgtViewModel Start()
{
SearchPane.Start();
return this;
}
public async void Add()
{
try
{
var nameEditor = _nameEditorFactory.CreateExport().Value;
await nameEditor.ShowDialogAsync();
SearchPane.CurrentStaffingResource = null;
await _navigator.NavigateToAsync<StaffingResourceDetailViewModel>(
target => target.Start(nameEditor.FirstName, nameEditor.MiddleName, nameEditor.LastName));
}
catch (TaskCanceledException)
{
UpdateCommands();
}
}
public async void Delete()
{
await _dialogManager.ShowMessageAsync(
string.Format("Are you sure you want to delete {0}?", ActiveStaffingResource.FullName),
DialogResult.Yes, DialogResult.No, DialogButtons.YesNo);
try
{
using (ActiveDetail.Busy.GetTicket())
{
ActiveUnitOfWork.StaffingResources.Delete(ActiveStaffingResource);
await ActiveUnitOfWork.CommitAsync();
}
ActiveItem.TryClose();
}
catch (TaskCanceledException)
{
ActiveUnitOfWork.Rollback();
}
catch (Exception)
{
ActiveUnitOfWork.Rollback();
throw;
}
}
public void Edit()
{
ActiveDetail.Start(ActiveStaffingResource.Id, EditMode.Edit);
}
public async void Save()
{
using (ActiveDetail.Busy.GetTicket())
await ActiveUnitOfWork.CommitAsync();
ActiveDetail.Start(ActiveStaffingResource.Id, EditMode.View);
}
public void Cancel()
{
var shouldClose = ActiveStaffingResource.EntityFacts.EntityState.IsAdded();
ActiveUnitOfWork.Rollback();
if (shouldClose)
ActiveDetail.TryClose();
else
ActiveDetail.Start(ActiveStaffingResource.Id, EditMode.View);
}
public void RefreshData()
{
ActiveDetail.RefreshData();
}
protected override void OnActivate()
{
base.OnActivate();
Start();
SearchPane.PropertyChanged += OnSearchPanePropertyChanged;
((IActivate) SearchPane).Activate();
if (_toolbarGroup == null)
{
_toolbarGroup = new ToolbarGroup(10)
{
new ToolbarAction(this, "Add", Add),
new ToolbarAction(this, "Delete", Delete),
new ToolbarAction(this, "Edit", Edit),
new ToolbarAction(this, "Save", Save),
new ToolbarAction(this, "Cancel", Cancel),
new ToolbarAction(this, "Refresh", RefreshData)
};
}
_toolbar.AddGroup(_toolbarGroup);
}
protected override void OnDeactivate(bool close)
{
base.OnDeactivate(close);
SearchPane.PropertyChanged -= OnSearchPanePropertyChanged;
((IDeactivate) SearchPane).Deactivate(close);
if (_retainedActiveItem != null)
{
_retainedActiveItem.PropertyChanged -= OnActiveDetailPropertyChanged;
_retainedActiveItem = null;
}
_toolbar.RemoveGroup(_toolbarGroup);
}
private async void OnSelectionChangeElapsed(object sender, EventArgs e)
{
_selectionChangeTimer.Stop();
if (SearchPane.CurrentStaffingResource == null) return;
try
{
if (ActiveStaffingResource != null && ActiveStaffingResource.Id == SearchPane.CurrentStaffingResource.Id)
return;
await _navigator.NavigateToAsync<StaffingResourceDetailViewModel>(
target => target.Start(SearchPane.CurrentStaffingResource.Id, EditMode.View));
}
catch (TaskCanceledException)
{
SearchPane.CurrentStaffingResource = null;
UpdateCommands();
}
}
private void OnSearchPanePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != "CurrentStaffingResource") return;
if (_selectionChangeTimer.IsEnabled) _selectionChangeTimer.Stop();
_selectionChangeTimer.Start();
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != "ActiveItem") return;
if (_retainedActiveItem != null)
_retainedActiveItem.PropertyChanged -= OnActiveDetailPropertyChanged;
_retainedActiveItem = ActiveItem;
if (ActiveItem != null)
ActiveItem.PropertyChanged += OnActiveDetailPropertyChanged;
UpdateCommands();
}
private void OnActiveDetailPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "StaffingResource")
UpdateCommands();
}
private void UpdateCommands()
{
NotifyOfPropertyChange(() => CanSave);
NotifyOfPropertyChange(() => CanCancel);
NotifyOfPropertyChange(() => CanDelete);
NotifyOfPropertyChange(() => CanRefreshData);
NotifyOfPropertyChange(() => CanEdit);
NotifyOfPropertyChange(() => CanAdd);
}
public void OnNavigatedFrom(NavigationArgs args)
{
}
public void OnNavigatedTo(NavigationArgs args)
{
}
public void OnNavigatingFrom(NavigationCancelArgs args)
{
// We need to explicity close the active item when navigating away.
// This is due to a potential bug in Caliburn.Micro. While CM properly deactivates the active item,
// it doesn't set ActiveItem to null. Since ResourceMgtViewModel is a singleton, this will
// cause the last active item and all of its dependencies to stay in memory, even though
// the user effectively closed the resource management module.
if (ActiveItem != null) ActiveItem.TryClose();
}
}
}