-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainViewModel.cs
187 lines (171 loc) · 7.84 KB
/
MainViewModel.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
#region #usings
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Xpf.Scheduling;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
#endregion #usings
namespace SchedulerDragDropExample
{
[POCOViewModel]
public class MainViewModel
{
const int DayViewVisibleDaysCount = 7;
public virtual ObservableCollection<MedicalAppointment> Appointments { get; set; }
public virtual ObservableCollection<Doctor> Doctors { get; set; }
public virtual ObservableCollection<PaymentState> PaymentStates { get; set; }
public virtual DateTime SchedulerStart { get; set; }
protected MainViewModel()
{
Doctors = new ObservableCollection<Doctor>();
Appointments = new ObservableCollection<MedicalAppointment>();
PaymentStates = new ObservableCollection<PaymentState>();
SchedulerStart = DateTime.Today.Date;
CreateDoctors();
CreateAppointments();
CreatePaymentStates();
}
private void CreateAppointments()
{
Random rand = new Random(DateTime.Now.Millisecond);
Appointments.Add(MedicalAppointment.Create(startTime: DateTime.Now.Date.AddHours(10), endTime: DateTime.Now.Date.AddHours(11.5), doctorId: 1, paymentStateId: 1, location: "101", patientName: "Dave Murrel", note: "Take care", firstVisit: true));
Appointments.Add(MedicalAppointment.Create(startTime: DateTime.Now.Date.AddDays(2).AddHours(15), endTime: DateTime.Now.Date.AddDays(2).AddHours(16.5), doctorId: 1, paymentStateId: 1, location: "101", patientName: "Mike Roller", note: "Schedule next visit soon", firstVisit: true));
Appointments.Add(MedicalAppointment.Create(startTime: DateTime.Now.Date.AddDays(1).AddHours(11), endTime: DateTime.Now.Date.AddDays(1).AddHours(12), doctorId: 2, paymentStateId: 1, location: "103", patientName: "Bert Parkins", note: string.Empty, firstVisit: true));
Appointments.Add(MedicalAppointment.Create(startTime: DateTime.Now.Date.AddDays(2).AddHours(10), endTime: DateTime.Now.Date.AddDays(2).AddHours(12), doctorId: 2, paymentStateId: 0, location: "103", patientName: "Carl Lucas", note: string.Empty, firstVisit: false));
Appointments.Add(MedicalAppointment.Create(startTime: DateTime.Now.Date.AddHours(12), endTime: DateTime.Now.Date.AddHours(13.5), doctorId: 3, paymentStateId: 1, location: "104", patientName: "Brad Barnes", note: "Tests are necessary", firstVisit: false));
Appointments.Add(MedicalAppointment.Create(startTime: DateTime.Now.Date.AddDays(1).AddHours(14), endTime: DateTime.Now.Date.AddDays(1).AddHours(15), doctorId: 3, paymentStateId: 1, location: "104", patientName: "Richard Fisher", note: string.Empty, firstVisit: true));
}
private void CreateDoctors()
{
Doctors.Add(Doctor.Create(id: 1, name: "Stomatologist"));
Doctors.Add(Doctor.Create(id: 2, name: "Ophthalmologist"));
Doctors.Add(Doctor.Create(id: 3, name: "Surgeon"));
}
private void CreatePaymentStates()
{
PaymentStates.Add(PaymentState.Create(id: 0, caption: "Unpaid", color: "Tomato"));
PaymentStates.Add(PaymentState.Create(id: 1, caption: "Paid", color: "LightGreen"));
}
#region #MyMove
public void MyMoveLeft(SchedulerControl scheduler)
{
MyMove(false, scheduler);
}
public void MyMoveRight(SchedulerControl scheduler)
{
MyMove(true, scheduler);
}
public void MyMove(bool forward, SchedulerControl scheduler)
{
if (SchedulerStart.Date == scheduler.Start.Date) return;
FixVisibleDays(forward, scheduler);
SchedulerStart = scheduler.Start;
}
private void FixVisibleDays(bool forward, SchedulerControl scheduler)
{
DayView myView = scheduler.ActiveView as DayView;
DateTime startDate = scheduler.Start;
if (myView != null)
{
DateTimeRange selection = scheduler.SelectedInterval;
startDate = CorrectDate(startDate, forward);
List<DateTime> visibleDays = GetDays(startDate);
// Specify DayView's visible days
myView.Days = visibleDays;
scheduler.SelectedInterval = AdjustSelection(forward, selection);
}
}
#endregion #MyMove
public void MyGoToDate(SchedulerControl scheduler)
{
scheduler.ShowGotoDateWindow(DateTime.Today);
FixVisibleDays(true, scheduler);
SchedulerStart = scheduler.Start;
}
public void MyGoToToday(SchedulerControl scheduler)
{
DateTime startDate = DateTime.Today;
scheduler.Start = startDate;
FixVisibleDays(true, scheduler);
SchedulerStart = scheduler.Start;
}
#region #OnVisibleIntervalsChanged
public void OnVisibleIntervalsChanged(SchedulerControl scheduler, VisibleIntervalsChangedEventArgs args)
{
if (!NeedCorrection(args.VisibleDates.ToList())) return;
FixVisibleDays(true, scheduler);
}
#endregion #OnVisibleIntervalsChanged
private DateTimeRange AdjustSelection(bool forward, DateTimeRange selection)
{
DateTime dtStart;
DateTime dtEnd;
TimeSpan diff = selection.End - selection.Start;
dtStart = CorrectDate(selection.Start, forward);
dtEnd = dtStart + diff;
return new DateTimeRange(dtStart, dtEnd);
}
public void MyNavigateBackward(SchedulerControl scheduler)
{
DayView myView = scheduler.ActiveView as DayView;
if (myView != null)
{
scheduler.Start = scheduler.Start.AddDays(-DayViewVisibleDaysCount);
myView.Days = GetDays(scheduler.Start);
}
else
scheduler.ActiveView.NavigateBackward();
}
#region #MyNavigateForward
public void MyNavigateForward(SchedulerControl scheduler)
{
DayView myView = scheduler.ActiveView as DayView;
if (myView != null)
{
scheduler.Start = scheduler.Start.AddDays(DayViewVisibleDaysCount);
// Calculate a list of days to display, excluding non-working days.
myView.Days = GetDays(scheduler.Start);
}
else
scheduler.ActiveView.NavigateForward();
}
#endregion #MyNavigateForward
private DateTime CorrectDate(DateTime day, bool forward)
{
int direction;
switch (day.DayOfWeek)
{
case DayOfWeek.Saturday:
direction = forward ? 2 : -1;
day = day.AddDays((direction));
break;
case DayOfWeek.Sunday:
direction = forward ? 1 : -2;
day = day.AddDays(direction);
break;
}
return day;
}
private bool NeedCorrection(List<DateTime> days)
{
if (days.Any(day => (day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday)))
return true;
return false;
}
public List<DateTime> GetDays(DateTime startDate)
{
var list = new List<DateTime>();
int i = 0;
while (list.Count < DayViewVisibleDaysCount)
{
var day = startDate.AddDays(i);
if (day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday)
list.Add(day);
i++;
}
return list;
}
}
}