AssignmentList.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 System.Windows.Input;
  11. using InABox.Configuration;
  12. using Syncfusion.SfSchedule.XForms;
  13. using WebSocketSharp;
  14. using XF.Material.Forms;
  15. using XF.Material.Forms.UI.Dialogs;
  16. using XF.Material.Forms.UI.Dialogs.Configurations;
  17. namespace comal.timesheets
  18. {
  19. public enum AssignmentView
  20. {
  21. Day,
  22. TimeLine
  23. }
  24. public enum AssignmentLookupType
  25. {
  26. ActiveJobs,
  27. UnbookedJobs,
  28. Tasks
  29. }
  30. public partial class AssignmentList : ContentPage
  31. {
  32. private AssignmentEdit _editor = null;
  33. private Guid[] _employeeids = new Guid[] { };
  34. private AssignmentModuleSettings _settings = null;
  35. private AssignmentView _view = AssignmentView.Day;
  36. private AssignmentLookupType _lookuptype = AssignmentLookupType.UnbookedJobs;
  37. private String _teamname = "";
  38. private AssignmentJobDataModel _jobs = null;
  39. private AssignmentKanbanDataModel _kanbans = null;
  40. public AssignmentList()
  41. {
  42. InitializeComponent();
  43. _settings = new LocalConfiguration<AssignmentModuleSettings>().Load();
  44. _jobs = new AssignmentJobDataModel();
  45. _kanbans = new AssignmentKanbanDataModel();
  46. DatePicker.Date = _settings.Date.IsEmpty() ? DateTime.Today : _settings.Date;
  47. _view = _settings.View;
  48. _employeeids = (_settings.Employees != null)
  49. ? _settings.Employees
  50. : new Guid[] { App.Data.Employee.ID };
  51. _lookuptype = _settings.LookupType;
  52. LookupType.Text = CoreUtils.Neatify(_lookuptype.ToString());
  53. _teamname = _settings.TeamName;
  54. }
  55. protected override void OnAppearing()
  56. {
  57. base.OnAppearing();
  58. RefreshLookups();
  59. Reload();
  60. }
  61. private void RefreshLookups()
  62. {
  63. if (_lookuptype == AssignmentLookupType.Tasks)
  64. {
  65. _kanbans.Load(
  66. new Filter<Kanban>(x=>x.Completed).IsEqualTo(DateTime.MinValue),
  67. () => Dispatcher.BeginInvokeOnMainThread(() =>
  68. {
  69. Lookups.ItemsSource = new ObservableCollection<AssignmentKanbanItem>(_kanbans.Items);
  70. }));
  71. }
  72. else if (_lookuptype == AssignmentLookupType.ActiveJobs)
  73. {
  74. _jobs.Load(
  75. new Filter<Job>(x=>x.JobStatus.Active).IsEqualTo(true),
  76. () => Dispatcher.BeginInvokeOnMainThread(() =>
  77. {
  78. Lookups.ItemsSource = new ObservableCollection<AssignmentJobItem>(_jobs.Items);
  79. }));
  80. }
  81. else if (_lookuptype == AssignmentLookupType.UnbookedJobs)
  82. {
  83. _jobs.Load(
  84. new Filter<Job>(x=>x.JobStatus.Active).IsEqualTo(true)
  85. .And(x=>x.OpenAssignments).IsEqualTo(0),
  86. () => Dispatcher.BeginInvokeOnMainThread(() =>
  87. {
  88. Lookups.ItemsSource = new ObservableCollection<AssignmentJobItem>(_jobs.Items);
  89. }));
  90. }
  91. }
  92. private void Reload()
  93. {
  94. DayView.DataSource = null;
  95. TimeLineView.DataSource = null;
  96. ScheduleType.Text = _teamname;
  97. if (_view == AssignmentView.Day)
  98. {
  99. DayViewColumn.Width = new GridLength(1, GridUnitType.Star);
  100. TimeLineViewColumn.Width = new GridLength(0, GridUnitType.Absolute);
  101. DayView.DayViewSettings.DayLabelSettings.TimeFormat = "HH:mm";
  102. }
  103. else
  104. {
  105. DayViewColumn.Width = new GridLength(0, GridUnitType.Absolute);
  106. TimeLineViewColumn.Width = new GridLength(1, GridUnitType.Star);
  107. TimeLineView.ResourceViewSettings.VisibleResourceCount = Math.Max(1,Math.Min(16, _employeeids.Length));
  108. TimeLineView.TimelineViewSettings.AppointmentHeight =
  109. (this.Height / TimeLineView.ResourceViewSettings.VisibleResourceCount) + 100;
  110. var resources = new ObservableCollection<object>();
  111. foreach (var empid in _employeeids)
  112. {
  113. var empname = GlobalVariables.EmployeeShells.FirstOrDefault(x => x.ID == empid)?.Name ?? empid.ToString();
  114. if (_employeeids.Length > 10)
  115. empname = String.Join("", empname.Split(' ').Select(x => x.Substring(0, 1)));
  116. else if (_employeeids.Length > 6)
  117. {
  118. var comps = empname.Split(' ').ToArray();
  119. empname = $"{comps.First()} {String.Join("", comps.Skip(1).Select(x => x.Substring(0, 1)))}";
  120. }
  121. resources.Add(
  122. new ScheduleResource()
  123. {
  124. Name = empname,
  125. Id = empid,
  126. Color = Color.Transparent,
  127. Image = ""
  128. }
  129. );
  130. }
  131. TimeLineView.ScheduleResources = resources;
  132. TimeLineView.ShowResourceView = true;
  133. }
  134. Refresh();
  135. }
  136. private void Refresh()
  137. {
  138. Title.Text = $"{DatePicker.Date:dd MMMM yyyy}";
  139. DataModel.Load(
  140. new Filter<Assignment>(x => x.Date).IsEqualTo(DatePicker.Date).And(x => x.EmployeeLink.ID).InList(_employeeids),
  141. () =>
  142. {
  143. Dispatcher.BeginInvokeOnMainThread(() =>{
  144. if (_view == AssignmentView.Day)
  145. DayView.DataSource = new ObservableCollection<AssignmentListDataModelItem>(DataModel.Items);
  146. else
  147. TimeLineView.DataSource = new ObservableCollection<AssignmentListDataModelItem>(DataModel.Items);
  148. });
  149. }
  150. );
  151. }
  152. private void SelectedDate_Tapped(object sender, EventArgs e)
  153. {
  154. DatePicker.Focus();
  155. }
  156. private void DatePicker_OnDateSelected(object sender, DateChangedEventArgs e)
  157. {
  158. if (_employeeids.Any())
  159. {
  160. _settings.Date = DatePicker.Date;
  161. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  162. }
  163. DayView.MoveToDate = DatePicker.Date;
  164. TimeLineView.MoveToDate = DatePicker.Date;
  165. Dispatcher.BeginInvokeOnMainThread(()=>
  166. {
  167. Refresh();
  168. });
  169. }
  170. private async void SelectEmployees_Tapped(object sender, EventArgs e)
  171. {
  172. var actions = new List<string>() { "Only Me" };
  173. //actions.AddRange(GlobalVariables.TeamEmployeeShells.Where(x=>x.ID == App.Data.Employee.ID).Select(x=>x.TeamName).Distinct());
  174. actions.AddRange(GlobalVariables.TeamEmployeeShells.Select(x=>x.TeamName).Distinct());
  175. var result = await MaterialDialog.Instance.SelectActionAsync(title: "Select a Team",
  176. actions: actions);
  177. if (result == 0)
  178. {
  179. _view = AssignmentView.Day;
  180. _employeeids = new Guid[] { App.Data.Employee.ID };
  181. _teamname = App.Data.Employee.Name;
  182. }
  183. else if (result > 0)
  184. {
  185. _view = AssignmentView.TimeLine;
  186. _employeeids = GlobalVariables.TeamEmployeeShells.Where(x => String.Equals(x.TeamName, actions[result]))
  187. .Select(x => x.ID).Distinct().ToArray();
  188. _teamname = actions[result];
  189. }
  190. _settings.Employees = _employeeids;
  191. _settings.View = _view;
  192. _settings.TeamName = _teamname;
  193. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  194. Dispatcher.BeginInvokeOnMainThread(()=>
  195. {
  196. Reload();
  197. });
  198. }
  199. private void Lookups_OnItemTapped(object sender, ItemTappedEventArgs e)
  200. {
  201. if (e.Item is AssignmentLookupItem lookup)
  202. {
  203. if (lookup.Selected)
  204. lookup.Selected = false;
  205. else
  206. {
  207. IList<AssignmentLookupItem> lookups = _lookuptype == AssignmentLookupType.Tasks
  208. ? _kanbans.Items.ToArray<AssignmentLookupItem>()
  209. : _jobs.Items.ToArray<AssignmentLookupItem>();
  210. {
  211. foreach (var other in lookups.Where(x => x.Selected).ToArray())
  212. other.Selected = false;
  213. }
  214. lookup.Selected = true;
  215. }
  216. }
  217. }
  218. private void LookupsType_Tapped(object sender, EventArgs e)
  219. {
  220. _lookuptype = _lookuptype == AssignmentLookupType.Tasks
  221. ? AssignmentLookupType.ActiveJobs
  222. : _lookuptype == AssignmentLookupType.ActiveJobs
  223. ? AssignmentLookupType.UnbookedJobs
  224. : AssignmentLookupType.Tasks;
  225. _settings.LookupType = _lookuptype;
  226. LookupType.Text = CoreUtils.Neatify(_lookuptype.ToString());
  227. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  228. RefreshLookups();
  229. }
  230. private async void Schedule_OnCellTapped(object sender, CellTappedEventArgs e)
  231. {
  232. if (e.Appointment is AssignmentListDataModelItem item)
  233. {
  234. var editor = new AssignmentEdit(item);
  235. Navigation.PushAsync(editor);
  236. }
  237. }
  238. private async void Schedule_OnCellLongPressed(object sender, CellTappedEventArgs e)
  239. {
  240. if (e.Appointment == null)
  241. {
  242. if (InABox.Core.Security.CanEdit<Assignment>())
  243. {
  244. CreateAssignment(
  245. e.Datetime,
  246. e.Resource as ScheduleResource
  247. );
  248. }
  249. }
  250. else if (InABox.Core.Security.CanDelete<Assignment>()
  251. && e.Appointment is AssignmentListDataModelItem assignment)
  252. {
  253. await DeleteAssignment(assignment.Id);
  254. }
  255. }
  256. private void CreateAssignment(DateTime date, ScheduleResource resource)
  257. {
  258. var assignment = new Assignment()
  259. {
  260. Date = date.Date,
  261. Title = "New Assignment",
  262. };
  263. assignment.Booked.Start = new TimeSpan(date.TimeOfDay.Hours, 0, 0);
  264. assignment.Booked.Finish = date.TimeOfDay.Add(new TimeSpan(1, 0, 0));
  265. assignment.Booked.Duration = new TimeSpan(1, 0, 0);
  266. assignment.EmployeeLink.ID = (resource is ScheduleResource sr)
  267. ? (Guid)sr.Id
  268. : App.Data.Employee.ID;
  269. var job = (_lookuptype == AssignmentLookupType.ActiveJobs) || (_lookuptype == AssignmentLookupType.UnbookedJobs)
  270. ? _jobs.Items.FirstOrDefault(x => x.Selected)
  271. : null;
  272. if (job != null)
  273. {
  274. assignment.JobLink.ID = job.Id;
  275. assignment.JobLink.JobNumber = job.Number;
  276. assignment.JobLink.Name = job.Name;
  277. assignment.Description = job.Description;
  278. assignment.Title = job.Name;
  279. job.Selected = false;
  280. }
  281. var task = _lookuptype == AssignmentLookupType.Tasks
  282. ? _kanbans.Items.FirstOrDefault(x => x.Selected)
  283. : null;
  284. if (task != null)
  285. {
  286. assignment.Task.ID = task.Id;
  287. assignment.Task.Number = int.Parse(task.Number);
  288. assignment.Task.Title = task.Name;
  289. assignment.Title = task.Name;
  290. assignment.Description = task.Description;
  291. task.Selected = false;
  292. }
  293. var editor = new AssignmentEdit(assignment);
  294. Navigation.PushAsync(editor);
  295. }
  296. private async Task DeleteAssignment(Guid id)
  297. {
  298. var confirm = await MaterialDialog.Instance.ConfirmAsync(
  299. "Are you sure you wish to delete this assignment?",
  300. "Confirm Deletion",
  301. "Yes, Delete",
  302. "Cancel",
  303. new MaterialAlertDialogConfiguration()
  304. {
  305. ButtonFontFamily = Material.FontFamily.Body2
  306. }
  307. );
  308. if (confirm == true)
  309. {
  310. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Deleting Assignment"))
  311. {
  312. var assignment = new Assignment() { ID = id };
  313. new Client<Assignment>().Delete(assignment, "Deleted on Mobile Device");
  314. }
  315. Refresh();
  316. }
  317. }
  318. }
  319. }