AssignmentList.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. {
  145. if (_view == AssignmentView.Day)
  146. {
  147. DayView.DataSource = new ObservableCollection<AssignmentListDataModelItem>(DataModel.Items);
  148. DayView.MoveToDate = ShowRelevantTime();
  149. }
  150. else
  151. {
  152. TimeLineView.DataSource = new ObservableCollection<AssignmentListDataModelItem>(DataModel.Items);
  153. TimeLineView.MoveToDate = ShowRelevantTime();
  154. }
  155. });
  156. }
  157. );
  158. }
  159. private DateTime ShowRelevantTime()
  160. {
  161. return (DataModel.Items.Count > 0 ? (DataModel.Items.First() as AssignmentListDataModelItem).StartTime : DateTime.Now).AddMinutes(-30);
  162. }
  163. private void SelectedDate_Tapped(object sender, EventArgs e)
  164. {
  165. DatePicker.Focus();
  166. }
  167. private void DatePicker_OnDateSelected(object sender, DateChangedEventArgs e)
  168. {
  169. if (_employeeids.Any())
  170. {
  171. _settings.Date = DatePicker.Date;
  172. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  173. }
  174. DayView.MoveToDate = DatePicker.Date;
  175. TimeLineView.MoveToDate = DatePicker.Date;
  176. Dispatcher.BeginInvokeOnMainThread(() =>
  177. {
  178. Refresh();
  179. });
  180. }
  181. private async void SelectEmployees_Tapped(object sender, EventArgs e)
  182. {
  183. var actions = new List<string>() { "Only Me" };
  184. //actions.AddRange(GlobalVariables.TeamEmployeeShells.Where(x=>x.ID == App.Data.Employee.ID).Select(x=>x.TeamName).Distinct());
  185. actions.AddRange(GlobalVariables.TeamEmployeeShells.Select(x => x.TeamName).Distinct());
  186. var result = await MaterialDialog.Instance.SelectActionAsync(title: "Select a Team",
  187. actions: actions);
  188. if (result == 0)
  189. {
  190. _view = AssignmentView.Day;
  191. _employeeids = new Guid[] { App.Data.Employee.ID };
  192. _teamname = App.Data.Employee.Name;
  193. }
  194. else if (result > 0)
  195. {
  196. _view = AssignmentView.TimeLine;
  197. _employeeids = GlobalVariables.TeamEmployeeShells.Where(x => String.Equals(x.TeamName, actions[result]))
  198. .Select(x => x.ID).Distinct().ToArray();
  199. _teamname = actions[result];
  200. }
  201. _settings.Employees = _employeeids;
  202. _settings.View = _view;
  203. _settings.TeamName = _teamname;
  204. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  205. Dispatcher.BeginInvokeOnMainThread(() =>
  206. {
  207. Reload();
  208. });
  209. }
  210. private void Lookups_OnItemTapped(object sender, ItemTappedEventArgs e)
  211. {
  212. if (e.Item is AssignmentLookupItem lookup)
  213. {
  214. if (lookup.Selected)
  215. lookup.Selected = false;
  216. else
  217. {
  218. IList<AssignmentLookupItem> lookups = _lookuptype == AssignmentLookupType.Tasks
  219. ? _kanbans.Items.ToArray<AssignmentLookupItem>()
  220. : _jobs.Items.ToArray<AssignmentLookupItem>();
  221. {
  222. foreach (var other in lookups.Where(x => x.Selected).ToArray())
  223. other.Selected = false;
  224. }
  225. lookup.Selected = true;
  226. }
  227. }
  228. }
  229. private void LookupsType_Tapped(object sender, EventArgs e)
  230. {
  231. _lookuptype = _lookuptype == AssignmentLookupType.Tasks
  232. ? AssignmentLookupType.ActiveJobs
  233. : _lookuptype == AssignmentLookupType.ActiveJobs
  234. ? AssignmentLookupType.UnbookedJobs
  235. : AssignmentLookupType.Tasks;
  236. _settings.LookupType = _lookuptype;
  237. LookupType.Text = CoreUtils.Neatify(_lookuptype.ToString());
  238. new LocalConfiguration<AssignmentModuleSettings>().Save(_settings);
  239. RefreshLookups();
  240. }
  241. private async void Schedule_OnCellTapped(object sender, CellTappedEventArgs e)
  242. {
  243. if (e.Appointment is AssignmentListDataModelItem item)
  244. {
  245. var editor = new AssignmentEdit(item);
  246. Navigation.PushAsync(editor);
  247. }
  248. }
  249. private async void Schedule_OnCellLongPressed(object sender, CellTappedEventArgs e)
  250. {
  251. if (e.Appointment == null)
  252. {
  253. if (InABox.Core.Security.CanEdit<Assignment>())
  254. {
  255. CreateAssignment(
  256. e.Datetime,
  257. e.Resource as ScheduleResource
  258. );
  259. }
  260. }
  261. else if (InABox.Core.Security.CanDelete<Assignment>()
  262. && e.Appointment is AssignmentListDataModelItem assignment)
  263. {
  264. await DeleteAssignment(assignment.Id);
  265. }
  266. }
  267. private void CreateAssignment(DateTime date, ScheduleResource resource)
  268. {
  269. var assignment = new Assignment()
  270. {
  271. Date = date.Date,
  272. Title = "New Assignment",
  273. };
  274. assignment.Booked.Start = new TimeSpan(date.TimeOfDay.Hours, 0, 0);
  275. assignment.Booked.Finish = date.TimeOfDay.Add(new TimeSpan(1, 0, 0));
  276. assignment.Booked.Duration = new TimeSpan(1, 0, 0);
  277. assignment.EmployeeLink.ID = (resource is ScheduleResource sr)
  278. ? (Guid)sr.Id
  279. : App.Data.Employee.ID;
  280. var job = (_lookuptype == AssignmentLookupType.ActiveJobs) || (_lookuptype == AssignmentLookupType.UnbookedJobs)
  281. ? _jobs.Items.FirstOrDefault(x => x.Selected)
  282. : null;
  283. if (job != null)
  284. {
  285. assignment.JobLink.ID = job.Id;
  286. assignment.JobLink.JobNumber = job.Number;
  287. assignment.JobLink.Name = job.Name;
  288. assignment.Description = job.Description;
  289. assignment.Title = job.Name;
  290. job.Selected = false;
  291. }
  292. var task = _lookuptype == AssignmentLookupType.Tasks
  293. ? _kanbans.Items.FirstOrDefault(x => x.Selected)
  294. : null;
  295. if (task != null)
  296. {
  297. assignment.Task.ID = task.Id;
  298. assignment.Task.Number = int.Parse(task.Number);
  299. assignment.Task.Title = task.Name;
  300. assignment.Title = task.Name;
  301. assignment.Description = task.Description;
  302. task.Selected = false;
  303. }
  304. var editor = new AssignmentEdit(assignment);
  305. Navigation.PushAsync(editor);
  306. }
  307. private async Task DeleteAssignment(Guid id)
  308. {
  309. var confirm = await MaterialDialog.Instance.ConfirmAsync(
  310. "Are you sure you wish to delete this assignment?",
  311. "Confirm Deletion",
  312. "Yes, Delete",
  313. "Cancel",
  314. new MaterialAlertDialogConfiguration()
  315. {
  316. ButtonFontFamily = Material.FontFamily.Body2
  317. }
  318. );
  319. if (confirm == true)
  320. {
  321. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Deleting Assignment"))
  322. {
  323. var assignment = new Assignment() { ID = id };
  324. new Client<Assignment>().Delete(assignment, "Deleted on Mobile Device");
  325. }
  326. Refresh();
  327. }
  328. }
  329. }
  330. }