forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragDrop.cs
295 lines (264 loc) · 11.4 KB
/
DragDrop.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
#region Copyright Syncfusion Inc. 2001-2021.
// Copyright Syncfusion Inc. 2001-2021. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Collections.Generic;
using Syncfusion.SfSchedule.iOS;
#if __UNIFIED__
using Foundation;
using UIKit;
using CoreGraphics;
#else
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using CGRect = System.Drawing.RectangleF;
using CGPoint = System.Drawing.PointF;
using CGSize = System.Drawing.SizeF;
using nfloat = System.Single;
using System.Drawing;
#endif
namespace SampleBrowser
{
public class DragDrop : SampleView
{
/// <summary>
/// Initialize the schedule.
/// </summary>
private SFSchedule schedule = new SFSchedule();
/// <summary>
/// Label for toast view.
/// </summary>
private UILabel toastUIView;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public DragDrop()
{
schedule = new SFSchedule();
schedule.ScheduleView = SFScheduleView.SFScheduleViewWeek;
NonAccessibleBlock nonAccessibleBlock = new NonAccessibleBlock();
//Create new instance of NonAccessibleBlocksCollection
NSMutableArray nonAccessibleBlocksCollection = new NSMutableArray();
WeekViewSettings weekViewSettings = new WeekViewSettings();
nonAccessibleBlock.StartHour = 13;
nonAccessibleBlock.EndHour = 14;
nonAccessibleBlock.Text = (NSString)"LUNCH";
nonAccessibleBlock.BackgroundColor = UIColor.Black;
nonAccessibleBlocksCollection.Add(nonAccessibleBlock);
weekViewSettings.NonAccessibleBlockCollection = nonAccessibleBlocksCollection;
schedule.WeekViewSettings = weekViewSettings;
schedule.AllowAppointmentDrag = true;
schedule.AppointmentDrop += Schedule_AppointmentDrop;
schedule.ItemsSource = CreateAppointments();
this.AddSubview(schedule);
}
/// <summary>
/// drop event for schedule appointment.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Schedule_AppointmentDrop(object sender, AppointmentDropEventArgs e)
{
e.Cancel = false;
if (IsInNonAccessRegion(e.DropTime))
{
DisplayToast("Cannot be moved to blocked time slots");
e.Cancel = true;
return;
}
DisplayToast("Moved to " + FormattedDate(e.DropTime));
}
private string FormattedDate(NSDate startDate)
{
NSDateFormatter dateFormat = new NSDateFormatter();
dateFormat.DateFormat = "EEEE, MMM d, h:mm a";
return dateFormat.ToString(startDate);
}
/// <summary>
/// check the appointment with in non accessible block region.
/// </summary>
/// <param name="dropTime"></param>
/// <returns></returns>
private bool IsInNonAccessRegion(NSDate dropTime)
{
NSCalendar calendar = NSCalendar.CurrentCalendar;
NSDateComponents start = calendar.Components(
NSCalendarUnit.Year |
NSCalendarUnit.Month |
NSCalendarUnit.Day |
NSCalendarUnit.Hour |
NSCalendarUnit.Minute |
NSCalendarUnit.Second,
dropTime);
if ((schedule.WeekViewSettings.NonAccessibleBlockCollection.GetItem<NonAccessibleBlock>(0).StartHour == start.Hour)
|| (schedule.WeekViewSettings.NonAccessibleBlockCollection.GetItem<NonAccessibleBlock>(0).StartHour - 1 == start.Hour && start.Minute > 0))
{
return true;
}
return false;
}
/// <summary>
/// displaying toast when drag or drop the appointment.
/// </summary>
/// <param name="toastText"></param>
private void DisplayToast(string toastText)
{
toastUIView = new UILabel();
toastUIView.Hidden = false;
toastUIView.Text = toastText;
toastUIView.Layer.CornerRadius = 20f;
toastUIView.TextColor = UIColor.White;
toastUIView.LineBreakMode = UILineBreakMode.WordWrap;
toastUIView.Lines = 2;
toastUIView.TextAlignment = UITextAlignment.Center;
toastUIView.ClipsToBounds = true;
toastUIView.Font = UIFont.SystemFontOfSize(14);
toastUIView.BackgroundColor = UIColor.FromRGB(101.0f / 255.0f, 101.0f / 255.0f, 101.0f / 255.0f);
toastUIView.Layer.ZPosition = 10;
this.AddSubview(toastUIView);
toastUIView.Frame = new CGRect(this.Frame.Right / 10, this.Frame.Bottom - 150, this.Frame.Right - ((this.Frame.Right / 10) * 2), 50);
UIView.Animate(
0.5,
0,
UIViewAnimationOptions.CurveLinear,
() =>
{
toastUIView.Alpha = 1.0f;
},
() =>
{
UIView.Animate(
3.0,
() =>
{
toastUIView.Alpha = 0.0f;
});
});
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (schedule != null)
{
schedule.AppointmentDrop -= Schedule_AppointmentDrop;
schedule.Dispose();
schedule = null;
}
if(toastUIView != null)
{
toastUIView.Dispose();
toastUIView = null;
}
}
base.Dispose(disposing);
}
public override void LayoutSubviews()
{
foreach (var view in this.Subviews)
{
if (view is UILabel)
{
this.BringSubviewToFront(toastUIView);
toastUIView.Frame = new CGRect(this.Frame.Right / 10, this.Frame.Bottom - 150, this.Frame.Right - ((this.Frame.Right / 10) * 2), 50);
}
else
{
view.Frame = Bounds;
}
}
base.LayoutSubviews();
}
/// <summary>
/// create appointments.
/// </summary>
/// <returns></returns>
private IEnumerable<ScheduleAppointment> CreateAppointments()
{
NSDate today = new NSDate();
SetColors();
SetSubjects();
List<ScheduleAppointment> appCollection = new List<ScheduleAppointment>();
NSCalendar calendar = NSCalendar.CurrentCalendar;
// Get the year, month, day from the date
NSDateComponents components = calendar.Components(
NSCalendarUnit.Year | NSCalendarUnit.Month | NSCalendarUnit.Day, today);
// Set the hour, minute, second
components.Hour = 10;
components.Minute = 0;
components.Second = 0;
// Get the year, month, day from the date
NSDateComponents endDateComponents = calendar.Components(NSCalendarUnit.Year | NSCalendarUnit.Month | NSCalendarUnit.Day, today);
// Set the hour, minute, second
endDateComponents.Hour = 12;
endDateComponents.Minute = 0;
endDateComponents.Second = 0;
Random randomNumber = new Random();
for (int i = 0; i < 10; i++)
{
components.Hour = randomNumber.Next(10, 16);
endDateComponents.Hour = components.Hour + randomNumber.Next(1, 1);
NSDate startDate = calendar.DateFromComponents(components);
NSDate endDate = calendar.DateFromComponents(endDateComponents);
ScheduleAppointment appointment = new ScheduleAppointment();
appointment.StartTime = startDate;
appointment.EndTime = endDate;
components.Day = components.Day + 1;
endDateComponents.Day = endDateComponents.Day + 1;
appointment.Subject = (NSString)subjectCollection[i];
appointment.AppointmentBackground = colorCollection[i];
if (i == 10 || i == 7)
{
appointment.IsAllDay = true;
}
appCollection.Add(appointment);
}
return appCollection;
}
//adding subject collection.
private List<String> subjectCollection;
private void SetSubjects()
{
subjectCollection = new List<String>();
subjectCollection.Add("GoToMeeting");
subjectCollection.Add("Business Meeting");
subjectCollection.Add("Conference");
subjectCollection.Add("Project Status Discussion");
subjectCollection.Add("Auditing");
subjectCollection.Add("Client Meeting");
subjectCollection.Add("Generate Report");
subjectCollection.Add("Target Meeting");
subjectCollection.Add("General Meeting");
subjectCollection.Add("Pay House Rent");
subjectCollection.Add("Car Service");
subjectCollection.Add("Medical Check Up");
subjectCollection.Add("Wedding Anniversary");
subjectCollection.Add("Sam's Birthday");
subjectCollection.Add("Jenny's Birthday");
}
// adding colors collection
private List<UIColor> colorCollection;
private void SetColors()
{
colorCollection = new List<UIColor>();
colorCollection.Add(UIColor.FromRGB(0xA2, 0xC1, 0x39));
colorCollection.Add(UIColor.FromRGB(0xD8, 0x00, 0x73));
colorCollection.Add(UIColor.FromRGB(0x1B, 0xA1, 0xE2));
colorCollection.Add(UIColor.FromRGB(0xE6, 0x71, 0xB8));
colorCollection.Add(UIColor.FromRGB(0xF0, 0x96, 0x09));
colorCollection.Add(UIColor.FromRGB(0x33, 0x99, 0x33));
colorCollection.Add(UIColor.FromRGB(0x00, 0xAB, 0xA9));
colorCollection.Add(UIColor.FromRGB(0xE6, 0x71, 0xB8));
colorCollection.Add(UIColor.FromRGB(0x1B, 0xA1, 0xE2));
colorCollection.Add(UIColor.FromRGB(0xD8, 0x00, 0x73));
colorCollection.Add(UIColor.FromRGB(0xA2, 0xC1, 0x39));
colorCollection.Add(UIColor.FromRGB(0xD8, 0x00, 0x73));
colorCollection.Add(UIColor.FromRGB(0x33, 0x99, 0x33));
colorCollection.Add(UIColor.FromRGB(0xE6, 0x71, 0xB8));
colorCollection.Add(UIColor.FromRGB(0x00, 0xAB, 0xA9));
}
}
}