PageRenderTime 22ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/ScheduleView/GroupingByTimeMarkers/ScheduleViewHelper_SL.cs

https://gitlab.com/webartoli/repro-lowquality-exporttoimage-radtreeview
C# | 318 lines | 276 code | 42 blank | 0 comment | 68 complexity | 35cdfcdb2754c0e735b943f2ebc3f829 MD5 | raw file
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Windows;
  5. using Telerik.Windows.Controls;
  6. using Telerik.Windows.Controls.ScheduleView;
  7. namespace GroupingByTimeMarkers
  8. {
  9. public class ScheduleViewHelper
  10. {
  11. private static bool IsAppointmentChangedThroughDialog;
  12. private static RadScheduleView scheduleView;
  13. public static readonly DependencyProperty IsTimeMarkerGroupingEnabledProperty =
  14. DependencyProperty.RegisterAttached("IsTimeMarkerGroupingEnabled", typeof(bool), typeof(ScheduleViewHelper), new PropertyMetadata(false, OnIsTimeMarkerGroupingEnabledChagned));
  15. public static bool GetIsTimeMarkerGroupingEnabled(DependencyObject obj)
  16. {
  17. return (bool)obj.GetValue(IsTimeMarkerGroupingEnabledProperty);
  18. }
  19. public static void SetIsTimeMarkerGroupingEnabled(DependencyObject obj, bool value)
  20. {
  21. obj.SetValue(IsTimeMarkerGroupingEnabledProperty, value);
  22. }
  23. private static void OnIsTimeMarkerGroupingEnabledChagned(DependencyObject d, DependencyPropertyChangedEventArgs e)
  24. {
  25. d.Dispatcher.BeginInvoke(new Action(() =>
  26. {
  27. scheduleView = d as RadScheduleView;
  28. if (scheduleView != null && e.NewValue != e.OldValue)
  29. {
  30. if ((bool)e.NewValue)
  31. {
  32. var viewModel = scheduleView.DataContext as MyViewModel;
  33. var timeMarkers = scheduleView.TimeMarkersSource;
  34. var timeMarkerResources = new ResourceType("TimeMarkers");
  35. foreach (TimeMarker marker in timeMarkers)
  36. {
  37. Resource res = new Resource(marker.TimeMarkerName);
  38. timeMarkerResources.Resources.Add(res);
  39. }
  40. viewModel.ResourcesTypes.Add(timeMarkerResources);
  41. ResourceGroupDescription groupDescription = new ResourceGroupDescription();
  42. groupDescription.ResourceType = timeMarkerResources.Name;
  43. groupDescription.ShowNullGroup = true;
  44. viewModel.GroupDescriptions.Add(groupDescription);
  45. scheduleView.AppointmentCreated += OnAppointmentCreated;
  46. scheduleView.AppointmentEdited += OnAppointmentEdited;
  47. scheduleView.DialogClosing += OnDialogClosing;
  48. scheduleView.Loaded += OnLoaded;
  49. }
  50. else
  51. {
  52. scheduleView.AppointmentCreated -= OnAppointmentCreated;
  53. scheduleView.AppointmentEdited -= OnAppointmentEdited;
  54. scheduleView.DialogClosing -= OnDialogClosing;
  55. scheduleView.Loaded -= OnLoaded;
  56. }
  57. }
  58. }));
  59. }
  60. private static void OnLoaded(object sender, RoutedEventArgs e)
  61. {
  62. var viewModel = scheduleView.DataContext as MyViewModel;
  63. viewModel.Appointments = viewModel.GenerateAppointments();
  64. scheduleView.AppointmentsSource = viewModel.Appointments;
  65. foreach (var app in viewModel.Appointments)
  66. {
  67. UpdateMasterAppointment((Appointment)app);
  68. }
  69. }
  70. private static void OnDialogClosing(object sender, CancelRoutedEventArgs e)
  71. {
  72. var closeDialogArgs = e as CloseDialogEventArgs;
  73. if (closeDialogArgs != null)
  74. {
  75. if (closeDialogArgs.DialogViewModel is AppointmentDialogViewModel)
  76. {
  77. if (closeDialogArgs.DialogResult.HasValue && closeDialogArgs.DialogResult == true)
  78. {
  79. IsAppointmentChangedThroughDialog = true;
  80. }
  81. else
  82. {
  83. if (closeDialogArgs.DialogResult == false)
  84. {
  85. IsAppointmentChangedThroughDialog = false;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. private static void OnAppointmentEdited(object sender, AppointmentEditedEventArgs e)
  92. {
  93. scheduleView = sender as RadScheduleView;
  94. var editedApp = e.Appointment as Appointment;
  95. var resourcers = editedApp.Resources;
  96. var timeMarker = editedApp.TimeMarker;
  97. var currnetTimeMarkerResouce = scheduleView.ResourceTypesSource.Cast<ResourceType>()
  98. .Select(a => a.Resources).Where(a => a.ResourceType == "TimeMarkers").FirstOrDefault();
  99. if (!editedApp.Resources.Any(a => a.ResourceType == currnetTimeMarkerResouce.ResourceType) && IsAppointmentChangedThroughDialog)
  100. {
  101. if (timeMarker != null)
  102. {
  103. var newResource = currnetTimeMarkerResouce.Where(a => a.DisplayName == timeMarker.TimeMarkerName).FirstOrDefault();
  104. EditAppointment(editedApp, null, newResource);
  105. }
  106. }
  107. else
  108. {
  109. if (IsAppointmentChangedThroughDialog)
  110. {
  111. var currentResource = editedApp.Resources.Select(a => a).Where(a => a.ResourceType == "TimeMarkers").FirstOrDefault();
  112. if (timeMarker != null)
  113. {
  114. var newResource = currnetTimeMarkerResouce.Where(a => a.DisplayName == timeMarker.TimeMarkerName).FirstOrDefault();
  115. if (currentResource != newResource)
  116. {
  117. EditAppointment(editedApp, currentResource, newResource);
  118. }
  119. }
  120. else
  121. {
  122. EditAppointment(editedApp, currentResource, null);
  123. }
  124. if (editedApp.RecurrenceRule != null)
  125. {
  126. if (editedApp.RecurrenceRule.Exceptions.Count > 0)
  127. {
  128. foreach (var excApp in editedApp.RecurrenceRule.Exceptions)
  129. {
  130. var app = excApp.Appointment as Appointment;
  131. var currentExcResource = app.Resources.Select(a => a).Where(a => a.ResourceType == "TimeMarkers").FirstOrDefault();
  132. if (app.TimeMarker != null)
  133. {
  134. var newExcResource = currnetTimeMarkerResouce.Where(a => a.DisplayName == app.TimeMarker.TimeMarkerName).FirstOrDefault();
  135. if(currentExcResource != null)
  136. {
  137. if (app.TimeMarker.TimeMarkerName != currentExcResource.DisplayName)
  138. {
  139. EditAppointment(app, currentExcResource, newExcResource);
  140. UpdateMasterAppointment(editedApp);
  141. }
  142. }
  143. else
  144. {
  145. EditAppointment(app, null, newExcResource);
  146. UpdateMasterAppointment(editedApp);
  147. }
  148. }
  149. else
  150. {
  151. EditAppointment(app, currentExcResource, null);
  152. UpdateMasterAppointment(editedApp);
  153. }
  154. }
  155. }
  156. }
  157. }
  158. else
  159. {
  160. var markers = new ObservableCollection<TimeMarker>(scheduleView.TimeMarkersSource.Cast<TimeMarker>().ToList());
  161. var currResourcesNames = editedApp.Resources.Select(b => b.ResourceName);
  162. var newMarker = markers.Where(a => currResourcesNames.Contains(a.TimeMarkerName)).FirstOrDefault();
  163. if (editedApp.TimeMarker != newMarker)
  164. {
  165. editedApp.TimeMarker = newMarker;
  166. }
  167. if (editedApp.RecurrenceRule != null)
  168. {
  169. if (editedApp.RecurrenceRule.Exceptions.Count > 0)
  170. {
  171. foreach (var excApp in editedApp.RecurrenceRule.Exceptions)
  172. {
  173. var exception = excApp.Appointment as Appointment;
  174. var currExcResourcesNames = exception.Resources.Select(b => b.ResourceName);
  175. var newExcMarker = markers.Where(a => currExcResourcesNames.Contains(a.TimeMarkerName)).FirstOrDefault();
  176. if (exception.TimeMarker != newExcMarker)
  177. {
  178. exception.TimeMarker = newExcMarker;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. if (IsAppointmentChangedThroughDialog)
  186. {
  187. IsAppointmentChangedThroughDialog = false;
  188. }
  189. }
  190. private static void OnAppointmentCreated(object sender, AppointmentCreatedEventArgs e)
  191. {
  192. scheduleView = sender as RadScheduleView;
  193. var createdApp = e.CreatedAppointment as Appointment;
  194. var timeMarker = createdApp.TimeMarker as TimeMarker;
  195. var resourcers = createdApp.Resources;
  196. var currentMarkerResource = scheduleView.ResourceTypesSource.Cast<ResourceType>()
  197. .Select(a => a.Resources).Where(a => a.ResourceType == "TimeMarkers").FirstOrDefault();
  198. if (timeMarker != null)
  199. {
  200. var markers = new ObservableCollection<TimeMarker>(scheduleView.TimeMarkersSource.Cast<TimeMarker>().ToList());
  201. if (!createdApp.Resources.Any(a => a.ResourceType == currentMarkerResource.ResourceType))
  202. {
  203. var newResource = currentMarkerResource.Where(a => a.DisplayName == timeMarker.TimeMarkerName).FirstOrDefault();
  204. createdApp.Resources.Add(newResource);
  205. }
  206. else
  207. {
  208. var oldMarker = markers.Where(a => a.TimeMarkerName.Equals(createdApp.Resources.Select(b => b.ResourceName).FirstOrDefault())).First();
  209. var currTimeMarkerRes = createdApp.Resources.Where(a => a.ResourceName.Equals(oldMarker.TimeMarkerName)).First();
  210. var newResource = currentMarkerResource.Where(a => a.DisplayName == timeMarker.TimeMarkerName).FirstOrDefault();
  211. EditAppointment(createdApp, currTimeMarkerRes, newResource);
  212. }
  213. }
  214. else
  215. {
  216. var markers = new ObservableCollection<TimeMarker>(scheduleView.TimeMarkersSource.Cast<TimeMarker>().ToList());
  217. if (createdApp.Resources.Any(a => a.ResourceType == currentMarkerResource.ResourceType))
  218. {
  219. var currentMarker = markers.Where(a => a.TimeMarkerName.Equals(createdApp.Resources.Select(b => b.ResourceName).FirstOrDefault())).First();
  220. createdApp.TimeMarker = currentMarker;
  221. }
  222. }
  223. if (IsAppointmentChangedThroughDialog)
  224. {
  225. IsAppointmentChangedThroughDialog = false;
  226. }
  227. }
  228. private static void EditAppointment(Appointment app, IResource currentResource, IResource newResource)
  229. {
  230. Application.Current.RootVisual.Dispatcher.BeginInvoke(new Action(() =>
  231. {
  232. if (currentResource != null && newResource != null)
  233. {
  234. if (scheduleView.BeginEdit(app))
  235. {
  236. app.Resources.Remove(currentResource);
  237. app.Resources.Add(newResource);
  238. scheduleView.Commit();
  239. }
  240. }
  241. else
  242. {
  243. if (currentResource != null)
  244. {
  245. if (scheduleView.BeginEdit(app))
  246. {
  247. app.Resources.Remove(currentResource);
  248. scheduleView.Commit();
  249. }
  250. }
  251. else
  252. {
  253. if(newResource != null)
  254. {
  255. if (scheduleView.BeginEdit(app))
  256. {
  257. app.Resources.Add(newResource);
  258. scheduleView.Commit();
  259. }
  260. }
  261. }
  262. }
  263. }));
  264. }
  265. private static void UpdateMasterAppointment(Appointment editedApp)
  266. {
  267. Application.Current.RootVisual.Dispatcher.BeginInvoke(new Action(() =>
  268. {
  269. if (scheduleView.BeginEdit(editedApp))
  270. {
  271. scheduleView.Commit();
  272. }
  273. }));
  274. }
  275. }
  276. }