This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchedulerValidationService.cs
64 lines (55 loc) · 3.04 KB
/
SchedulerValidationService.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
using DevExpress.Mvvm;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Scheduling;
using System.Collections.Generic;
using System.Linq;
namespace DXSample {
public class SchedulerValidationService : DialogService {
bool ProcessAppointments(IReadOnlyList<AppointmentItem> appts, IList<AppointmentItem> itemsToCancel) {
var toCancel = new List<AppointmentItem>();
foreach(var item in appts) {
var range = new DateTimeRange(item.Start, item.End);
var startLunchRange = new DateTimeRange(item.Start.Date.AddHours(13), item.Start.Date.AddHours(14));
var endLunchRange = new DateTimeRange(item.End.Date.AddHours(13), item.End.Date.AddHours(14));
if(range.Intersect(startLunchRange).Duration.Ticks != 0 ||
range.Intersect(endLunchRange).Duration.Ticks != 0 ||
range.Duration.Hours > 23)
toCancel.Add(item);
}
if(toCancel.Count > 0) {
var Cancel = new UICommand() { Caption = "Cancel", IsDefault = true };
var CancelConflicts = new UICommand() { Caption = "Cancel Conflicts" };
var Ignore = new UICommand() { Caption = "Ignore", IsCancel = true };
var result = this.ShowDialog(new List<UICommand>()
{ Cancel, CancelConflicts, Ignore },
"Warning",
"WarningUserControl",
"The following appointment(-s) intersects the lunch time:\n\n" +
string.Join("\n", toCancel.Select(c => c.Subject)) +
"\n\nClick 'Cancel' to discard all changes.\nClick 'Cancel Conflicts' to cancel changes only in these appointment(-s).");
if(result == Cancel)
return false;
if(result == CancelConflicts)
foreach(var item in toCancel)
itemsToCancel.Add(item);
}
return true;
}
private void Scheduler_AppointmentAdding(object sender, AppointmentAddingEventArgs e)
{ e.Cancel = !ProcessAppointments(e.Appointments, e.CanceledAppointments); }
private void Scheduler_AppointmentEditing(object sender, AppointmentEditingEventArgs e)
{ e.Cancel = !ProcessAppointments(e.EditAppointments, e.CanceledEditAppointments); }
protected override void OnAttached() {
base.OnAttached();
var scheduler = this.AssociatedObject as SchedulerControl;
scheduler.AppointmentEditing += Scheduler_AppointmentEditing;
scheduler.AppointmentAdding += Scheduler_AppointmentAdding;
}
protected override void OnDetaching() {
var scheduler = this.AssociatedObject as SchedulerControl;
scheduler.AppointmentEditing -= Scheduler_AppointmentEditing;
scheduler.AppointmentAdding -= Scheduler_AppointmentAdding;
base.OnDetaching();
}
}
}