AssignmentList.xaml.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. using Comal.Classes;
  7. using Xamarin.Forms;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using comal.timesheets.CustomControls;
  11. using comal.timesheets.iOS.Assignments;
  12. using InABox.Configuration;
  13. using Syncfusion.Office;
  14. using Syncfusion.SfSchedule.XForms;
  15. using Xamarin.Forms.Xaml;
  16. using XF.Material.Forms;
  17. using XF.Material.Forms.UI.Dialogs;
  18. using XF.Material.Forms.UI.Dialogs.Configurations;
  19. namespace comal.timesheets
  20. {
  21. public enum AssignmentView
  22. {
  23. Day,
  24. TimeLine
  25. }
  26. public partial class AssignmentList : ContentPage
  27. {
  28. private AssignmentEdit _editor = null;
  29. private Guid[] _employeeids = new Guid[] { };
  30. private AssignmentModuleSettings _settings = null;
  31. private AssignmentView _view = AssignmentView.Day;
  32. public AssignmentList()
  33. {
  34. InitializeComponent();
  35. _settings = new LocalConfiguration<AssignmentModuleSettings>().Load();
  36. DatePicker.Date = _settings.Date.IsEmpty() ? DateTime.Today : _settings.Date;
  37. _view = _settings.View;
  38. _employeeids = (_settings.Employees != null)
  39. ? _settings.Employees
  40. : new Guid[] { App.Data.Employee.ID };
  41. }
  42. protected override void OnAppearing()
  43. {
  44. base.OnAppearing();
  45. Reload();
  46. }
  47. private void Reload()
  48. {
  49. DayView.DataSource = null;
  50. TimeLineView.DataSource = null;
  51. if (_view == AssignmentView.Day)
  52. {
  53. DayViewColumn.Width = new GridLength(1, GridUnitType.Star);
  54. TimeLineViewColumn.Width = new GridLength(0, GridUnitType.Absolute);
  55. DayView.DayViewSettings.DayLabelSettings.TimeFormat = "HH:mm";
  56. }
  57. else
  58. {
  59. DayViewColumn.Width = new GridLength(0, GridUnitType.Absolute);
  60. TimeLineViewColumn.Width = new GridLength(1, GridUnitType.Star);
  61. TimeLineView.ResourceViewSettings.VisibleResourceCount = Math.Max(1,Math.Min(8, _employeeids.Length));
  62. TimeLineView.TimelineViewSettings.AppointmentHeight =
  63. (this.Height / TimeLineView.ResourceViewSettings.VisibleResourceCount) + 100;
  64. var resources = new ObservableCollection<object>();
  65. foreach (var empid in _employeeids)
  66. {
  67. var empname = GlobalVariables.EmployeeShells.FirstOrDefault(x => x.ID == empid)?.Name ?? empid.ToString();
  68. resources.Add(
  69. new ScheduleResource()
  70. {
  71. Name = empname, //String.Join("", empname.Split(' ').Select(x=>x.Substring(0,1))),
  72. Id = empid,
  73. Color = Color.Red,
  74. Image = ""
  75. }
  76. );
  77. }
  78. TimeLineView.ScheduleResources = resources;
  79. TimeLineView.ShowResourceView = true;
  80. }
  81. Refresh();
  82. }
  83. private void Refresh()
  84. {
  85. Title.Text = $"{DatePicker.Date:dd MMMM yyyy}";
  86. DataModel.Load(
  87. new Filter<Assignment>(x => x.Date).IsEqualTo(DatePicker.Date).And(x => x.EmployeeLink.ID).InList(_employeeids),
  88. () =>
  89. {
  90. Dispatcher.BeginInvokeOnMainThread(() =>{
  91. if (_view == AssignmentView.Day)
  92. DayView.DataSource = new ObservableCollection<AssignmentListDataModelItem>(DataModel.Items);
  93. else
  94. TimeLineView.DataSource = new ObservableCollection<AssignmentListDataModelItem>(DataModel.Items);
  95. });
  96. }
  97. );
  98. }
  99. private void BackButton_OnClicked(object sender, EventArgs e)
  100. {
  101. Navigation.PopAsync();
  102. }
  103. private void SelectedDate_Tapped(object sender, EventArgs e)
  104. {
  105. DatePicker.Focus();
  106. }
  107. private void DatePicker_OnDateSelected(object sender, DateChangedEventArgs e)
  108. {
  109. if (_employeeids.Any())
  110. {
  111. _settings.Date = DatePicker.Date;
  112. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  113. }
  114. DayView.MoveToDate = DatePicker.Date;
  115. TimeLineView.MoveToDate = DatePicker.Date;
  116. Dispatcher.BeginInvokeOnMainThread(()=>
  117. {
  118. Refresh();
  119. });
  120. }
  121. private async void SelectEmployees_Tapped(object sender, EventArgs e)
  122. {
  123. var actions = new List<string>() { "Only Me" };
  124. actions.AddRange(GlobalVariables.TeamEmployeeShells.Where(x=>x.ID == App.Data.Employee.ID).Select(x=>x.TeamName).Distinct());
  125. var result = await MaterialDialog.Instance.SelectActionAsync(title: "Select a Team",
  126. actions: actions);
  127. if (result == 0)
  128. {
  129. _view = AssignmentView.Day;
  130. _employeeids = new Guid[] { App.Data.Employee.ID };
  131. }
  132. else if (result > 0)
  133. {
  134. _view = AssignmentView.TimeLine;
  135. _employeeids = GlobalVariables.TeamEmployeeShells.Where(x => String.Equals(x.TeamName, actions[result]))
  136. .Select(x => x.ID).Distinct().ToArray();
  137. }
  138. _settings.Employees = _employeeids;
  139. _settings.View = _view;
  140. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  141. Dispatcher.BeginInvokeOnMainThread(()=>
  142. {
  143. Reload();
  144. });
  145. }
  146. private async void Schedule_OnCellTapped(object sender, CellTappedEventArgs e)
  147. {
  148. if (e.Appointment is AssignmentListDataModelItem item)
  149. {
  150. var editor = new AssignmentEdit(item);
  151. Navigation.PushAsync(editor);
  152. }
  153. }
  154. private async void Schedule_OnCellLongPressed(object sender, CellTappedEventArgs e)
  155. {
  156. if (e.Appointment == null)
  157. {
  158. if (InABox.Core.Security.CanEdit<Assignment>())
  159. {
  160. var assignment = new Assignment()
  161. {
  162. Date = e.Datetime.Date,
  163. Start = new TimeSpan(e.Datetime.TimeOfDay.Hours,0,0),
  164. Finish = e.Datetime.TimeOfDay.Add(new TimeSpan(1, 0, 0)),
  165. Duration = new TimeSpan(1, 0, 0),
  166. Title = "New Assignment"
  167. };
  168. assignment.EmployeeLink.ID = (e.Resource is ScheduleResource sr)
  169. ? (Guid)sr.Id
  170. : App.Data.Employee.ID;
  171. var editor = new AssignmentEdit(assignment);
  172. Navigation.PushAsync(editor);
  173. }
  174. }
  175. else if (InABox.Core.Security.CanDelete<Assignment>())
  176. {
  177. var confirm = await MaterialDialog.Instance.ConfirmAsync(
  178. "Are you sure you wish to delete this assignment?",
  179. "Confirm Deletion",
  180. "Yes, Delete",
  181. "Cancel",
  182. new MaterialAlertDialogConfiguration()
  183. {
  184. ButtonFontFamily = Material.FontFamily.Body2
  185. }
  186. );
  187. if (confirm == true)
  188. {
  189. using(await MaterialDialog.Instance.LoadingDialogAsync(message: "Deleting Assignment"))
  190. {
  191. var assignment = new Assignment() { ID = (e.Appointment as AssignmentListDataModelItem).Id };
  192. new Client<Assignment>().Delete(assignment, "Deleted on Mobile Device");
  193. }
  194. Refresh();
  195. }
  196. }
  197. }
  198. }
  199. }